By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,663 Members | 1,304 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,663 IT Pros & Developers. It's quick & easy.

lxml validation and xpath id function

P: n/a

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=""
<xs:element name="root">
<xs:element ref="child"/>
<xs:element name="child">
<xs:attribute name="id" use="required" type="xs:ID"/>

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.

Jul 1 '08 #1
Share this Question
Share on Google+
1 Reply

P: n/a
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:

Jul 2 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.