form filling XML

G

George

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!!
 
F

Fredrik Lundh

George said:
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>
 
P

Paul Boddie

George said:
[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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top