current


The current() function returns a single item, the item that is the context item at the point in the stylesheet where the XPath expression containing this function call is called.

Changes in 2.0

The function has been generalized so it may return any item (a node or an atomic value), not only a node.

The function may now be used within an XSLT pattern.

Signature

There are no arguments.

 

Data type

Meaning

Result

item()

The item that is the context item at the outermost level of the XPath expression containing the function call.

Effect

At any point in the processing of a stylesheet, there is generally a context item. The XPath expression «. » , if it is used as a stand-alone expression within a stylesheet, will always select the context item and will return the same result as the expression current() . Within an XPath expression, the context item may change: for example, within a predicate, the context item is the item being tested using the predicate. The result of the current() function, however, is the same wherever it is used within an expression. This makes it useful within predicates, as a way of referring to the item that would have been the context item at the outermost level.

This is best explained by example. The following example processes all <part> elements that have a code attribute whose value is the same as the code attribute of the element that is the context item at the point where the instruction is evaluated.

  <xsl:apply-templates select="//part[@code = current()/@code]"/>  

Another way of writing this would be:

  <xsl:apply-templates select="for $c in . return //part[@code = $c/@code]"/>  

and in fact this substitution is completely general; any complete XPath expression that references the current() function could be replaced by one in which a variable is bound to the value of «. » at the outermost level of the expression, and the call on current() is replaced by a reference to that variable.

The context item in a stylesheet is established as follows :

  • When evaluating a global variable, the context item is the initial context item, supplied by the calling application (it may be undefined if no initial context item has been supplied). Usually, this will be the document node of the main input document.

  • When <xsl:apply-templates> is used to process a selected set of nodes, each selected node in turn becomes the context item. So when a template rule is invoked, the context item is always the node that caused that template rule to be selected. On return from <xsl:apply-templates> , the context item reverts to its previous value.

  • When <xsl:for-each> is used to process a selected sequence of items, each selected item in turn becomes the context item. When the <xsl:for-each> loop completes, the context item reverts to its previous value.

  • When a stylesheet function is called from within an XPath expression, the context item is undefined. This means that any attempt to reference «. » or current() will raise an error.

  • The <xsl:for-each- group > and <xsl:analyze-string> instructions also change the context item: for details, see the description of these instructions in Chapter 5.

  • All other instructions, including <xsl:call-template> and <xsl: apply-imports > , leave the context item unchanged.

When the current() function is used in a pattern, it refers to the node that is being matched against the pattern. For example, the pattern «part[ ancestor ::*/@code!= current()/@code] » matches all part elements that have an ancestor with a code attribute that differs from the code attribute on the element itself.

Usage

The reason the current() function is provided is to allow you to determine the XSLT context item when it is different from the XPath context item- specifically , inside a predicate. The XPath context item can always be determined using the path expression «. » .

The most common situation where current() is useful is when you want to follow a cross-reference from the context node to some other node. For example, the expression «//department [deptNr = current() / @dept] » finds a <department> element referenced from the dept attribute of the context item (which might be an <employee> element).

Example

The following example shows the use of current() in a predicate.

current()
start example

This example lists the books in a catalog; in the description of each book, it also lists other books in the same category.

Source

The source document is booklist.xml .

  <booklist>   <book category="S">   <title>Number, the Language of Science</title>   <author>Danzig</author>   </book>   <book category="FC">   <title>The Young Visiters</title>   <author>Daisy Ashford</author>   </book>   <book category="FC">   <title>When We Were Very Young</title>   <author>A. A. Milne</author>   </book>   <book category="CS">   <title>Design Patterns</title>   <author>Erich Gamma</author>   <author>Richard Helm</author>   <author>Ralph Johnson</author>   <author>John Vlissides</author>   </book>   </booklist>  

Stylesheet

The stylesheet is list-books.xsl . It processes all the books in an <xsl:for-each> loop, and for each one it displays the title and the first author. Then it looks for other books in the same category. Here it uses the predicate «[./@category = current()/@category] » , which is true if the category attribute of the context element is the same as the category attribute of the current element. The context element is the one being tested; the element returned by current() is the one whose entry is being displayed. It also tests that these two elements are distinct elements, using the condition «not(.is current() ) » . In this case, you could also get away with writing «.!=current() » , which tests whether the two nodes have a different string value, but it can be a more expensive test, and it doesn't mean quite the same thing.

  <xsl:transform   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"   version="2.0"   >   <xsl:template match="/">   <html><body>   <xsl:variable name="all-books" select="//book"/>   <xsl:for-each select="$all-books">   <h1><xsl:value-of select="title" /></h1>   <p><i>by </i><xsl:value-of select="author[1]"/>   <xsl:if test="count(author)!=1"> and others</xsl:if>   </p>   <xsl:variable name="others"   select="$all-books[./@category=current()/@category and   not(. is current())]"/>   <xsl:if test="$others">   <p>Other books in this category:</p><ul>   <xsl:for-each select="$others">   <li><xsl:value-of select="title"/></li>   </xsl:for-each>   </ul>   </xsl:if>   </xsl:for-each>   </body></html>   </xsl:template>   </xsl:transform>  

Output

The output of the transformation is as follows.

  <html>   <body>   <h1>Number, the Language of Science</h1>   <p><i>by </i>Danzig   </p>   <h1>The Young Visiters</h1><p><i>by </i>Daisy Ashford   </p>   <p>Other books in this category:</p>   <ul>   <li>When We Were Very Young</li>   </ul>   <h1>When We Were Very Young</h1>   <p><i>by </i>A. A. Milne   </P>   <p>Other books in this category:</p>   <ul>   <li>The Young Visiters</li>   </ul>   <h1>Design Patterns</h1>   <p><i>by </i>Erich Gamma and others   </p>   </body>   </html>  
end example
 



XSLT 2.0 Programmer's Reference
NetBeansв„ў IDE Field Guide: Developing Desktop, Web, Enterprise, and Mobile Applications (2nd Edition)
ISBN: 764569090
EAN: 2147483647
Year: 2003
Pages: 324

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