Skip to main content

If you don't have an IBM ID and password, register here.

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. This profile includes the first name, last name, and display name you identified when you registered with developerWorks. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

XML for Data: What's new in XPath 2.0?

An early look at some of the planned features for XPath 2.0

Kevin Williams (kevin@blueoxide.com), CTO, Blue Oxide Technologies, LLC
Kevin Williams is the CTO of Blue Oxide Technologies, LLC, a company that designs XML and XML Web service design software. Visit their Web site at http://www.blueoxide.com. He can be reached for comment at kevin@blueoxide.com.

Summary:  Kevin Williams takes a look at the latest status of the XPath 2.0 specifications and provides some specific examples of XPath 2.0 features that will make the XML developer's life easier. Examples are provided in XML and XPath.

View more content in this series

Date:  01 Sep 2002
Level:  Intermediate

Comments:  

XSLT 1.0 and XPath 1.0 were originally intended to provide simple style language support for XML documents, primarily to convert those documents into HTML for rendering to a browser. Since XSLT and XPath became available, however, they have been pressed into all sorts of tasks for which they weren't originally designed -- from the sophisticated manipulation of data in XML documents (aggregation, distinct selection, relationship pivoting) to XSLT's transformation of one XML form into another. In version 2.0 of these specifications, the W3C attempts to make XSLT and XPath much more flexible and robust in order to handle the new way these technologies are being used.

In the last column, I looked at some of the new features of XSLT. In this column, I will take a look at just a few highlights of XPath 2.0 -- there are far too many to mention in a single column.

For the purposes of this column, certain prefixes map to the following:

  • The xf: prefix is presumed to map to the XPath 2.0 functions namespace (http://www.w3.org/2002/08/xquery-functions).
  • The xsl: prefix maps to the XSLT 2.0 namespace.
  • The xs: prefix maps to the XML Schema namespace.

The xf:distinct-values function

One of the most significant challenges facing developers when working with XSLT 1.0 style sheets is to write the XML equivalent of a SELECT DISTINCT in SQL -- that is, an expression that takes a node set and returns a list of unique values from those nodes. This was not impossible in XSLT 1.0 and XPath 1.0, but fiendishly difficult. Basically, you had to write an xsl:for-each statement to evaluate each node in a particular sorted order, and then keep peeking back at the node list to see if any others matched the particular one being processed. With XSLT 2.0 and the introduction of the xf:distinct-values function, this problem goes away. Here's a quick example of the XML document in Listing 1:


Listing 1. Books with author names
<books>
  <book author="Kevin Williams" title="Professional XML 2.0" />
  <book author="Lewis Carroll" title="Alice in Wonderland" />
  <book author="Lewis Carroll" title="Through the Looking-Glass" />
</books>

Suppose you want to normalize out the author information and create a document that looks like Listing 2:


Listing 2. Authors with book titles
<authors>
  <author name="Kevin Williams">
    <book title="Professional XML 2.0" />
  </author>
  <author name="Lewis Carroll">
    <book title="Alice in Wonderland" />
    <book title="Through the Looking-Glass" />
  </author>
</authors>

To accomplish this, you need to do something like Listing 3 in XPath and XSLT 1.0:


Listing 3. SELECT DISTINCT in XPath 1.0
<xsl:template match="books">
  <authors>
    <xsl:for-each select="book">
      <xsl:sort select="@author" />
      <xsl:variable name="thisAuthor">
        <xsl:value-of select="@author" />
      </xsl:variable>
      <xsl:if test="count(preceding-sibling::book)=0">
        <author name="{$thisAuthor}">
          <xsl:for-each select="../book[@author=$thisAuthor]">
            <book title="{@title}" />
          </xsl:for-each>
        </author>
      </xsl:if>
      <xsl:if test="preceding-sibling::book[1]/@author != $thisAuthor">
        <author name="{$thisAuthor}">
          <xsl:for-each select="../book[@author=$thisAuthor]">
            <book title="{@title}" />
          </xsl:for-each>
        </author>
      </xsl:if>
      </xsl:for-each>
    </authors>
</xsl:template>

A bit messy and difficult to document, however using xf:distinct, this becomes easy, as shown in Listing 4:


Listing 4. xf:distinct in XPath 2.0
<xsl:template match="books">
  <authors>
    <xsl:for-each select="xf:distinct(book/@author)">
      <xsl:variable name="thisAuthor"><xsl:value-of select=".">
      <author name="{$thisAuthor}">
        <xsl:for-each select="book[@author=$thisAuthor]">
          <book title="{@title}" />
        </xsl:for-each>
      </author>
    </xsl:for-each>
  </authors>
</xsl:template>

How's this different from xsl:for-each-group? xf:distinct is available anywhere XPath is implemented -- so it is available as part of XQuery 1.0, as well as specialized XPath processors. With xf:distinct, you can perform other processing on the value set, as opposed to xsl:for-each-group, which forces the distinct values to be operated on individually.

This one change addresses many of the problems XSLT authors encounter when attempting to style documents. There are many other changes to XSLT that style sheet programmers will find useful.


The xf:document function

Working with multiple documents can be problematic in XSLT 1.0. The designers of XPath 2.0 wisely chose to include additional document processing capability through the xf:document mechanism. This function allows one or more documents to be loaded from URLs and processed. For example, suppose you have the type of document shown in Listings 5, 6, and 7 in a repository:


Listing 5. Sample part document 1
<part name="Grommets" size="3 in." color="blue" />


Listing 6. Sample part document 2
<part name="Grommets" size="3 in." color="blue" />


Listing 7. Sample part document 3
<part name="Grommets" size="3 in." color="blue" />

Suppose you now have 500 of these documents in a single directory, and you want to create a list of all the blue parts you carry. Rather than writing code in a middle-tier language like Java to load each of these documents and look for parts with the color blue, you can write an index (see Listing 8) that lists where all the parts may be found:


Listing 8. Part index document
<parts>
  <part file="part1.doc" />
  <part file="part2.doc" />
  <part file="part3.doc" />
</parts>

You can write a single style sheet that does the job for you with a fragment like the one in Listing 9:


Listing 9. xf:document used to cross documents
<xsl:template match="parts">
  <blueParts>
    <xsl:for-each select="xf:document(part/@file)/@color='blue'">
      <part name={@name} size={@size} color={@color} />
    </xsl:for-each>
  </blueParts>
</xsl:template>

This technique lends itself particularly well to distributed networks of content, where the part information can be housed on one server and the customer information on another. Using style sheets that use xf:document to pull information from other URLs allows this data to live where it was created.


The xf:current-dateTime function

When a style sheet is applied to an XML document, it is often helpful to include the creation date of the transformed result in the output. This is particularly important when creating HTML documents to drive a user interface; such information can help caching systems know when a copy of the information is stale. You can obtain the current date and time using the xf:current-dateTime function in XPath 2.0 (see Listing 10):


Listing 10. xf:current-dateTime example
<xsl:value-of select="xf:current-dateTime()" />

This might return the string shown in Listing 11:


Listing 11. xf:current-dateTime sample result
2002-09-17T18:22:08z

You can then use this string as-is or transform it into a different date format for use in the resulting document.


Better XML Schema compatibility

Since XPath 2.0 is now being shared between XSLT 2.0 and XQuery 1.0, it follows that XPath would need more robust support for XML Schema. Indeed, the entire data model in XPath 2.0 is now strongly typed: Rather than having simple string, number, and boolean types, values now use the primitives defined as part of the XML Schema specification. A full set of functions is provided so you can explicitly convert these values from one type to another as part of their manipulation in XPath 2.0. For example, a value can be cast to another type by using the type name as if it were a function. So you could cast a value to an unsigned integer using the code fragment in Listing 12:


Listing 12. XML Schema type cast example
xs:unsignedInt(item)

The strong typing in XPath 2.0 ensures that a document created by an XSLT style sheet can be validated against a strongly-typed XML schema. Before XSLT 2.0, there was no way to guarantee, for example, that a number was an unsigned integer -- the XML Schema validation step was necessary to ensure that a faulty value was not provided by the style sheet.

In this document, I've just touched on the highlights of what XPath 2.0 has to offer. While this technology is still a good ways from being promoted to recommendation status (at least six months), familiarizing yourself with XPath 2.0's features and functionality will prepare you to take full advantage of it when implementations start to emerge.


Resources

About the author

Kevin Williams is the CTO of Blue Oxide Technologies, LLC, a company that designs XML and XML Web service design software. Visit their Web site at http://www.blueoxide.com. He can be reached for comment at kevin@blueoxide.com.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in

If you don't have an IBM ID and password, register here.


Forgot your IBM ID?


Forgot your password?
Change your password


By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. This profile includes the first name, last name, and display name you identified when you registered with developerWorks. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)


By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=XML
ArticleID=12164
ArticleTitle=XML for Data: What's new in XPath 2.0?
publish-date=09012002
author1-email=kevin@blueoxide.com
author1-email-cc=

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).