Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
The next example is similar—but this time, instead of looking for a vendor-supplied extension function, we will be looking for a user-supplied function implemented as a Java method.
Example 2: Testing Availability of a Java Method
In this example, we'll assume that the stylesheet (
charset-available.xsl
) is always going to run under a particular XSLT 2.0 processor, but that it might run under different Java VMs. We'll suppose the existence of an XSLT 2.0 processor that can run under both JDK 1.3 and JDK 1.4, and we'll suppose that we want to use the Java method
Charset.isSupported()
in the
java.nio.charset
package, which was new in JDK 1.4. Under JDK 1.3, we'll behave as if the extension function returned
false
.
Stylesheet
We achieve this by writing two versions of a variable declaration. Only one of them is compiled, based on the value of the
use-when
attribute.
as=‘xs:boolean’
select=‘Charset:isSupported(“EUC-JP”)’
xmlns:Charset=‘java:java.nio.charset.Charset’
use-when=‘function-available(“Charset:isSupported”, 1)’/>
as=‘xs:boolean’
select=‘false()’
xmlns:Charset=‘java:java.nio.charset.Charset’
use-when=‘not(function-available(“Charset:isSupported”, 1))’/>
encoding=“{if ($charset-ok) then ‘EUC-JP’ else ‘UTF-8’}”>
See Also
element-available()
on page 764
generate-id
This function is available in XSLT only
.
The
generate-id()
function generates a string, in the form of an XML
Name
, that uniquely identifies a node. The result is guaranteed to be different for every node that participates in a given transformation.
For example, the expression
generate-id(..)
might return the string
N015732
when using one XSLT processor, and
b23a1c79
when using another.
Changes in 2.0
None.
Signature
Argument | Type | Meaning |
node (optional) | node()? | The input node. If the argument is omitted, the context node is used. If an empty sequence is supplied, the zero-length string is returned. |
Result | xs:string | A string value that uniquely identifies the node. This will consist only of ASCII alphanumeric characters, and the first character will be alphabetic. This makes the identifier suitable for use in many contexts, for example as an ID value in an XML document or an HTML anchor . |
Effect
If the
node
argument is omitted, it defaults to
.
, the context item. A type error occurs if this is not a node.
If the
node
argument is an empty sequence, the function returns a zero-length string.
The function returns an arbitrary string. Within a given transformation, the function will always return the same string for the same node, and it will always return different strings for different nodes: in other words,
generate-id($A)=generate-id($B)
is true if and only if
$A is $B
is true, and this includes the case where the nodes are in different documents.