471,349 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,349 software developers and data experts.

ElementTree find with xmlns

I'm having problems parsing a file:
>>tree = ElementTree.fromstring("""<html xmlns="http://www.w3.org/1999/xhtml" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:doap="http://usefulinc.com/ns/doap#">
<body>Hello world</body>
</html>""")
>>print tree.find('body')
None

The above works fine with the first element being a simple <html>, but
not when I have all the xmlns's.

Thanks,
Rob

Oct 13 '07 #1
2 3082
On Oct 12, 11:19 pm, cakebread <cakebr...@gmail.comwrote:
I'm having problems parsing a file:
>tree = ElementTree.fromstring("""<html xmlns="http://www.w3.org/1999/xhtml" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:doap="http://usefulinc.com/ns/doap#">

<body>Hello world</body>
</html>""")
>print tree.find('body')

None

The above works fine with the first element being a simple <html>, but
not when I have all the xmlns's.

Thanks,
Rob

You have to prefix the element name with its namespace. The following
will work
>>tree.find('{http://www.w3.org/1999/xhtml}body')
<Element {http://www.w3.org/1999/xhtml}body at 779d28>

(Python 2.5, OS X 10.4.10)

Oct 13 '07 #2
On Oct 12, 8:35 pm, Rajarshi <rajarshi.g...@gmail.comwrote:
>
You have to prefix the element name with its namespace. The following
will work
>tree.find('{http://www.w3.org/1999/xhtml}body')

<Element {http://www.w3.org/1999/xhtml}body at 779d28>
Pefect, thank you, Rajarshi!

Oct 13 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Matthew Thorley | last post: by
9 posts views Thread by Chris Spencer | last post: by
1 post views Thread by Gerard Flanagan | last post: by
2 posts views Thread by mirandacascade | last post: by
reply views Thread by Mark E. Smith | last post: by
1 post views Thread by Willemsjunk | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.