xsl:attribute xsl:comment xsl:copy xsl:element xsl:fallback xsl:for-each xsl:if xsl:message xsl:otherwise xsl:param xsl:processing-instruction xsl:template xsl:variable xsl:when... xsl:i
Trang 1xsl:text Syntax
The xsl:text element is used to insert text verbatim into the document:
<xsl:text
disable-output-escaping = “yes” | “no”>
string data
</xsl:text>
By default, special XML characters will be escaped By setting disable-output-escapingto “yes”, they will not be Table 13.19 lists the parent-child relationships
Table 13.19 Parent-Child Relationships
CAN BE A CHILD OF CAN BE A PARENT OF
xsl:attribute
xsl:comment
xsl:copy
xsl:element
xsl:fallback
xsl:for-each
xsl:if
xsl:message
xsl:otherwise
xsl:param
xsl:processing-instruction
xsl:template
xsl:variable
xsl:when
Trang 2Generally, xsl:text is used to include arbitrary amounts of whitespace in the end document With the disable-output-escaping attribute set to yes, you can also use it to create XML tags in the text However, you have to use the escaped characters inside the xml:text tag:
<pre>
<xsl:text>
W o w ! ! ! ! W h i t e s p a c e ! ! !
Default tag: <aTag/>
</xsl:text>
<xsl:text disable-output-escaping=”yes”>
disable-output-escaping tag: <aTag/>
</xsl:text>
</pre>
The xsl:comment element generates an XML comment in the output:
<xsl:comment>
XSLT and XML
</xsl:comment>
Table 13.20 lists the parent-child relationships
Table 13.20 Parent-Child Relationships
CAN BE A CHILD OF CAN BE A PARENT OF
xsl:copy xsl:apply-imports
xsl:element xsl:apply-templates
xsl:fallback xsl:call-templates
xsl:for-each xsl:choose
(continues)
Trang 3Table 13.20 Parent-Child Relationships (Continued)
CAN BE A CHILD OF CAN BE A PARENT OF
xsl:if xsl:copy
xsl:message xsl:copy-of
xsl:otherwise xsl:fallback
xsl:param xsl:for-each
xsl:template xsl:if
xsl:variable xsl:message
xsl:text xsl:value-of xsl:variable
The start tag is replaced with <!— and the end tag is replaced with —> Here is an example:
<xsl:comment> This is my comment </xsl:comment>
The xsl:copy element allows you to copy the current node It won’t copy the attrib-utes or child elements
<xsl:copy
use-attribute-sets = “attribute_set_list”>
</xsl:copy>
The sole attribute, use-attribute-sets, is set to a whitespace-delimited list of attribute set names These are merged and the attributes are set on the merged node You call also use the attribute element to accomplish the same thing Table 13.21 lists the parent-child relationships
Trang 4Table 13.21 Parent-Child Relationships
CAN BE A CHILD OF CAN BE A PARENT OF
xsl:copy xsl:apply-templates
xsl:element xsl:attribute
xsl:fallback xsl:call-template
xsl:for-each xsl:choose
xsl:if xsl:comment
xsl:message xsl:copy
xsl:otherwise xsl:copy-of
xsl:param xsl:element
xsl:processing-instruction xsl:fallback
xsl:template xsl:for-each
xsl:variable xsl:if
xsl:number xsl:processing-instruction xsl:text
xsl:value-of xsl:variable
The following example uses the xsl:copy element to copy only those rows of the clerk employees:
<?xml version=”1.0”?>
<xsl:stylesheet
version=”1.0”
xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”>
<xsl:template match=”page”>
<xsl:element name=”ROWSET”>
<xsl:apply-templates select=”ROWSET/ROW”/>
</xsl:element>
</xsl:template>
Trang 5<xsl:template match=”ROW”>
<xsl:if test=”JOB=’CLERK’”>
<xsl:copy>
<xsl:for-each select=”@*”>
<xsl:copy/>
</xsl:for-each>
<xsl:apply-templates select=”ENAME | SAL”/>
</xsl:copy>
</xsl:if>
</xsl:template>
<xsl:template match=”ENAME | SAL”>
<xsl:copy>
<xsl:value-of select=”.”/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
The xsl:copy-of element allows you to copy the tree fragment specified by the selectattribute:
<xsl:copy-of
select = “XPath_expression” />
The single attribute, select, is an expression that should evaluate to a node set The members of the node set and their children will be copied Table 13.22 lists the parent-child relationships
Table 13.22 Parent-Child Relationships
CAN BE A CHILD OF CAN BE A PARENT OF
xsl:attribute
xsl:comment
xsl:copy
xsl:element
xsl:fallback
xsl:for-each
xsl:if
Trang 6Table 13.22 (Continued)
CAN BE A CHILD OF CAN BE A PARENT OF
xsl:if
xsl:message
xsl:otherwise
xsl:param
xsl:processing-instruction
xsl:template
xsl:variable
xsl:when
Here is a simple example that creates a new XML document containing only those employees who are clerks:
<?xml version=”1.0”?>
<xsl:stylesheet
version=”1.0”
xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”>
<xsl:template match=”page”>
<xsl:apply-templates select=”ROWSET/ROW”/>
</xsl:template>
<xsl:template match=”ROW”>
<xsl:if test=”JOB=’CLERK’”>
<xsl:copy-of select=”.”/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
The xsl:namespace-alias allows you translate one namespace in the input to another namespace in the output:
<xsl:namespace-alias
stylesheet-prefix = “prefix” | “#default”
result-prefix = “prefix” | “#default” />
Table 13.23 lists the attributes
Trang 7Table 13.23 xsl:namespace-alias Attributes
ATTRIBUTE DESCRIPTION
stylesheet-prefix The prefix of the namespace that should be
translated in the output
result-prefix The prefix that should replace the prefix described
by the stylesheet-prefix attribute
If the string “#default” is used for either attribute, it would represent the default namespace This is either no namespace declared by xmlns or no namespace at all The xsl:namespace-alias element must be the child of the root element and may not have child elements
Your stylesheets can have multiple xsl:namespace-alias elements However,
it is an error to have multiple xsl:namespace-alias elements that have the same stylesheet-prefix, and it may produce conflicts to have multiple xsl: namespace-aliaselements that have the same result-prefix elements
The xsl:processing-instruction allows you to specify an xml processing instruction It can’t be used to specify the xml declaration; for that, you can use the xsl:outputelement
<xsl:processing-instruction
name = “processing_instruction_name”>
xslt template elements and other text & xml
</xsl:processing-instruction>
The name attribute specifies the name of the processing instruction Table 13.24 lists the parent-child relationships
Table 13.24 Parent-Child Relationships
CAN BE A CHILD OF CAN BE A PARENT OF
xsl:copy xsl:apply-imports
xsl:element xsl:apply-templates
xsl:fallback xsl:call-template
xsl:for-each xsl:choose
Trang 8Table 13.24 (Continued)
CAN BE A CHILD OF CAN BE A PARENT OF
xsl:if xsl:copy
xsl:message xsl:copy-of
xsl:otherwise xsl:fallback
xsl:param xsl:for-each
xsl:template xsl:if
xsl:variable xsl:message
xsl:value-of xsl:variable
The value of the xsl:processing-instruction becomes the string value included in the processing instruction Any template elements between the start and end tags will be evaluated Here is an example in which you specify that the out-put uses a particular stylesheet:
<xsl:processing-instruction name=”xml-stylesheet”>
type=”text/xml” href=”someStylesheet.xsl”
</xsl:processing-instruction>
This xsl:processing-instruction would produce the following processing instruction in the output:
<?xml-stylesheet type=”text/xml” href=”someStylesheet.xsl”?>
An xsl:processing-instruction element must evaluate to a valid XML processing instruction
Numbering Elements
There are two XSLT elements that concern numbering The first, xsl:number, is used
to provide a formatted number that details the position of a node in a node list The second, xsl:decimal-format, is used in conjunction with the format-number function that will be covered in the “XPath” section
Trang 9The xsl:number element inserts a formatted number into the output Typically, this element is used to provide numbering for a set of XML elements in the source document
<xsl:number
level = “single” | “multiple” | “any”
count = “pattern”
from = “pattern”
value = “number_expression”
format = “format_mask”
lang = “nmtoken”
letter-value = { “alphabetic” | “traditional” }
grouping-separator = “char”
grouping-size = “number” />
The attributes of elements control how the numbering is done By default, the num-berelement produces the next number in sequence each time the XSLT processor encounters the particular element The attributes enhance this basic behavior Table 13.25 lists the attributes
Table 13.25 xsl:number Attributes
ATTRIBUTE DESCRIPTION
level If set to single, only the nodes in a node set will be
numbered; if set to multiple, the counting crosses node sets will be numbered Any level is used for multiple layered counting
count A pattern that describes which patterns should be
counted
from A pattern that specifies where counting should
start
value If present, the expression will be evaluated and the
result placed into the output as the number By default, the value that the xsl:number element outputs is determined by context More information on this below
format Specifies the format mask for the outputted
number
lang Specifies the language to use in determining the
sequence when alphabetic characters are used in the numbering
Trang 10Table 13.25 (Continued)
ATTRIBUTE DESCRIPTION
letter-value Used to remove ambiguities, in many languages,
of alphabetic lettering By default, traditional is specified This means that a format of I starts the roman numeral sequence I, II, III, and so on
Specifying alphabetic starts the sequence I, J, K, and so on
grouping-separator Specifies the character that separates digits In
English this is customarily a comma, but in some other languages this is a space The default is a comma
grouping-size Specifies the size of groups separated by the
grouping separator By default, this is 3
Table 13.26 lists the parent-child relationships
Table 13.26 Parent-Child Relationships
CAN BE A CHILD OF CAN BE A PARENT OF
xsl:attribute
xsl:comment
xsl:copy
xsl:element
xsl:fallback
xsl:for-each
xsl:if
xsl:message
xsl:otherwise
xsl:param
xsl:processing-instruction
xsl:template
xsl:variable
xsl:when
Trang 11xsl:decimal-format Syntax
The xsl:decimal-format element is used in conjunction with the format -number()function It defines a named decimal format that can be referenced by the format-number()function when it translates floating-point numbers for output
<xsl:decimal-format
name = “decimal_format_name”
decimal-separator = “decimal_separator”
grouping-separator = “grouping_separator”
infinity = “infinity_string”
minus-sign = “minus_char”
NaN = “NaN_string”
percent = “percent_char”
per-mille = “per_mille”
zero-digit = “zero_digit”
digit = “digit”
pattern-separator = “pattern_separator” />
Table 13.27 describes the syntax
This element can only be a child of the root element and can’t have any children
Table 13.27 xsl:decimal-format Syntax
ATTRIBUTE DESCRIPTION
Name The string by which this decimal format can be
referenced in the format-number-function call decimal-separator Separates the integer part from the fractional part
“.”is the default
grouping-separator Separates groups of digits “,” is the default
infinity String that represents infinity “Infinity” is the
default
minus-sign Prefixes negative numbers “-”is the default
NaN Represents “not a number” “NaN” is the default Percent Represents percent “%” is the default
per-mille Represents per mille #x2030 is the default
Zero-digit Represents zero in the format pattern used by
format-number() “0” is the default
Digit Represent digits in the format pattern used by
format-number() “#” is the default
Pattern-separator Separates positive and negative subpatterns in the
format pattern “;”is the default
Trang 12Variables and Parameters
XSLT allows you to make your templates more modular by using variables and para-meters Parameters behave more like the parameters that you use in other languages, while variables behave more like constants The xsl:param and xsl:variable ments represent parameters and variables, respectively The xsl:with-param ele-ment is used to pass parameters to a template This section covers these three eleele-ments and provides examples of how variables and parameters can be used
An xsl:variable actually behaves like a constant—once its value is set, it can’t be changed:
<xsl:variable
name=”variable_name”>
.
</xsl:variable>
<xsl:variable
name=”variable_name”
select=”expression”/>
The name attribute is the name of the variable If the select attribute is specified, the result of the expression will be the value for the param, and the xsl:param element should have the empty-element form Table 13.28 lists the parent-child relationships
Table 13.28 Parent-Child Relationships
CAN BE A CHILD OF CAN BE A PARENT OF
xsl:attribute xsl:apply-imports
xsl:comment xsl:apply-templates
xsl:copy xsl:attribute
xsl:element xsl:call-template
xsl:fallback xsl:choose
xsl:for-each xsl:comment
xsl:if xsl:copy
xsl:message xsl:copy-of
xsl:otherwise xsl:element
xsl:param xsl:fallback
(continues)
Trang 13Table 13.28 Parent-Child Relationships (Continued)
CAN BE A CHILD OF CAN BE A PARENT OF
xsl:processing-instruction xsl:for-each
xsl:stylesheet xsl:if
xsl:template xsl:message
xsl:transform xsl:number
xsl:variable xsl:processing-instruction
xsl:value-of xsl:variables
The xsl:variable is typically used to grab information from the source XML that will be reused repeatedly throughout the stylesheet or to store information Once you grab the information, you can use it again and again by referencing the variable name with a $ prefix, as in $variable_name A variable’s value is set as part of its definition, making it quite different from the variables you use in other programming languages Those variables must be defined and their value must be changed many times over the life of the programs in which they are found In this way, variables act like constants You can define a variable either at the stylesheet level or at the template-body level When a variable is defined at the stylesheet level, it has global scope—you can use it anywhere in the document When it is defined at the template-body level, it can be used only if it is referenced in following siblings or descendants of following siblings This is similar to how local variables work in procedural languages—they aren’t avail-able outside of the subroutine In other words, they aren’t availavail-able outside the ele-ment in which they are created, and they are available only inside the eleele-ment after they have been declared
Variables at different levels of scope can have the same name If there are conflicts, the most narrowly scoped variable takes precedence For instance, the variable row_numdefined inside of a template preempts the variable row_num defined at the stylesheet level
<xsl:stylesheet
version=”1.0”
xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”>
<xsl:variable name=”row_num” select=”0”/>
<xsl:template match=”page”>