By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,898 Members | 1,245 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,898 IT Pros & Developers. It's quick & easy.

form filling XML

P: n/a
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>dinsdag</Tuesday>
<Wednesday>woensdag</Wednesday>
<Thursday>donderdag</Thursday>
<Friday>vrijdag</Friday>
<Saturday>zaterdag</Saturday>
<Sunday>zondag</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=None, fillin=None):
Fo = xml.dom.minidom.parseString(form)
Fi = xml.dom.minidom.parseString(fillin)

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
Share this Question
Share on Google+
2 Replies


P: n/a
"George" <bu*******@hotmail.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.ElementTree 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.findall(".//li"):
text = fill_elem.findtext(elem.text)
if text:
elem.text = text

print ET.tostring(form_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

P: n/a
George wrote:

[form]
<li>Monday</li>
<li>Tuesday</li>
<li>Wednesday</li>
<li>Thursday</li>
<li>Friday</li>
[fillin]
<dutchdays>
<Monday>maandag</Monday>
<Tuesday>dinsdag</Tuesday>
<Wednesday>woensdag</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(form)
for li in xml.xpath.Evaluate("//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.nodeValue

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 discussion thread is closed

Replies have been disabled for this discussion.