[lxml-dev] id function of xpath and parseid

Daniel Albeseder daniel.albeseder at tttech.com
Mon Jul 27 10:49:25 CEST 2009


Hi!

I wonder why there is no parseid function inside the objectify module?
Is there a reason, why this is only in etree?

Additionally I wonder, how the `id` function of XPath does work with
lxml. I created a schema-aware parser, which reads an XML-file, where
some attributes are declared as xs:ID inside the schema. However the
`xpath` method always returns an empty list of nodes, even if the IDs
given are inside the XML.

Example:
----

from lxml import etree

xsd = \
"""
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<xsd:element name="root" type="rootType"/>
	<xsd:complexType name="rootType">
		<xsd:sequence>
			<xsd:element name="foo" type="fooType" maxOccurs="unbounded"/>
		</xsd:sequence>
		<xsd:attribute name="id" type="xsd:ID" use="required"/>
	</xsd:complexType>
	<xsd:complexType name="fooType">
		<xsd:attribute name="id" type="xsd:ID" use="optional"/>
	</xsd:complexType>
</xsd:schema>
"""
xml = """<root id="a"><foo id="b"/><foo id="bar"/><foo/></root>"""

schema = etree.XMLSchema (etree.XML (xsd))
parser = etree.XMLParser (schema = schema)
root   = etree.XML (xml, parser = parser)

for i in ["a", "b", "bar"] :
    print root.xpath ("id('%s')" % i)

print "---"

print root.get ("id")
for el in root :
    print el.get ("id")

---

Returns just:

[]
[]
[]
---
a
b
bar
None

where the first three lists should not be empty IMHO. Can anyone help
me?

Best regards
Daniel




More information about the lxml-dev mailing list