XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (539 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
9.93Mb size Format: txt, pdf, ePub

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

element referenced from the
dept
attribute of the context item (which might be an

element).

Example

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

Example: current()

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
.



     Number, the Language of Science

     Danzig



     The Young Visiters

     Daisy Ashford



     When We Were Very Young

     A. A. Milne



     Design Patterns

     Erich Gamma

     Richard Helm

     Ralph Johnson

     John Vlissides



Stylesheet

The stylesheet is
list-books.xsl
. It processes all the books in an

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.

   xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”

   version=“2.0”

   xmlns:book=“books.uri”

   exclude-result-prefixes=“book”

>


  

    

       

       

Category: 

                select=“$categories/category

                [@code=current()/@category]/@desc”/>

       


    

  



  

  

  



Output

The output of the transformation is as follows.


   

      Number, the Language of Science

      

Category: Science


      The Young Visiters

      

Category: Children's Fiction


      When We Were Very Young

      

Category: Children's Fiction


      Design Patterns

      

Category: Computing


   


current-date, current-dateTime, current-time

These three functions are used to obtain the current date, the current time, or both.

Signature

These functions take no arguments.

Type
Meaning
Result
xs:date, xs:dateTime, or xs:time
The current date, dateTime, or time

Effect

The current date and time forms part of the runtime context of an XPath expression. It will normally be taken from the system clock.

The resulting value will always have an explicit timezone component. The timezone will be taken from the implicit timezone provided by the evaluation context. In practice, this means it will probably be derived from the system default timezone for the machine on which the XSLT processor is running, or from the profile of the particular user.

In XSLT 2.0 it is defined that multiple calls on
current-dateTime()
and the other two functions will return the same result every time they are called within a single transformation. This means you can't call the function at the beginning and the end of the transformation to measure the elapsed time. The reason for this rule is that XSLT is rather purist about being a strictly functional language, and in a strictly functional language, calling the same function twice with the same arguments always returns the same result. This property makes life much simpler for optimizers.

In XQuery 1.0, the same rule applies to multiple calls within a single query.

Examples

Expression
Possible Result
current-date()
An
xs:date
, say
2008-06-02Z
current-dateTime()
An
xs:dateTime
, say
2008-06-02T12:35:02-05:00
current-time()
An
xs:time
, say
12:35:02Z

Other books

Ice Hunter by Joseph Heywood
Mastered by Maxwell, H. L.
In the Blink of an Eye by Michael Waltrip
Judas Burning by Carolyn Haines
Refugee by Anthony, Piers
Luna: New Moon by Ian McDonald
Poacher by Leon Mare
Calico Cross by DeAnna Kinney