Using Accessor Functions

The XPath 2.0 Data Model describes a set of accessor functions for use with different types of nodes, and some of those accessor functions are made available through general functions. Here they are:

  • fn:node- name returns the name of a node.

  • fn:string returns the string value of the argument.

  • fn:data takes a sequence of items and returns a sequence of atomic values.

  • fn: base-uri returns a node's base URI.

  • fn: document-uri returns the document's URI.

We'll take a closer look at these functions here.

The fn:node-name Function

The fn:node-name function returns the expanded name of nodes that can have names (for other node kinds, it returns the empty sequence). Here is the signature for this functionthe question mark at the end of the return type indicates in XPath 2.0 that the empty sequence can be returned:

 
 fn:node-name(  $srcval  as node) as xs:QName? 

Expanded names consist of a pair of values, a namespace URI, and a local name. In consequence, the way this function actually returns its values is implementation-specific.

The fn:string Function

The fn:string function returns the string value of a node. Here's how you use this functionthis function can take one argument or an empty sequence, which XPath represents by adding a question mark after the argument type:

 
 fn:string(  $srcval  as  item?  ) as xs:string 

Here, fn:string returns the value of $srcval represented as a xs:string . If you don't pass an argument to this function, $srcval defaults to the context item (which is represented by a dot, .). If you pass an empty sequence, you'll get an empty string, "", back.

String representations in XPath 2.0 are nearly the same as in XPath 1.0, with some small differencesfor example, the representations of positive and negative infinity are now 'INF' and '-INF' rather than 'Infinity' and '-Infinity' . You can see an example in ch09_01.xsl , Listing 9.1in this case, we're displaying the string value of the <planet> elements in our planetary data document.

Listing 9.1 An XSLT Example Using the XPath Function fn:string ( ch09_01.xsl )
[View full width]
 <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns graphics/ccc.gif :xs="http://www.w3.org/2001/XMLSchema">  <xsl:template match="//planet">   <xsl:value-of select="string(.)"/>   </xsl:template>  </xsl:stylesheet> 

You can see the results when you apply this stylesheet to our planetary data document, including the string value of each <planet> element:

 
 C:\Saxon>java net.sf.saxon.Transform ch02_01.xml ch09_02.xsl <?xml version="1.0" encoding="UTF-8"?>         Mercury         .0553         58.65         1516         .983         43.4         Venus         .815         116.75         3716         .943         66.8         Earth         1         1         2107         1         128.4 

This function is handy for turning data of various types into strings. Because of XPath 2.0's reliance on strong data typing, this is an important functionfor example, if you want to pass a non-string variable's value to a string function like fn:concat , you should convert that value to a string using the fn:string function first.

The fn:data Function

The fn:data function converts a sequence of items (which can include nodes and atomic values) into a sequence of atomic values. Here's how you use this function in generalthe * symbol here means "zero or more of," which is how you represent a sequence in function signatures:

 
 fn:data(  $srcval  as  item  *) as xdt:anyAtomicType* 

If an item in the passed sequence is already an atomic value, it's returned in the returned sequence. On the other hand, if an item in the passed sequence is a node, its typed value is returned (see http://www.w3.org/TR/xpath-datamodel/#dm-typed-value for a description of exactly how typed values for nodes are calculated). If the node has not been validated , its typed value is simply its string value, given the type xdt:untypedAtomic .

The fn:base-uri Function

This accessor function returns the base URI for a node. Here's how you use this function:

 
 fn:base-uri(  $srcval  as node) as xs:string? 

In XML documents, you set a base URI with the XML xml:base attribute. You can see an example in ch09_03.xml , where we're setting a base URI for our planetary data document in a new version, ch09_02.xml (Listing 9.2).

Listing 9.2 Setting a Base URI ( ch09_02.xml )
 <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="ch01_02.xsl"?>  <planets xml:base="http://www.XPathCorp.com">  <planet>         <name>Mercury</name>         <mass units="(Earth = 1)">.0553</mass>         <day units="days">58.65</day>         <radius units="miles">1516</radius>         <density units="(Earth = 1)">.983</density>         <distance units="million miles">43.4</distance>         <!--At perihelion-->     </planet>     <planet>         <name>Venus</name>         <mass units="(Earth = 1)">.815</mass>         <day units="days">116.75</day>         <radius units="miles">3716</radius>         <density units="(Earth = 1)">.943</density>         <distance units="million miles">66.8</distance>         <!--At perihelion-->     </planet>     <planet>         <name>Earth</name>         <mass units="(Earth = 1)">1</mass>         <day units="days">1</day>         <radius units="miles">2107</radius>         <density units="(Earth = 1)">1</density>         <distance units="million miles">128.4</distance>         <!--At perihelion-->     </planet> </planets> 

Now we can determine the base URI of each <planet> element in a new XSLT 2.0 stylesheet, as you see in ch09_03.xsl (Listing 9.3).

Listing 9.3 Reading a Base URI ( ch09_03.xsl )
[View full width]
 <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns graphics/ccc.gif :xs="http://www.w3.org/2001/XMLSchema">  <xsl:template match="//planet">   <xsl:value-of select="base-uri(.)"/>   </xsl:template>  </xsl:stylesheet> 

And here's the result when you apply this stylesheet to our new XML documentyou can see the base URI of the <planet> elements in this example:

 
 C:\Saxon>java net.sf.saxon.Transform ch09_03.xml ch09_04.xsl <?xml version="1.0" encoding="UTF-8"?>     http://www.XPathCorp.com     http://www.XPathCorp.com     http://www.XPathCorp.com 

The fn:document-uri Function

When you pass it a node, the fn:document-uri function returns the URI of the document that contains the node, which is useful, for example, if you want to display an error that lists the current document's name. Here's how you use this function:

 
 fn:document-uri(  $srcval  as node) as xs:string? 

You can see an example putting fn:document-uri to work in ch09_04.xsl (Listing 9.4). Here, we're simply displaying the URI of the current document to test this function.

Listing 9.4 Using the fn:document-uri Function ( ch09_04.xsl )
[View full width]
 <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns graphics/ccc.gif :xs="http://www.w3.org/2001/XMLSchema">  <xsl:template match="/">   <xsl:value-of select="document-uri(.)"/>   </xsl:template>  </xsl:stylesheet> 

And here's the result you get when apply this stylesheet to ch09_02.xml in this case the result is the location on disk of ch09_02.xml :

 
 C:\Saxon>java net.sf.saxon.Transform ch09_03.xml ch09_04.xsl <?xml version="1.0" encoding="UTF-8"?> file:/C:/Saxon/ch09_02.xml 


XPath. Navigating XML with XPath 1.0 and 2.0 Kick Start
XPath Kick Start: Navigating XML with XPath 1.0 and 2.0
ISBN: 0672324113
EAN: 2147483647
Year: 2002
Pages: 131

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net