Distinguish between empty string and no children, in XPath 2?

Discussion in 'XML' started by Arndt Jonasson, Sep 5, 2008.

  1. Let's say we have a schema (maybe expressed in XML Schema, but not
    necessarily so), that allows this instance document:

    <top>
    <txt>This is text</txt>
    <books>
    <book>Tarzan</book>
    <book>Harry Potter</book>
    </books>
    </top>

    The text /top/txt may be empty, and the element /top/books may have no
    children, so this instance document is also allowed:

    <top>
    <txt/>
    <books/>
    </top>

    I now want to write an XPath expression that selects all nodes that do
    not have children in the schema. It would always select /top/txt and
    it would never select /top/books, even in the second example above.

    With XPath 1.0, this is not possible, since schema information is not
    used there. But can it be done in XPath 2.0? I find the standard
    document a bit forbidding, although I'm fairly well acquainted with
    the 1.0 document.

    We would like to add such capability to the XPath 1.0 implementation
    in our application (which does have access to the schema), and if
    XPath 2.0 offers a way to express it, it seems best not to reinvent
    anything, hence this question.
     
    Arndt Jonasson, Sep 5, 2008
    #1
    1. Advertising

  2. Arndt Jonasson wrote:
    > Let's say we have a schema (maybe expressed in XML Schema, but not
    > necessarily so), that allows this instance document:
    >
    > <top>
    > <txt>This is text</txt>
    > <books>
    > <book>Tarzan</book>
    > <book>Harry Potter</book>
    > </books>
    > </top>
    >
    > The text /top/txt may be empty, and the element /top/books may have no
    > children, so this instance document is also allowed:
    >
    > <top>
    > <txt/>
    > <books/>
    > </top>
    >
    > I now want to write an XPath expression that selects all nodes that do
    > not have children in the schema. It would always select /top/txt and
    > it would never select /top/books, even in the second example above.
    >
    > With XPath 1.0, this is not possible, since schema information is not
    > used there. But can it be done in XPath 2.0? I find the standard
    > document a bit forbidding, although I'm fairly well acquainted with
    > the 1.0 document.


    Even in the XSLT 2.0 data model the txt element has a child node, it is
    a text child node. So your description of saying does not have "children
    in the schema" is not very precise. Are you looking for elements which
    have a simple type in the meaning of the W3C schema language, meaning
    they have no child _elements_ and no attributes? I am not sure whether
    schema aware XSLT 2.0 allows you do detect elements which have a simple
    type respectively do distinguish in your stylesheet between elements
    having a simple type and those having a complex type. As far as I know
    all you can do is match an element based on its type and validate input
    or output elements based on a schema.

    You might want to ask on the XSL mailing list
    http://www.mulberrytech.com/xsl/xsl-list/, spec writers and implementors
    like Michael Kay are regulars there so you should get a more qualified
    answer there than here.



    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Sep 5, 2008
    #2
    1. Advertising

  3. Arndt Jonasson wrote:
    > Let's say we have a schema (maybe expressed in XML Schema, but not
    > necessarily so), that allows this instance document:
    >
    > <top>
    > <txt>This is text</txt>
    > <books>
    > <book>Tarzan</book>
    > <book>Harry Potter</book>
    > </books>
    > </top>
    >
    > The text /top/txt may be empty, and the element /top/books may have no
    > children, so this instance document is also allowed:
    >
    > <top>
    > <txt/>
    > <books/>
    > </top>
    >
    > I now want to write an XPath expression that selects all nodes that do
    > not have children in the schema. It would always select /top/txt and
    > it would never select /top/books, even in the second example above.
    >
    > With XPath 1.0, this is not possible, since schema information is not
    > used there. But can it be done in XPath 2.0? I find the standard
    > document a bit forbidding, although I'm fairly well acquainted with
    > the 1.0 document.


    Even in the XSLT 2.0 data model the txt element has a child node, it is
    a text child node. So your description of saying does not have "children
    in the schema" is not very precise. Are you looking for elements which
    have a simple type in the meaning of the W3C schema language, meaning
    they have no child _elements_ and no attributes? I am not sure whether
    schema aware XSLT 2.0 allows you do detect elements which have a simple
    type respectively do distinguish in your stylesheet between elements
    having a simple type and those having a complex type. As far as I know
    all you can do is match an element based on its type and validate input
    or output elements based on a schema.

    You might want to ask on the XSL mailing list
    http://www.mulberrytech.com/xsl/xsl-list/, spec writers and implementors
    like Michael Kay are regulars there so you should get a more qualified
    answer there than here.



    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Sep 5, 2008
    #3
  4. On Sep 5, 2:00 pm, Martin Honnen <> wrote:

    > Even in the XSLT 2.0 data model the txt element has a child node, it is
    > a text child node. So your description of saying does not have "children
    > in the schema" is not very precise. Are you looking for elements which
    > have a simple type in the meaning of the W3C schema language, meaning
    > they have no child _elements_ and no attributes?


    Yes, I mistakenly left out the word "element".

    > You might want to ask on the XSL mailing listhttp://www.mulberrytech.com/xsl/xsl-list/, spec writers and implementors
    > like Michael Kay are regulars there so you should get a more qualified
    > answer there than here.


    Thanks, I'll try there.
     
    Arndt Jonasson, Sep 5, 2008
    #4
  5. On Sep 5, 2:00 pm, Martin Honnen <> wrote:

    > Even in the XSLT 2.0 data model the txt element has a child node, it is
    > a text child node. So your description of saying does not have "children
    > in the schema" is not very precise. Are you looking for elements which
    > have a simple type in the meaning of the W3C schema language, meaning
    > they have no child _elements_ and no attributes?


    Yes, I mistakenly left out the word "element".

    > You might want to ask on the XSL mailing listhttp://www.mulberrytech.com/xsl/xsl-list/, spec writers and implementors
    > like Michael Kay are regulars there so you should get a more qualified
    > answer there than here.


    Thanks, I'll try there.
     
    Arndt Jonasson, Sep 5, 2008
    #5
  6. "Arndt Jonasson" <> wrote in message
    news:...
    >
    > Let's say we have a schema (maybe expressed in XML Schema, but not
    > necessarily so), that allows this instance document:
    >
    > <top>
    > <txt>This is text</txt>
    > <books>
    > <book>Tarzan</book>
    > <book>Harry Potter</book>
    > </books>
    > </top>
    >
    > The text /top/txt may be empty, and the element /top/books may have no
    > children, so this instance document is also allowed:
    >
    > <top>
    > <txt/>
    > <books/>
    > </top>
    >
    > I now want to write an XPath expression that selects all nodes that do
    > not have children in the schema. It would always select /top/txt and
    > it would never select /top/books, even in the second example above.
    >


    This is possible in XPath 2.0 if the schema has separate types for all cases
    of element that must not have children-elements.

    Then one can use the so called ElementTest, which is defined in the
    following way:

    ElementTest ::= "element" "(" (ElementNameOrWildcard (","
    TypeName "?"?)?)? ")"


    One of the possible XPath 2.0 expressions will be something like the
    following:

    //element(*,Type1) | //element(*,Type2) | ... |
    //element(*,TypeN)


    where Type1, Type2, ..., typeN are all the schema types that define
    elements that cannot have children-elements.

    Probably substitution groups can be used so that all types above can be
    derived from a single abstract type, let's say "ChildlessElement".

    Then the expression would be simply:

    //element(*,ChildlessElement)


    Of course, to be able to evaluate such XPath 2.0 expressions one must have a
    full-blown XPath 2.0 implementation (either a Schema-Aware XSLT 2.0
    processor, or an XQuery processor)

    For more information see the XPath 2.0 spec:

    http://www.w3.org/TR/xpath20/#doc-xpath-ElementTest

    http://www.w3.org/TR/xpath20/#id-element-test


    Cheers,
    Dimitre Novatchev
     
    Dimitre Novatchev, Sep 7, 2008
    #6
  7. "Arndt Jonasson" <> wrote in message
    news:...
    >
    > Let's say we have a schema (maybe expressed in XML Schema, but not
    > necessarily so), that allows this instance document:
    >
    > <top>
    > <txt>This is text</txt>
    > <books>
    > <book>Tarzan</book>
    > <book>Harry Potter</book>
    > </books>
    > </top>
    >
    > The text /top/txt may be empty, and the element /top/books may have no
    > children, so this instance document is also allowed:
    >
    > <top>
    > <txt/>
    > <books/>
    > </top>
    >
    > I now want to write an XPath expression that selects all nodes that do
    > not have children in the schema. It would always select /top/txt and
    > it would never select /top/books, even in the second example above.
    >


    This is possible in XPath 2.0 if the schema has separate types for all cases
    of element that must not have children-elements.

    Then one can use the so called ElementTest, which is defined in the
    following way:

    ElementTest ::= "element" "(" (ElementNameOrWildcard (","
    TypeName "?"?)?)? ")"


    One of the possible XPath 2.0 expressions will be something like the
    following:

    //element(*,Type1) | //element(*,Type2) | ... |
    //element(*,TypeN)


    where Type1, Type2, ..., typeN are all the schema types that define
    elements that cannot have children-elements.

    Probably substitution groups can be used so that all types above can be
    derived from a single abstract type, let's say "ChildlessElement".

    Then the expression would be simply:

    //element(*,ChildlessElement)


    Of course, to be able to evaluate such XPath 2.0 expressions one must have a
    full-blown XPath 2.0 implementation (either a Schema-Aware XSLT 2.0
    processor, or an XQuery processor)

    For more information see the XPath 2.0 spec:

    http://www.w3.org/TR/xpath20/#doc-xpath-ElementTest

    http://www.w3.org/TR/xpath20/#id-element-test


    Cheers,
    Dimitre Novatchev
     
    Dimitre Novatchev, Sep 7, 2008
    #7
    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. mosfet
    Replies:
    6
    Views:
    1,368
    Rolf Magnus
    Nov 27, 2003
  2. Vinodh Kumar P
    Replies:
    1
    Views:
    352
    Jack Klein
    Dec 12, 2003
  3. Andre
    Replies:
    7
    Views:
    484
    Dan Pop
    Jul 21, 2003
  4. Tzury Bar Yochay
    Replies:
    1
    Views:
    416
    Gabriel Genellina
    Mar 24, 2008
  5. Ramon F Herrera
    Replies:
    13
    Views:
    1,839
    Manuel Collado
    Jun 28, 2012
Loading...

Share This Page