[lxml-dev] Using the Xpath id function

jholg at gmx.de jholg at gmx.de
Wed Jul 2 14:08:35 CEST 2008


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:

 >>> from lxml import etree
>>> root = etree.fromstring('<root><child id="foo"/></root>')

>>> root.xpath('//*[@id="foo"]')
['']

>>> for elt in root.xpath('//*[@id="foo"]'): print etree.tostring(elt)
...
<child id="foo"/>
>>> 
  Regarding the xpath id() function I think you'd need a DTD:

 See http://www.w3.org/TR/xpath:

"""

Function: node-set id(object)

The id function selects elements by theirunique ID (see [5.2.1 Unique 
IDs]).

""" 

 and:

 """

5.2.1 Unique IDs

An element node may have a unique identifier (ID).  This is the value of 
the attribute that is declared in the DTD as type ID.  No two elements in a 
document may have the sameunique ID.  If an XML processor reports two 
elements in a document ashaving the same unique ID (which is possible only 
if the document isinvalid) then the second element in document order must 
be treated asnot having a unique ID.


> NOTE: If a document does not have a DTD, then no element in thedocument 
> will have a unique ID.

>  
""" 

 Holger 


> 

> 

-- 
Psssst! Schon vom neuen GMX MultiMessenger gehört?
Der kann`s mit allen: http://www.gmx.net/de/go/multimessenger
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://codespeak.net/pipermail/lxml-dev/attachments/20080702/f0341afb/attachment.htm 


More information about the lxml-dev mailing list