Understanding the XPath Data Model, Page 3
There are some types built into XPath 2.0 that have already been derived by restriction from the XML schema xs:duration type. These types are in the namespace http://www.w3.org/2003/05/xpath-datatypes, which is represented by the prefix xdt:
xdt:dayTimeDuration is a subtype of xs:duration, which contains only day, hour, minute, and second components. In XPath 2.0, if you subtract two date values, the result is of the xdt:dayTimeDuration type.
xdt:yearMonthDuration is a subtype of xs:duration, which is restricted to only year and month components.
In addition, there are three abstract types, xdt:anyAtomicType, xdt:untypedAtomic, and xdt:untypedAny, which are now built into XPath 2.0. Because they're abstract, you can't create variables of these types directly, but you can use them in certain places
xdt:anyAtomicType is an abstract type that is the base type of all atomic values. All atomic types, such as xs:integer, xs:string, and xdt:untypedAtomic, are subtypes of xdt:anyAtomicType.
xdt:untypedAtomic is an atomic type used for untyped data, such as text that is not given a specific type by schema validation. xdt:untypedAtomic is the type used to annotate unvalidated attribute nodes, for example, attribute nodes in well-formed documents. xdt:untypedAny is the type used to annotate unvalidated element nodes, for example, elements in well-formed documents.
xdt:untypedAny is a type that annotates an element whose type is unknown (such as might occur in a schemaless document).
As in XPath 1.0, you often don't deal with the various data types directly. Instead, you might use functions or expressions that return items of the various data types. For example, say that you wanted to use the XPath 2.0 current-dateTime function, which returns a value of the xs:dateTime type. Here's how we might assign an XSLT variable named rightNow the xs:dateTime value returned by current-dateTime:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:variable name="rightNow" select="current-dateTime()" /> . . . </xsl:stylesheet>
Now we can use this new variable as a valid XPath 2.0 expression, because its type, xs:dateTime, is valid in XPath 2.0. Here's how that might work in a style sheet that just displays the current date and time (replacing the document node of whatever document you use it with that data) :
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:variable name="rightNow" select="current-dateTime()" /> <xsl:template match="/"> The date and time is: <xsl:value-of select="$rightNow"/> </xsl:template> </xsl:stylesheet>
And here's what you get when you use the style sheet with Saxonas you can see, our xs:dateTime variable was indeed supported:
<?xml version="1.0" encoding="UTF-8"?> The date and time is: 2003-08-29T19:38:01.787Z
However, sometimes you do want to work with the supported data types explicitly. Here's an example, where we're using the xs:date constructor to create an xs:date value:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xsl:template match="/"> <xsl:value-of select="xs:date('2004-09-02')"/> </xsl:template> </xsl:stylesheet>
Types Derived by List or Union - What about types you may derive in XML schema that are not restricted, such as types derived by list or union? Items of these types are converted into sequences in XPath 2.0it's easy to see how list types are converted into sequences, but union types are more troublesome. When you derive a type from the union of other types, that union is converted into a simple sequence of the types in the union, one after the other. The actual type defined by union is not preserved, although its components are. Only the type of each individual item in the union is kept in this case.
Now that we've created our xs:date value, Saxon is able to display its value to us this way:
<?xml version="1.0" encoding="UTF-8"?> 2004-09-02
XPath 2.0 builds on top of XPath 1.0 and adds much more power, including expressions like if, for, some, and others.
Sequences are new in XPath 2.0, and they are ordered collections of zero or more items. And an item is either an atomic value or a node.
XPath 1.0 supported only four data types, XPath 2.0 extends that to support not only the node kinds supported in XPath 1.0, but also many atomic types, which include the primitive simple types defined by the XML Schema specification and values whose types are derived from them by restriction in a schema.
About the Author
Steven Holzner is an award-winning author who has been writing about XML topics such as XSLT as long as they've been around. He's the author of XPath Kick Start : Navigating XML with XPath 1.0 and 2.0 (published by Sams Publishing), and has written 67 books, all on programming topics, selling well over a million copies. His books have been translated into 16 languages around the world and include a good number of industry bestsellers. He's a former contributing editor of PC Magazine, graduated from MIT, and received his Ph.D. at Cornell. He's been on the faculty of both MIT and Cornell, and also teaches corporate seminars around the country.
Page 3 of 3