1

I want to evaluate XML documents with this kind of structure:

<?xml version="1.0" encoding="UTF-8"?>
<Service_name version="3.3.0">
...

where Service_name tag name is not constant string, while version attribute is required.

For that purpose this xpath expression: /*[1]/@version evaluates fine with any xpath processor, but I can't figure how with Python ElementTree.

For example:

import xml.etree.ElementTree as ET
doc = ET.parse('sample.xml')
v = find('/*[1]/@version').text

raises KeyError: '@'

I tried similar combinations but none that worked.

How can I get version number from above example document with ElementTree?

theta
  • 24,593
  • 37
  • 119
  • 159

2 Answers2

2

ElementTree uses its own path syntax, which is more or less a subset of xpath. If you want an ElementTree compatible library with full xpath support, try lxml.

Steven
  • 28,002
  • 5
  • 61
  • 51
  • Yes, I use lxml myself, but working on a part of code that should not require additional modules except SPL. – theta Sep 01 '12 at 00:59
  • Then you'll have to select the element using the path syntax, and retrieve the attribute with the `.get()` method: `doc.find('/*[1]').get('version')` (theta's answer is easier if you know it is the root element) – Steven Sep 03 '12 at 08:58
0

Although it's not XPath as question name, this seems like one way:

doc.getroot().get('version')

theta
  • 24,593
  • 37
  • 119
  • 159