tags:

views:

534

answers:

3

I'd like to get some rows (z:row rows) from XML using:

<rs:data>
    <z:row Attribute1="1" Attribute2="1" />
    <z:row Attribute1="2" Attribute2="2" />
    <z:row Attribute1="3" Attribute2="3" />
    <z:row Attribute1="4" Attribute2="4" />
    <z:row Attribute1="5" Attribute2="5" />
    <z:row Attribute1="6" Attribute2="6" />
</rs:data>

I'm having trouble using (Python):

ElementTree.parse('myxmlfile.xml').getroot().findall('//z:row')

I think that two points are invalid in that case.

Anyone knows how can I do that?

A: 

If you don't want to figure out setting up namespaces properly, you can ignore them like this:

XPathGet("//*[local-name() = 'row']")

Which selects every node whose name (without namespace) is row.

Welbog
+1  A: 

The "z:" prefixes represent an XML namespace. you'll need to find out what that namespace is, and do the following:

XmlDocument doc = new XmlDocument();
doc.Load(@"File.xml");
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("z", @"http://thenamespace.com");
XmlNodeList nodes = doc.SelectNodes(@"//z:row", ns);
John Saunders
+1  A: 

If I define the namespaces like this:

<?xml version="1.0"?>
<rs:data xmlns="http://example.com" xmlns:rs="http://example.com/rs" xmlns:z="http://example.com/z"&gt;
  <z:row Attribute1="1" Attribute2="1" />
  <z:row Attribute1="2" Attribute2="2" />
  <z:row Attribute1="3" Attribute2="3" />
  <z:row Attribute1="4" Attribute2="4" />
  <z:row Attribute1="5" Attribute2="5" />
  <z:row Attribute1="6" Attribute2="6" />
</rs:data>

the Python ElementTree-API can be used like this:

ElementTree.parse("r.xml").getroot().findall('{http://example.com/z}row')
# => [<Element {http://example.com/z}row at 551ee0>, <Element {http://example.com/z}row at 551c60>, <Element {http://example.com/z}row at 551f08>, <Element {http://example.com/z}row at 551be8>, <Element {http://example.com/z}row at 551eb8>, <Element {http://example.com/z}row at 551f30>]

See also http://effbot.org/zone/element.htm#xml-namespaces

The lxml tutorial may be more helpful: http://codespeak.net/lxml/tutorial.html#namespaces
tgray
Just remember that "import lxml.etree" == "import xml.etree.ElementTree as etree" with a few exceptions.
tgray