Using XPath Expressions in Stylesheets

This section provides information about using XPath expressions in stylesheets. It includes the following topics:

Using Variables

In a query that you specify in a stylesheet, you can refer to variables that you defined elsewhere in the stylesheet. Use the following format to refer to a variable:

$variable_name
               

            

In a stylesheet, you can define variables with either of the following instructions:

Obtaining System Properties

In a query in a stylesheet, there are three system properties for which you can obtain information:

  • xsl:version returns 1.0 as the version of XSLT that the Stylus Studio XSLT processor implements.
  • xsl:vendor returns Sonic Software as the vendor of Stylus Studio's XSLT processor.
  • xsl:vendor-url returns http://www.stylusstudio.com as the vendor URL.

To obtain this information, call the system-property() function. The format is

object system-property(string)
               

            

The string you specify must identify one of the three properties and must be a qualified name. This function returns an object that represents the value of the system property you specify.

Determining If Functions Are Available

In a query in a stylesheet, to determine whether the XPath processor supports a particular function, call the function-available() function. The format is

boolean function-available(string)
               

            

Specify a string that identifies the name of the function. The XPath processor returns true if it implements that function.

Obtaining the Current Node for the Current XSLT Template

In a stylesheet, the current node is the node for which the XSLT processor instantiates a template. When the XPath processor evaluates an expression during stylesheet processing, the initial context node for the expression is set to the current node for the stylesheet instruction that contains the expression. Because the context node can change during evaluation of subexpressions, it is useful to be able to retrieve, from within a subexpression, the original context node for which the expression is being evaluated. You can use the current() function for this purpose. The format is

node-set current() 
               

            

The current() function returns a node set that contains only the current node for the current template. The current() function is specified in the W3C XSLT Recommendation.

For example, the following stylesheet causes the XSLT processor to pass the bookstore node to the outer xsl:for-each instruction:

<xsl:stylesheet 
               

              
xmlns:xsl="http://www.w3.org/XSL/Transform" version="1.0" >
                   
<xsl:template match="/"> 
                   
<xsl:for-each select="bookstore"> 
                   
<xsl:for-each select=
                   
"book[@style=current()/@specialty]"> 
                   
... 
                   
</xsl:for-each> 
                   
</xsl:for-each> 
                   
</xsl:template> 
                   

                
</xsl:stylesheet>

The bookstore node is the current node within the outer xsl:for-each instruction. Within the inner xsl:for-each instruction, a book node is the current node.

The current() function in the inner expression returns the bookstore element because the bookstore element is the current node for the inner xsl:for-each instruction. The result of the query contains book elements if the value of their style attribute is the same as the value of the specialty attribute of the bookstore element ( novel).

Suppose the select attribute in the inner xsl:for-each instruction specified the dot ( .) instead of the current() function:

<xsl:for-each select="book[@style=./@specialty]">
               

            

In a query, the dot specifies the context node. This query would return a book if the value of its style attribute was the same as the value of its specialty attribute.

You can nest xsl:for-each instructions more than one level deep. In any given nested xsl:for-each instruction, the current() function returns the current node for the closest enclosing xsl:for-each instruction.

Finding an Element with a Particular Key

The key() function, defined in the XSLT Recommendation, obtains the node whose key value matches the specified key. The format is

node-set key(string, object)
               

            

The first argument specifies the name of the key. The value of this argument must be a qualified name. The second argument specifies the node or nodes to examine. When the second argument is a node set, the result is the union of the results of applying the key() function to the string value of each of the nodes in the set. When the second argument is any other type, the XPath processor converts the argument to a string, as if by a call to the string() function. The key() function returns a node set that contains the nodes in the same document as the context node that have a value for the named key that is equal to this string.

Example

For example, the videos.xml document, which is in the examples directory of the Stylus Studio installation directory, contains the following elements:

<result> 
               

              
<actors> 
                   
<actor id="00000003">Jones, Tommy Lee</actor> 
                   
... 
                   
</actors> 
                   
<videos> 
                   
<video id="id1235AA0"> 
                   
<title>The Fugitive</title> 
                   
... 
                   
<actorRef>00000003</actorRef> 
                   
<actorRef>00000006</actorRef> 
                   
... 
                   
</video> 
                   
... 
                   
</videos> 
                   

                
</result>

When you display information about a video in a Web browser, you want to display the names of the actors. Because the actors are referenced only by an ID number, you create a key table in your stylesheet:

<xsl:key 
               

              
name="actors" 
                   
match="/result/actors/actor" 
                   
use="@id"/>
                   

                

This indexes all actors by their ID. To process a video, your stylesheet specifies the following:

<xsl:for-each select="actorRef"> 
               

              
<xsl:value-of select="key('actors', .)"/> 
                   

                
</xsl:for-each>

This instructs the XPath processor to look up the actor element in the actors key table by using the actorRef element as a key.

Generating Temporary IDs for Nodes

The generate-id() function, defined in the XSLT recommendation, generates temporary IDs for nodes.

Caution

 

The ID generated by the generate-id() function is not an object ID. The value generated by the generate-id() function is guaranteed to be the same only during an XSL transformation. If the source document changes, the value for this ID can change.

Format

The format for the generate-id() function is as follows:

string generate-id(node-set?)
               

            

The generate-id() function returns a string that uniquely identifies the node in node-set that is first in document order. This string starts with xln and ends with eight hexadecimal digits. Syntactically, the string is an XML name.

If the node-set argument is empty, the generate-id() function returns an empty string. If you omit the node-set argument, the generate-id() function generates a temporary ID for the context node.

 
Free Stylus Studio XML Training: