Home >
Online Product Documentation >
Table of Contents >
Using XPath Expressions in Stylesheets
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:
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
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
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
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:
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:
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
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.
For example, the
videos.xml document, which is in the
examples directory of the Stylus Studio installation directory, contains the following elements:
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:
This indexes all actors by their ID. To process a video, your stylesheet specifies the following:
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:
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.