Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
Usage
The
Before using
The output produced by
A common use of
The Microsoft MSXML parser ignores
terminate=“yes”
is specified, it generates an error, which can be handled through the script on the HTML page that invoked the transformation.
Examples
The following example issues a message and quits if the value of the
Unfortunately, there is no mechanism defined in the XSLT standard that allows the location of the error in the source document to be included in the message. In this example, validation against a schema would probably give better diagnostics.
The following example extends this by allowing several such errors to be reported in a single run, terminating the run only after all errors have been reported. It works by assigning a global variable to the set of nodes in error:
select=“//sales[not(. castable as xs:decimal)]”/>
is not numeric
…
Localized Messages
XSLT is designed very much with internationalization in mind, and no doubt the requirement to localize message text was discussed by the working group. They clearly decided that no special facilities were needed and instead included a detailed example in the XSLT 1.0 specification showing how the message text can be localized (output in the user's native language). The example is worth repeating because it shows a general technique.
Messages for a particular language are stored in a file whose name identifies the language, for example German messages might be stored in
messages/de.xml
. The message file might have the structure:
A stylesheet that wishes to produce messages in the appropriate local language will need a parameter to identify this language. The stylesheet can then get access to the messages file for the appropriate language, and read the messages from there:
select=“concat(‘messages/’, $language, ‘.xml’)”/>
select=“document($message-file)/messages”/>
The same technique can, of course, be used for producing localized text to include in the output of the transformation.
See Also
xsl:namespace
The
Changes in 2.0
This instruction is new in XSLT 2.0.
Format
name = { string }
select? = expression
Position
Attributes
Name | Value | Meaning |
name mandatory | Attribute value template returning an NCName or a zero-length string | The name of the namespace node (which represents the namespace prefix) |
select optional | XPath Expression | Expression to compute the string value of the namespace node (which represents the namespace URI) |
Content
If the
select
attribute is present, the element must be empty. Otherwise, it must contain a nonempty sequence constructor.
Effect
The
name
attribute determines the name of the namespace node. This corresponds to the namespace prefix. If the value of the
name
attribute is a zero-length string, the namespace node defines the default namespace. Otherwise, the name must be an NCName (a valid XML name containing no colon). The
name
attribute can be written as an attribute value template, allowing the name to be computed dynamically.
The string value of the namespace node, which represents the namespace URI, is established using either the
select
attribute or the contained sequence constructor. For consistency, this instruction uses the results of evaluating the
select
attribute or sequence constructor in the same way as other instructions such as
select
attribute is used. Normally, however, the result of the
select
attribute should be a single string.