473,385 Members | 1,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
Jul 1 '08 #1
1 2956
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...xml.devel/3815

Stefan
Jul 2 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mike | last post by:
Related to another topic I just posted, I wanted to discuss ways to optimize the validation of very large (>100MB) XML documents. First, I have no idea if something like this already exists; it...
2
by: Olaf Meyer | last post by:
Apprentently xerces 2.6.0 (Java) does not validate against contraints specified in the schema (e.g. constraints specified via unique element). The validation works with the XML editor I'm using...
5
by: paul_zaoldyeck | last post by:
does anyone know how to validate an xml file against multiple defined schema? can you show me some examples? i'm making here an xml reader.. thank you
30
by: Chas Emerick | last post by:
I looked around for an ElementTree-specific mailing list, but found none -- my apologies if this is too broad a forum for this question. I've been using the lxml variant of the ElementTree API,...
4
by: marcroy.olsen | last post by:
Hi, What to one do if one what to use lxml(http://codespeak.net/lxml/ index.html) on a mac? Best regards
1
by: Ryan K | last post by:
If I have a cElementTree.ElementTree (or the one from the Standard Library), can I use lxml's validation features on it since it implements the same ElementTree API? Thanks, Ryan
2
by: Ampedesign | last post by:
I'm trying to extract all the links on a page with lxml. Ideally, I would like it to return me a list of hrefs of each link on the page, in a list. How would I go about doing this?
1
by: hrishy | last post by:
Hi I am validating a xmlfile against a xsd (My.xsd) but i notice that the xsd has a include which includes another xsd (My1.xsd) I have written a simple program that to validate this from...
2
by: jasiu85 | last post by:
Hey, I have a problem with character encoding in LXML. Here's how it goes: I read an HTML document from a third-party site. It is supposed to be in UTF-8, but unfortunately from time to time...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.