xsl-list
[Top] [All Lists]

Re: [xsl] Distinguish between empty string and no children, in XPath 2?

2008-09-05 08:49:26
At 2008-09-05 14:33 +0200, Arndt Jonasson wrote:
I hope this is the right forum for this question. I asked it first
in the new group comp.text.xml, but was directed here. It has nothing
directly to do with XSLT, only with XPath.

XPath is in scope for this list.

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 child elements 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 think not in a generic fashion ... in XPath 2.0 you can test elements of particular named types (and the names of the types those types are based on) as not having any child elements, but you cannot test aspects of an element's type anonymously such as "any type that allows only element content".

Unless, perhaps, you had the flexibility in your schema to define a base type with your own name that is a very generic type that allows only element children of any name, and then specialize all of your element-content element types on that type restricting each one to the child elements for that particular type. Then in your XPath you can test elements as being an element of your named generic type and it would be checking that the element is one that has element content.

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.

Sharing your approach in the archive will probably be very interesting to readers of the list.

I hope this helps.

. . . . . . . . . . . . . . Ken

--
Upcoming XSLT/XSL-FO hands-on courses:      Wellington, NZ 2009-01
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
G. Ken Holman                 mailto:gkholman(_at_)CraneSoftwrights(_dot_)com
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--