Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
Examples
The following example returns the name of a state in the USA based on a two-letter abbreviation for the state. If the abbreviation is not that of a recognized state, it outputs the abbreviation itself.
……
An alternative way of coding such an expression is to use template rules, perhaps in a particular mode:
The following example declares a variable called
width
and initializes its value to the
width
attribute of the current node, if there is one, or to 100 otherwise.
You might be tempted to write this as follows:
This is legal XSLT, but it does not achieve the required effect. This is because both the variables called
width
have a scope that is bounded by the containing element, so they are inaccessible outside the
Everyone has personal preferences when coding. I tend to prefer constructs that are more compact than
See Also
xsl:comment
The
Changes in 2.0
A
select
attribute is added in XSLT 2.0, allowing the content of the comment to be defined by an XPath expression rather than by a sequence constructor. It's no longer an error to include two adjacent hyphens in the value of a comment—the processor will insert a space to make it legal XML.
Format
select? =
expression
>
Position
Attributes
Name | Value | Meaning |
select optional | XPath Expression | Defines the string value of this comment node |
The
select
attribute and the sequence constructor are mutually exclusive; if one is present, the other must be absent.
Content
A sequence constructor.
Effect
The value of the comment is produced by evaluating either the
select
attribute or the sequence constructor. If neither is present, the comment will be empty.
The value is computed in the same way as for
If the comment includes a hyphen that is immediately followed either by a second hyphen or by the end of the comment, a single space will be added after the offending hyphen to ensure that the comment follows the XML rules.
In XML or HTML output, the comment will appear as:
Usage
In theory, a comment has no meaning to the software that processes the output document—it is intended only for human readers. Comments are therefore useful to record when and how the document was generated, or perhaps to explain the meaning of the tags.
Comments can be particularly useful for debugging the stylesheet. If each
Comments in HTML output are used for some special markup conventions, for example surrounding Dynamic HTML scripts. The purpose of the comment here is to ensure that browsers that don't understand the script will skip over it rather than display it as text. An example is shown below.
Examples
Three examples follow.
Example 1: Showing the Date and Time of Transformation
The following example generates a comment showing the date and time at which the output file was generated, and identifying the XSLT processor that was used:
current-dateTime(),
‘[D] [MNn] [Y] at [H]:[m]:[s]’)”/>
Typical output might be:
Example 2: Generating Commented-Out JavaScript
The following example outputs a piece of client-side JavaScript to an HTML output file:
The output will look like this:
The comment cannot be written as a comment in the stylesheet, of course, because then the XSLT processor would ignore it entirely. Comments in the stylesheet are not copied to the output destination.
Example 3: Generating Comments Containing Markup
Sometimes you want to generate comments that contain markup, for example:
It's not possible to do this by creating a result tree that contains an element node as a child of a comment node, because comment nodes can't have children. This leaves two possibilities: either create the serialized representation of the element “by hand”, as a string, or trick the serializer into generating the comment delimiters by using character maps. The first approach looks like this:
<link rel=“stylesheet” type=“text/css” href=“IE5style.css” />
<![endif]
The alternative solution uses a character map:
where the two entities
if-lt-IE6
and
endif
are defined in the DTD to map to arbitrary private-use-area characters, followed by:
xsl:copy
The
Changes in 2.0
New attributes
copy-namespaces
and
inherit-namespaces
have been added, to allow finer control over whether namespace nodes for an element should be copied or not.
New attributes
validation
and
type
have been added to control whether and how a copied node is validated against a schema.
Format
copy-namespaces? = “yes” | “no”
inherit-namespaces? = “yes” | “no”
use-attribute-sets? = qnames
validation? = “strict” | “lax” | “preserve” | “strip”
type? = qname>
Position
Attributes
Name | Value | Meaning |
copy-namespaces optional | yes or no . (Default is yes ). | Indicates whether the namespace nodes of an element should be copied. |
inherit-namespaces optional | yes or no . (Default is yes ). | Indicates whether the children of a copied element will inherit its namespaces. |
use-attribute-sets optional | Whitespace-separated list of lexical QNames | The names of attribute sets to be applied to a copied element. |
validation optional | strict , lax , preserve , or skip | Indicates whether and how the copied nodes should be subjected to schema validation, or whether existing type annotations should be retained or removed. |