[lxml-dev] Using the Xpath id function
Jim Washington
jwashin at vt.edu
Wed Jul 2 16:17:12 CEST 2008
jholg at gmx.de wrote:
>
>
> Hi,
>
>> from lxml import etree
>> schema_root = etree.parse(file('schema.xsd'))
>> schema = etree.XMLSchema(schema_root)
>> parser = etree.XMLParser(schema=schema)
>> root = etree.fromstring('<root><child id="foo"/></root>', parser)
>> root.xpath('id("foo")') --> []
>>
>> I was expecting to get the <child/> element with that last statement
>> (well, inside a list that is), but instead I just get an empty list.
>> Is there anything obvious I'm doing wrong? As far as I can see the
>> lxml documentation says this should work.
>
[...]
> You can always check for the id attribute without any schema involvement:
[...]
> Regarding the xpath id() function I think you'd need a DTD:
[...]
xml:id, http://www.w3.org/TR/xml-id/ works OK, if you do not want to
use a schema. At least, it works with lxml 2.1.beta3.
from lxml import etree
XML_NAMESPACE='http://www.w3.org/XML/1998/namespace'
XML_PREFIX= '{%s}' % XML_NAMESPACE
f = etree.Element('test')
f.set(XML_PREFIX+'id','23455')
etree.tostring(f)
'<test xml:id="23455"/>'
g = etree.SubElement(f,'test1')
g.set(XML_PREFIX+'id','23456')
f.xpath('id("23456")')
[<Element test1 at 777520>]
f.xpath('id("23455")')
[<Element test at 7774c8>]
>>>
- Jim Washington
More information about the lxml-dev
mailing list