lxml validation and xpath id function

Discussion in 'Python' started by Floris Bruynooghe, Jul 1, 2008.

  1. 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
     
    Floris Bruynooghe, Jul 1, 2008
    #1
    1. Advertising

  2. Stefan Behnel, Jul 2, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marvin_123456

    "Memory leak" in javax.xml.xpath.XPath

    Marvin_123456, Jul 29, 2005, in forum: Java
    Replies:
    4
    Views:
    2,028
    jan V
    Jul 29, 2005
  2. Alastair Cameron
    Replies:
    1
    Views:
    7,524
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  3. Anna
    Replies:
    0
    Views:
    572
  4. goog
    Replies:
    0
    Views:
    536
  5. hrishy

    lxml and schema validation

    hrishy, Oct 3, 2008, in forum: Python
    Replies:
    1
    Views:
    577
    Stefan Behnel
    Oct 3, 2008
Loading...

Share This Page