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

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

The generated identifiers are unique within a single execution of the stylesheet. If the same stylesheet is used several times, with the same or different source documents, the function may generate the same identifiers in each run but is under no obligation to do so.

Usage and Examples

In XSLT 1.0, the
generate-id()
function was often used to determine whether two expressions represented the same node, that is, to compare nodes by identity. XPath 2.0 offers the
is
operator for this purpose, so this usage can be expected to dwindle.

The main intended purpose of the
generate-id()
function is to create hyperlinks in the output document. For example, it can be used to generate ID and IDREF attributes in an output XML document, or

and

pairs in an output HTML document.

Example: Using generate-id() to Create Links

This example takes as input a file
resorts.xml
containing details of a collection of holiday resorts, each of which includes a list of hotels.

Source

Stylesheet

The stylesheet
resorts.xsl
constructs an output HTML page in which the hotels are listed first, followed by information about the resorts. Each hotel entry contains a hyperlink to the relevant resort details. The links for the resorts are generated using
generate-id()
applied to the

element.

This is a complete stylesheet that uses the
Simplified Stylesheet
syntax introduced on page 125, in Chapter 3.

Notice how
generate-id()
is used twice, once to generate the identifier of the resort, the next time to generate a link from the hotel.

Output

The following output was obtained using Saxon. I have added some extra indentation to show the structure. A different product will generate different identifiers in the

elements, but the links will work just as well.


   

      Hotels

        Grand Hotel

          

Address:  … 


          

Stars: 5


          

Resort: Amsterdam


        Central Hotel

          

Address:  … 


          

Stars: 5


          

Resort: Bruges


        Less Grand Hotel

        

Address:  … 


         

Stars: 2


         

Resort: Amsterdam


       Peripheral Hotel

         

Address:  … 


         

Stars: 2


         

Resort: Bruges


     Resorts

       Amsterdam

         

A wordy description of Amsterdam


       Bruges

         

An eloquent description of Bruges


  


There is no inverse function to
generate-id()
: specifically, there is no direct way to find a node if its generated id is known, other than the potentially inefficient:

//node()[generate-id()=$X]

If you need to do this, however, you can set up a key definition as follows.


Then find the element with a given id value using the expression:

key(‘gid-key’, $X)

It is important to appreciate that the generated identifiers bear no resemblance to any ID attribute values in the source document, so the nodes cannot be found using the
id()
function.

Also, the ID values generated in one run of the processor may be different from those generated in a subsequent run. You need to bear this in mind if you are using the ID values as hyperlinks. If the transformation is likely to be run more than once, then it isn't safe to reference these ID values from another document, or even to save them as a bookmark in a browser.

See Also

id()
on page 802

key()
on page 812

is
operator in Chapter 8 on page 593

hours-from-dateTime, hours-from-time

These two functions extract the hour component from an
xs:dateTime
or
xs:time
value. For example, at noon local time both these functions return 12.

Signature

Argument
Type
Meaning
input
xs:dateTime?
or
xs:time?
The value from which the hour component is to be extracted. The type of the supplied argument must correspond to the type implied by the function name.
Result
xs:integer
The hour, in the range 0 to 23 (midnight is represented as 0)
.

Effect

The function returns the hour component of the supplied
xs:dateTime
or
xs:time
. The value is from the time as expressed in its local timezone (not normalized to UTC). This means that if the time (or dateTime) has a timezone, the value is the time in that timezone; if it has no timezone, it is the value as written.

If an empty sequence is supplied, an empty sequence is returned.

Examples

Expression
Result
hours-from-time(xs:time(“12:35:03.142”))
12
hours-from-dateTime(xs:dateTime(“2008-02-28T13:55:30”))
13
hours-from-time(xs:time(“23:59:59+01:00”))
23
hours-from-dateTime(xs:dateTime(“2008-07-31T22:10:00-05:00”))
22
hours-from-dateTime(xs:dateTime(“2008-07-31T24:00:00”))
0

See Also

current-date()
,
-dateTime()
,
-time()
on page 738

format-date()
,
-dateTime()
,
-time()
on page 781

minutes-from-dateTime()
,
-time()
on page 832

seconds-from-dateTime()
,
-time()
on page 873

hours-from-duration

This function extracts the value of the hours component from a normalized
xs:duration
value.

Signature

Argument
Type
Meaning
input
xs:duration
?
The value from which the component is to be extracted. If an empty sequence is supplied, an empty sequence is returned.
Result
xs:integer?
The hours component, in the range

23 to +23
.

Effect

The function returns the hours component of the supplied
xs:duration
. The duration value is first normalized so that the number of hours is less than 24, the number of minutes is less than 60, and so on. The result will be negative if the duration is negative. The result will therefore be in the range −23 to +23.

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

Other books

The Bourne Betrayal by Lustbader, Eric Van, Ludlum, Robert
The Hottest Ticket in Town by Kimberly Van Meter
The Riders by Tim Winton
The Mapmaker's Daughter by Laurel Corona
Morningstar by Armstrong, S. L.
White People by Allan Gurganus
As an Earl Desires by Lorraine Heath