[lxml-dev] Using the Xpath id function
Floris Bruynooghe
floris.bruynooghe at gmail.com
Wed Jul 2 17:31:45 CEST 2008
Hello
2008/7/2 Jim Washington <jwashin at vt.edu>:
> jholg at gmx.de wrote:
>>> 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:
Indeed, converting the schema to a DTD solved my problem! Too simple
to think of myself of course. Somehow the documentation must have
confused me into believing validation with XMLSchema or DTD would work
(I did understand RelaxNG wouldn't work). Converting the schema is
simple using trang anyway (which I need to do in any case as I'm
writing it in compact RelaxNG).
> 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>]
>>>>
That's quite nifty too! Don't think I'll use it this time but very
good to know.
Thanks for your tips
Floris
--
Debian GNU/Linux -- The Power of Freedom
www.debian.org | www.gnu.org | www.kernel.org
More information about the lxml-dev
mailing list