Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

lxml validation and xpath id function

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


floris.bruynooghe at gmail

Jun 30, 2008, 5:25 PM

Post #1 of 2 (121 views)
Permalink
lxml validation and xpath id function

Hi

I'm trying to use the .xpath('id("foo")') function on an lxml tree but
can't get it to work.

Given the following XML: <root><child id="foo"/></root>

And it's XMLSchema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element ref="child"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="child">
<xs:complexType>
<xs:attribute name="id" use="required" type="xs:ID"/>
</xs:complexType>
</xs:element>
</xs:schema>

Or in more readable, compact RelaxNG, form:

element root {
element child {
attribute id { xsd:ID }
}
}

Now I'm trying to parse the XML and use the .xpath() method to find
the <child/> element using the id XPath function:

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.

Cheers
Floris
--
http://mail.python.org/mailman/listinfo/python-list


stefan_ml at behnel

Jul 2, 2008, 9:22 AM

Post #2 of 2 (92 views)
Permalink
Re: lxml validation and xpath id function [In reply to]

Floris Bruynooghe wrote:
> I'm trying to use the .xpath('id("foo")') function on an lxml tree but
> can't get it to work.

Quick follow-up: this has been answered on the lxml mailing list:

http://comments.gmane.org/gmane.comp.python.lxml.devel/3815

Stefan
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.