473,662 Members | 2,375 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

form filling XML

How can I do the following in python:

given two strings:
form="""
<html>
<head> <title> My Sample Web Page </title> </head>
<body bgcolor="white" >
<p>
What are the weekdays?
<ol>
<li>Monday</li>
<li>Tuesday</li>
<li>Wednesday </li>
<li>Thursday</li>
<li>Friday</li>
</ol>
</p>
</body>
</html>
"""
fillin="""
<dutchdays>
<Monday>maandag </Monday>
<Tuesday>dinsda g</Tuesday>
<Wednesday>woen sdag</Wednesday>
<Thursday>donde rdag</Thursday>
<Friday>vrijdag </Friday>
<Saturday>zater dag</Saturday>
<Sunday>zonda g</Sunday>
</dutchdays>
"""

How can I compare the text in the element tags <li> with the elements
tags in filling and if they match replace the text within the elements
tags <li> with the text in the matching element tag of fillin.
For example Since the text Monday in form matches the Element tag
<Monday> in fillin put maandag in the element tag <li> of Monday.

Kind of a Pseudo Code

import xml.dom.minidom

def xmlform(form=No ne, fillin=None):
Fo = xml.dom.minidom .parseString(fo rm)
Fi = xml.dom.minidom .parseString(fi llin)

Fo information:
get element tags for li
get text for li

Fi information:
get childtags for dutchdays

if(text for li=child tags for dutchdays):
replace child tags for dutchdays text with text for li

There needs to be a loop but I cannot figure out what type of loop
maybe a while len(Fo)>0: to process through the form.

Thanks for the help!!

Oct 13 '05 #1
2 1853
"George" <bu*******@hotm ail.com> wrote:
How can I compare the text in the element tags <li> with the elements
tags in filling and if they match replace the text within the elements
tags <li> with the text in the matching element tag of fillin.
For example Since the text Monday in form matches the Element tag
<Monday> in fillin put maandag in the element tag <li> of Monday.


here's one way to do it:

import elementtree.Ele mentTree as ET
# or: import cElementTree as ET
# or: import lxml.etree import ET

form="""..."""

fillin="""..."" "

form_elem = ET.XML(form)
fill_elem = ET.XML(fillin)

for elem in form_elem.finda ll(".//li"):
text = fill_elem.findt ext(elem.text)
if text:
elem.text = text

print ET.tostring(for m_elem)

using your example, this prints:

<html>
<head> <title> My Sample Web Page </title> </head>
<body bgcolor="white" >
<p>
What are the weekdays?
<ol>
<li>maandag</li>
<li>dinsdag</li>
<li>woensdag</li>
<li>donderdag </li>
<li>vrijdag</li>
</ol>
</p>
</body>
</html>

links:

http://effbot.org/zone/element-index.htm
http://effbot.org/zone/celementtree.htm
http://codespeak.net/lxml/

(if you're on linux, check your local package source for elementtree
packages)

</F>

Oct 13 '05 #2
George wrote:

[form]
<li>Monday</li>
<li>Tuesday</li>
<li>Wednesday </li>
<li>Thursday</li>
<li>Friday</li>
[fillin]
<dutchdays>
<Monday>maandag </Monday>
<Tuesday>dinsda g</Tuesday>
<Wednesday>woen sdag</Wednesday>
[...]
How can I compare the text in the element tags <li> with the elements
tags in filling and if they match replace the text within the elements
tags <li> with the text in the matching element tag of fillin.


You need to first get each of the li elements in form - something which
is best done using XPath:

from xml.dom.minidom import parseString
import xml.xpath
form_doc = parseString(for m)
for li in xml.xpath.Evalu ate("//li", form_doc):
Do something with the element.

Here, the XPath expression "//li" means "get all li elements in the
document". When you're examining an li element, you need to get the
textual content in order to find out which day it is you want to
replace. I recommend the following:

li.normalize()
day_text = li.childNodes[0]
day_str = day_text.nodeVa lue

With this, you have the string value of the day (rather than some
node), and can then go looking for that day's Dutch translation in the
other document using similar techniques. Since you look for the
translation for each li element, you're going to be using nested loops
rather than a single one.

I hope this gets you started on the problem, anyway!

Paul

Oct 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2595
by: Martin Mrazek | last post by:
Hi, I put almost ten pages long HTML form on our server. It sends filled data into DB. Filling takes almost one hour and users complain that they cannot save the partly filled form and continue filling later. Simply all form has to be filled at once. What are ways for saving partly filled HTML forms on user's comp and use it later for completion of filling?
2
1786
by: hennakapoor | last post by:
How do the form filling software determine which text field gets what data i.e. a typical form filling software will ask the user to enter the username / address / password etc. Whenever it goes to a website (ANY website) that requires these data - it tries to supply the relevant data to the most appropriate text field. How does it decide that? If there is any place more relevant than this newsgroup to get more info about this - please...
1
2101
by: jobi.joy | last post by:
I have a requirement of having the capability of offline form filling for my website.So users can some way download the form and can fill out in offline mode. Any standard way to do this. I am not interested to write a webservices or complex Smart client for this. Any simple way like manipulating it in HTML applications(HTA). Any thoughts on it? Regards
3
3941
by: Omar Llanos | last post by:
I have Form1 and Form2 (which is inherited from Form1), and I created a button in Form2 that will fill up a textbox in Form1. What code would do that? I tried the simplest way: //from child form in button2 clicked event: textbox1.Text = "Hello";
1
9862
by: Hananiel | last post by:
I have a User control that I load dynamically into a Form. The designer has no property to let me dock it. How do i Dock it or get it to stretch out to fill. Setting this.Dock = DockStyle.Fill; in the constructor did not work. Neither did implementing the resize event. thanks, Hananiel
10
4408
by: B-Dog | last post by:
I want to run something once my form in completely loaded and drawn but running at the end of form load doesn't do I guess cause of the large dataset fill and the form isn't fully loaded. Where can I stick some code so that I can run it after the form in fully drawn and loaded? I was told there is no real way to know when it is "completely" loaded?
1
2539
by: Galka | last post by:
Hello I have a form to enter names and some other personal information. When a name is entered, it is checked against existing records: maybe, such name was entered before? If yes, user is notified and asked, would you like to see the previous record? If user answers yes, form on the screen is scrolled to the record with the same name. All works well, apart from number of record between navigation buttons: it is still the same number of...
1
4774
by: NumberCruncher | last post by:
Hi All, I am struggling with setting up my first system of tables, forms,and reports, and could use your help! I am setting up a database to keep track of the production of a produced item. The item is a panel, with a specific texture. There are standard panels that are then produced in a limited number of specific textures. The number of panels/project varies, so I keep track of the actual number of panels per project with the count:...
10
1917
by: Anil Gupte | last post by:
I am playing a Windows Media File which is protected by their DRM. I want to be able to automatically fill in the form asking for payment with my saved parameters. Is there any way to do this? Note, this is not on the web - this is a desktop app that has an embedded player or using mciSendString to play the file. Thanx in advance, Anil Gupte www.keeninc.net www.icinema.com
2
1797
by: GarthRodgers | last post by:
Hey all I'm designing an online exam booking system in Access and need to find a way to import table data into Roboform (or some other form filling package) straight from the database. The only two options I can think of are exporting the table as an XML or as a data access page, and taking it from there, but being new to access I'm not entirely sure how these formats work. So, is there any way that data stored within my access database...
0
8343
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8856
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8545
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7365
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6185
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4179
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1992
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.