<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Id: REC-xslt-19991116.xml,v 1.1 1999/11/16 15:01:35 hugo Exp $ -->
<!DOCTYPE spec PUBLIC "-//W3C//DTD Specification V2.0//EN"
                      "/XML/1998/06/xmlspec-v20.dtd" [
<!ENTITY XML "http://www.w3.org/TR/REC-xml">
<!ENTITY XMLNames "http://www.w3.org/TR/REC-xml-names">
<!ENTITY XSLT.ns "http://www.w3.org/1999/XSL/Transform">
<!ENTITY XSLTA.ns "http://www.w3.org/1999/XSL/TransformAlias">
<!ENTITY XSLFO.ns "http://www.w3.org/1999/XSL/Format">
<!ENTITY XHTML.ns "http://www.w3.org/TR/xhtml1/strict">
<!ENTITY year "1999">
<!ENTITY month "November">
<!ENTITY MM "11">
<!ENTITY day "16">
<!ENTITY DD "16">
<!ENTITY YYYYMMDD "&year;&MM;&DD;">
<!ENTITY LEV "REC">
<!ENTITY XPath "http://www.w3.org/TR/xpath">
<!ATTLIST xfunction href CDATA "&XPath;">
<!-- DTD customizations -->
<!ELEMENT proto (arg*)>
<!ATTLIST proto
  name NMTOKEN #REQUIRED
  return-type (number|string|boolean|node-set|object) #REQUIRED
>
<!ELEMENT arg EMPTY>
<!ATTLIST arg
  type (number|string|boolean|node-set|object) #REQUIRED
  occur (opt|rep) #IMPLIED
>
<!ELEMENT function (#PCDATA)>
<!ELEMENT xfunction (#PCDATA)>
<!ATTLIST xfunction href CDATA #REQUIRED>
<!ELEMENT e:element-syntax
  (e:in-category*, e:attribute*, (e:empty|e:text|e:element|e:model|e:sequence|e:choice))
>
<!ATTLIST e:element-syntax
  xmlns:e CDATA #FIXED "http://www.w3.org/1999/XSL/Spec/ElementSyntax"
  name NMTOKEN #REQUIRED
>
<!ELEMENT e:in-category EMPTY>
<!ATTLIST
  e:in-category name NMTOKEN #REQUIRED
  xmlns:e CDATA #FIXED "http://www.w3.org/1999/XSL/Spec/ElementSyntax"
>
<!ELEMENT e:attribute (e:attribute-value-template|(e:constant|e:data-type)+)>
<!ATTLIST e:attribute
  name NMTOKEN #REQUIRED
  required (yes) #IMPLIED
  xmlns:e CDATA #FIXED "http://www.w3.org/1999/XSL/Spec/ElementSyntax"
>
<!ELEMENT e:attribute-value-template (e:constant|e:data-type)+>
<!ATTLIST e:attribute-value-template
  xmlns:e CDATA #FIXED "http://www.w3.org/1999/XSL/Spec/ElementSyntax"
>
<!ELEMENT e:constant EMPTY>
<!ATTLIST
  e:constant value CDATA #REQUIRED
  xmlns:e CDATA #FIXED "http://www.w3.org/1999/XSL/Spec/ElementSyntax"
>
<!ELEMENT e:data-type EMPTY>
<!ATTLIST e:data-type
  name NMTOKEN #REQUIRED
  xmlns:e CDATA #FIXED "http://www.w3.org/1999/XSL/Spec/ElementSyntax"
>
<!ELEMENT e:empty EMPTY>
<!ATTLIST e:empty
  xmlns:e CDATA #FIXED "http://www.w3.org/1999/XSL/Spec/ElementSyntax"
>
<!ELEMENT e:text EMPTY>
<!ATTLIST e:text
  xmlns:e CDATA #FIXED "http://www.w3.org/1999/XSL/Spec/ElementSyntax"
>
<!ELEMENT e:element EMPTY>
<!ATTLIST e:element
  name NMTOKEN #REQUIRED
  repeat (zero-or-one|zero-or-more|one-or-more) #IMPLIED
  xmlns:e CDATA #FIXED "http://www.w3.org/1999/XSL/Spec/ElementSyntax"
>
<!ELEMENT e:model EMPTY>
<!ATTLIST e:model
  name NMTOKEN #REQUIRED
  repeat (zero-or-one|zero-or-more|one-or-more) #IMPLIED
  xmlns:e CDATA #FIXED "http://www.w3.org/1999/XSL/Spec/ElementSyntax"
>
<!ELEMENT e:sequence (e:element|e:model|e:choice)+>
<!ATTLIST e:sequence
  repeat (zero-or-one|zero-or-more|one-or-more) #IMPLIED
  xmlns:e CDATA #FIXED "http://www.w3.org/1999/XSL/Spec/ElementSyntax"
>
<!ELEMENT e:choice (e:element|e:model|e:sequence)+>
<!ATTLIST e:choice
  repeat (zero-or-one|zero-or-more|one-or-more) #IMPLIED
  xmlns:e CDATA #FIXED "http://www.w3.org/1999/XSL/Spec/ElementSyntax"
>
<!ELEMENT e:element-syntax-summary EMPTY>
<!ATTLIST e:element-syntax-summary 
  xmlns:e CDATA #FIXED "http://www.w3.org/1999/XSL/Spec/ElementSyntax"
>
<!ENTITY % local.illus.class "|proto|e:element-syntax|e:element-syntax-summary">
<!ENTITY % local.tech.class "|function|xfunction">
<!ENTITY % local.loc.class "|var">
<!ELEMENT var (#PCDATA)>
<!ATTLIST spec
  xmlns:e CDATA #FIXED "http://www.w3.org/1999/XSL/Spec/ElementSyntax"
>
]>
<spec xmlns:e="http://www.w3.org/1999/XSL/Spec/ElementSyntax">
<header>
<title>XSL Transformations (XSLT)</title>
<version>Version 1.0</version>
<w3c-designation>&LEV;-xslt-&YYYYMMDD;</w3c-designation>
<w3c-doctype>W3C Recommendation</w3c-doctype>
<pubdate><day>&day;</day><month>&month;</month><year>&year;</year></pubdate>
<publoc>
<loc href="http://www.w3.org/TR/&year;/&LEV;-xslt-&YYYYMMDD;"
          >http://www.w3.org/TR/&year;/&LEV;-xslt-&YYYYMMDD;</loc>
<loc role="available-format"
href="http://www.w3.org/TR/&year;/&LEV;-xslt-&YYYYMMDD;.xml">XML</loc>
<loc role="available-format"
href="http://www.w3.org/TR/&year;/&LEV;-xslt-&YYYYMMDD;.html">HTML</loc>
<!--
<loc href="http://www.w3.org/TR/&year;/&LEV;-xslt-&YYYYMMDD;.pdf"
          >http://www.w3.org/TR/&year;/&LEV;-xslt-&YYYYMMDD;.pdf</loc>
-->
</publoc>
<latestloc>
<loc href="http://www.w3.org/TR/xslt"
          >http://www.w3.org/TR/xslt</loc>
</latestloc>
<prevlocs>
<loc href="http://www.w3.org/TR/1999/PR-xslt-19991008"
          >http://www.w3.org/TR/1999/PR-xslt-19991008</loc>
<loc href="http://www.w3.org/1999/08/WD-xslt-19990813"
          >http://www.w3.org/1999/08/WD-xslt-19990813</loc>
<loc href="http://www.w3.org/1999/07/WD-xslt-19990709"
          >http://www.w3.org/1999/07/WD-xslt-19990709</loc>
<loc href="http://www.w3.org/TR/1999/WD-xslt-19990421"
          >http://www.w3.org/TR/1999/WD-xslt-19990421</loc>
<loc href="http://www.w3.org/TR/1998/WD-xsl-19981216"
          >http://www.w3.org/TR/1998/WD-xsl-19981216</loc>
<loc href="http://www.w3.org/TR/1998/WD-xsl-19980818"
          >http://www.w3.org/TR/1998/WD-xsl-19980818</loc>
</prevlocs>
<authlist>
<author>
<name>James Clark</name>
<email href="mailto:jjc@jclark.com">jjc@jclark.com</email>
</author>
</authlist>

<status>

<p>This document has been reviewed by W3C Members and other interested
parties and has been endorsed by the Director as a W3C <loc
href="http://www.w3.org/Consortium/Process/#RecsW3C">Recommendation</loc>. It
is a stable document and may be used as reference material or cited as
a normative reference from other documents. W3C's role in making the
Recommendation is to draw attention to the specification and to
promote its widespread deployment. This enhances the functionality and
interoperability of the Web.</p>

<p>The list of known errors in this specification is available at
<loc href="http://www.w3.org/&year;/&MM;/&LEV;-xslt-&YYYYMMDD;-errata"
>http://www.w3.org/&year;/&MM;/&LEV;-xslt-&YYYYMMDD;-errata</loc>.</p>

<p>Comments on this specification may be sent to <loc
href="mailto:xsl-editors@w3.org">xsl-editors@w3.org</loc>; <loc
href="http://lists.w3.org/Archives/Public/xsl-editors">archives</loc>
of the comments are available.  Public discussion of XSL, including
XSL Transformations, takes place on the <loc
href="http://www.mulberrytech.com/xsl/xsl-list/index.html">XSL-List</loc>
mailing list.</p>

<p>The English version of this specification is the only normative
version. However, for translations of this document, see <loc
href="http://www.w3.org/Style/XSL/translations.html"
>http://www.w3.org/Style/XSL/translations.html</loc>.</p>

<p>A list of current W3C Recommendations and other technical documents
can be found at <loc
href="http://www.w3.org/TR">http://www.w3.org/TR</loc>.</p>

<p>This specification has been produced as part of the <loc
href="http://www.w3.org/Style/Activity">W3C Style activity</loc>.</p>

</status>

<abstract>

<p>This specification defines the syntax and semantics of XSLT, which
is a language for transforming XML documents into other XML
documents.</p>

<p>XSLT is designed for use as part of XSL, which is a stylesheet
language for XML. In addition to XSLT, XSL includes an XML vocabulary
for specifying formatting.  XSL specifies the styling of an XML
document by using XSLT to describe how the document is transformed
into another XML document that uses the formatting vocabulary.</p>

<p>XSLT is also designed to be used independently of XSL.  However,
XSLT is not intended as a completely general-purpose XML
transformation language.  Rather it is designed primarily for the
kinds of transformations that are needed when XSLT is used as part of
XSL.</p>

</abstract>

<langusage>
<language id="EN">English</language>
<language id="ebnf">EBNF</language>
</langusage>
<revisiondesc>
<slist>
<sitem>See RCS log for revision history.</sitem>
</slist>
</revisiondesc>
</header>
<body>
<div1>
<head>Introduction</head>

<p>This specification defines the syntax and semantics of the XSLT
language.  A transformation in the XSLT language is expressed as a
well-formed XML document <bibref ref="XML"/> conforming to the
Namespaces in XML Recommendation <bibref ref="XMLNAMES"/>, which may
include both elements that are defined by XSLT and elements that are
not defined by XSLT.  <termdef id="dt-xslt-namespace" term="XSLT
Namespace">XSLT-defined elements are distinguished by belonging to a
specific XML namespace (see <specref ref="xslt-namespace"/>), which is
referred to in this specification as the <term>XSLT
namespace</term>.</termdef> Thus this specification is a definition of
the syntax and semantics of the XSLT namespace.</p>

<p>A transformation expressed in XSLT describes rules for transforming
a source tree into a result tree.  The transformation is achieved by
associating patterns with templates.  A pattern is matched against
elements in the source tree.  A template is instantiated to create
part of the result tree.  The result tree is separate from the source
tree.  The structure of the result tree can be completely different
from the structure of the source tree. In constructing the result
tree, elements from the source tree can be filtered and reordered, and
arbitrary structure can be added.</p>

<p>A transformation expressed in XSLT is called a stylesheet.  This is
because, in the case when XSLT is transforming into the XSL formatting
vocabulary, the transformation functions as a stylesheet.</p>

<p>This document does not specify how an XSLT stylesheet is associated
with an XML document.  It is recommended that XSL processors support
the mechanism described in <bibref ref="XMLSTYLE"/>.  When this or any
other mechanism yields a sequence of more than one XSLT stylesheet to
be applied simultaneously to a XML document, then the effect
should be the same as applying a single stylesheet that imports each
member of the sequence in order (see <specref ref="import"/>).</p>

<p>A stylesheet contains a set of template rules.  A template rule has
two parts: a pattern which is matched against nodes in the source tree
and a template which can be instantiated to form part of the result
tree.  This allows a stylesheet to be applicable to a wide class of
documents that have similar source tree structures.</p>

<p>A template is instantiated for a particular source element
to create part of the result tree. A template can contain elements
that specify literal result element structure.  A template can also
contain elements from the XSLT namespace
that are instructions for creating result tree
fragments.  When a template is instantiated, each instruction is
executed and replaced by the result tree fragment that it creates.
Instructions can select and process descendant source elements.  Processing a
descendant element creates a result tree fragment by finding the
applicable template rule and instantiating its template. Note
that elements are only processed when they have been selected by the
execution of an instruction.  The result tree is constructed by
finding the template rule for the root node and instantiating
its template.</p>

<p>In the process of finding the applicable template rule, more
than one template rule may have a pattern that matches a given
element. However, only one template rule will be applied. The
method for deciding which template rule to apply is described
in <specref ref="conflict"/>.</p>

<p>A single template by itself has considerable power: it can create
structures of arbitrary complexity; it can pull string values out of
arbitrary locations in the source tree; it can generate structures
that are repeated according to the occurrence of elements in the
source tree.  For simple transformations where the structure of the
result tree is independent of the structure of the source tree, a
stylesheet can often consist of only a single template, which
functions as a template for the complete result tree.  Transformations
on XML documents that represent data are often of this kind (see
<specref ref="data-example"/>). XSLT allows a simplified syntax for
such stylesheets (see <specref ref="result-element-stylesheet"/>).</p>

<p>When a template is instantiated, it is always instantiated with
respect to a <termdef id="dt-current-node" term="Current
Node"><term>current node</term></termdef> and a <termdef
id="dt-current-node-list" term="Current Node List"><term>current node
list</term></termdef>. The current node is always a member of the
current node list.  Many operations in XSLT are relative to the
current node. Only a few instructions change the current node list or
the current node (see <specref ref="rules"/> and <specref
ref="for-each"/>); during the instantiation of one of these
instructions, the current node list changes to a new list of nodes and
each member of this new list becomes the current node in turn; after
the instantiation of the instruction is complete, the current node and
current node list revert to what they were before the instruction was
instantiated.</p>

<p>XSLT makes use of the expression language defined by <bibref
ref="XPATH"/> for selecting elements for processing, for conditional
processing and for generating text.</p>

<p>XSLT provides two <quote>hooks</quote> for extending the language,
one hook for extending the set of instruction elements used in
templates and one hook for extending the set of functions used in
XPath expressions.  These hooks are both based on XML namespaces.
This version of XSLT does not define a mechanism for implementing the
hooks. See <specref ref="extension"/>.</p>

<note><p>The XSL WG intends to define such a mechanism in a future
version of this specification or in a separate
specification.</p></note>

<p>The element syntax summary notation used to describe the syntax of
XSLT-defined elements is described in <specref ref="notation"/>.</p>

<p>The MIME media types <code>text/xml</code> and
<code>application/xml</code> <bibref ref="RFC2376"/> should be used
for XSLT stylesheets.  It is possible that a media type will be
registered specifically for XSLT stylesheets; if and when it is, that
media type may also be used.</p>

</div1>

<div1>
<head>Stylesheet Structure</head>

<div2 id="xslt-namespace">
<head>XSLT Namespace</head>

<p>The XSLT namespace has the URI <code>&XSLT.ns;</code>.</p>

<note><p>The <code>1999</code> in the URI indicates the year in which
the URI was allocated by the W3C.  It does not indicate the version of
XSLT being used, which is specified by attributes (see <specref
ref="stylesheet-element"/> and <specref
ref="result-element-stylesheet"/>).</p></note>

<p>XSLT processors must use the XML namespaces mechanism <bibref
ref="XMLNAMES"/> to recognize elements and attributes from this
namespace. Elements from the XSLT namespace are recognized only in the
stylesheet not in the source document. The complete list of
XSLT-defined elements is specified in <specref
ref="element-syntax-summary"/>.  Vendors must not extend the XSLT
namespace with additional elements or attributes. Instead, any
extension must be in a separate namespace.  Any namespace that is used
for additional instruction elements must be identified by means of the
extension element mechanism specified in <specref
ref="extension-element"/>.</p>

<p>This specification uses a prefix of <code>xsl:</code> for referring
to elements in the XSLT namespace. However, XSLT stylesheets are free
to use any prefix, provided that there is a namespace declaration that
binds the prefix to the URI of the XSLT namespace.</p>

<p>An element from the XSLT namespace may have any attribute not from
the XSLT namespace, provided that the <xtermref
href="&XPath;#dt-expanded-name">expanded-name</xtermref> of the
attribute has a non-null namespace URI.  The presence of such
attributes must not change the behavior of XSLT elements and functions
defined in this document. Thus, an XSLT processor is always free to
ignore such attributes, and must ignore such attributes without giving
an error if it does not recognize the namespace URI. Such attributes
can provide, for example, unique identifiers, optimization hints, or
documentation.</p>

<p>It is an error for an element from the XSLT namespace to have
attributes with expanded-names that have null namespace URIs
(i.e. attributes with unprefixed names) other than attributes defined
for the element in this document.</p>

<note><p>The conventions used for the names of XSLT elements,
attributes and functions are that names are all lower-case, use
hyphens to separate words, and use abbreviations only if they already
appear in the syntax of a related language such as XML or
HTML.</p></note>


</div2>

<div2 id="stylesheet-element">
<head>Stylesheet Element</head>

<e:element-syntax name="stylesheet">
  <e:attribute name="id">
    <e:data-type name="id"/>
  </e:attribute>
  <e:attribute name="extension-element-prefixes">
    <e:data-type name="tokens"/>
  </e:attribute>
  <e:attribute name="exclude-result-prefixes">
    <e:data-type name="tokens"/>
  </e:attribute>
  <e:attribute name="version" required="yes">
    <e:data-type name="number"/>
  </e:attribute>
  <e:sequence>
    <e:element repeat="zero-or-more" name="import"/>
    <e:model name="top-level-elements"/>
  </e:sequence>
</e:element-syntax>

<e:element-syntax name="transform">
  <e:attribute name="id">
    <e:data-type name="id"/>
  </e:attribute>
  <e:attribute name="extension-element-prefixes">
    <e:data-type name="tokens"/>
  </e:attribute>
  <e:attribute name="exclude-result-prefixes">
    <e:data-type name="tokens"/>
  </e:attribute>
  <e:attribute name="version" required="yes">
    <e:data-type name="number"/>
  </e:attribute>
  <e:sequence>
    <e:element repeat="zero-or-more" name="import"/>
    <e:model name="top-level-elements"/>
  </e:sequence>
</e:element-syntax>

<p>A stylesheet is represented by an <code>xsl:stylesheet</code>
element in an XML document.  <code>xsl:transform</code> is allowed as
a synonym for <code>xsl:stylesheet</code>.</p>

<p>An <code>xsl:stylesheet</code> element must have a
<code>version</code> attribute, indicating the version of XSLT that
the stylesheet requires.  For this version of XSLT, the value should
be <code>1.0</code>.  When the value is not equal to <code>1.0</code>,
forwards-compatible processing mode is enabled (see <specref
ref="forwards"/>).</p>

<p>The <code>xsl:stylesheet</code> element may contain the following types
of elements:</p>
<ulist>
<item><p><code>xsl:import</code></p></item>
<item><p><code>xsl:include</code></p></item>
<item><p><code>xsl:strip-space</code></p></item>
<item><p><code>xsl:preserve-space</code></p></item>
<item><p><code>xsl:output</code></p></item>
<item><p><code>xsl:key</code></p></item>
<item><p><code>xsl:decimal-format</code></p></item>
<item><p><code>xsl:namespace-alias</code></p></item>
<item><p><code>xsl:attribute-set</code></p></item>
<item><p><code>xsl:variable</code></p></item>
<item><p><code>xsl:param</code></p></item>
<item><p><code>xsl:template</code></p></item>
</ulist>

<p><termdef id="dt-top-level" term="Top-level">An element occurring as
a child of an <code>xsl:stylesheet</code> element is called a
<term>top-level</term> element.</termdef></p>

<p>This example shows the structure of a stylesheet.  Ellipses
(<code>...</code>) indicate where attribute values or content have
been omitted.  Although this example shows one of each type of allowed
element, stylesheets may contain zero or more of each of these
elements.</p>

<eg>&lt;xsl:stylesheet version="1.0"
                xmlns:xsl="&XSLT.ns;"><![CDATA[
  <xsl:import href="..."/>

  <xsl:include href="..."/>

  <xsl:strip-space elements="..."/>
  
  <xsl:preserve-space elements="..."/>

  <xsl:output method="..."/>

  <xsl:key name="..." match="..." use="..."/>

  <xsl:decimal-format name="..."/>

  <xsl:namespace-alias stylesheet-prefix="..." result-prefix="..."/>

  <xsl:attribute-set name="...">
    ...
  </xsl:attribute-set>

  <xsl:variable name="...">...</xsl:variable>

  <xsl:param name="...">...</xsl:param>

  <xsl:template match="...">
    ...
  </xsl:template>

  <xsl:template name="...">
    ...
  </xsl:template>

</xsl:stylesheet>]]></eg>

<p>The order in which the children of the <code>xsl:stylesheet</code>
element occur is not significant except for <code>xsl:import</code>
elements and for error recovery.  Users are free to order the elements
as they prefer, and stylesheet creation tools need not provide control
over the order in which the elements occur.</p>

<p>In addition, the <code>xsl:stylesheet</code> element may contain
any element not from the XSLT namespace, provided that the
expanded-name of the element has a non-null namespace URI.  The presence of
such top-level elements must not change the behavior of XSLT elements
and functions defined in this document; for example, it would not be
permitted for such a top-level element to specify that
<code>xsl:apply-templates</code> was to use different rules to resolve
conflicts. Thus, an XSLT processor is always free to ignore such
top-level elements, and must ignore a top-level element without giving
an error if it does not recognize the namespace URI. Such elements can
provide, for example,</p>

<ulist>

<item><p>information used by extension elements or extension functions
(see <specref ref="extension"/>),</p></item>

<item><p>information about what to do with the result tree,</p></item>

<item><p>information about how to obtain the source tree,</p></item>

<item><p>metadata about the stylesheet,</p></item>

<item><p>structured documentation for the stylesheet.</p></item>

</ulist>

</div2>

<div2 id="result-element-stylesheet">
<head>Literal Result Element as Stylesheet</head>

<p>A simplified syntax is allowed for stylesheets that consist of only
a single template for the root node.  The stylesheet may consist of
just a literal result element (see <specref
ref="literal-result-element"/>).  Such a stylesheet is equivalent to a
stylesheet with an <code>xsl:stylesheet</code> element containing a
template rule containing the literal result element; the template rule
has a match pattern of <code>/</code>. For example</p>

<eg>&lt;html xsl:version="1.0"
      xmlns:xsl="&XSLT.ns;"
      xmlns="&XHTML.ns;"><![CDATA[
  <head>
    <title>Expense Report Summary</title>
  </head>
  <body>
    <p>Total Amount: <xsl:value-of select="expense-report/total"/></p>
  </body>
</html>]]></eg>

<p>has the same meaning as</p>

<eg>&lt;xsl:stylesheet version="1.0"
                xmlns:xsl="&XSLT.ns;"
                xmlns="&XHTML.ns;"><![CDATA[
<xsl:template match="/">
<html>
  <head>
    <title>Expense Report Summary</title>
  </head>
  <body>
    <p>Total Amount: <xsl:value-of select="expense-report/total"/></p>
  </body>
</html>
</xsl:template>
</xsl:stylesheet>]]></eg>

<p>A literal result element that is the document element of a
stylesheet must have an <code>xsl:version</code> attribute, which
indicates the version of XSLT that the stylesheet requires.  For this
version of XSLT, the value should be <code>1.0</code>; the value must
be a <xnt href="&XPath;#NT-Number">Number</xnt>.  Other literal result
elements may also have an <code>xsl:version</code> attribute. When the
<code>xsl:version</code> attribute is not equal to <code>1.0</code>,
forwards-compatible processing mode is enabled (see <specref
ref="forwards"/>).</p>

<p>The allowed content of a literal result element when used as a
stylesheet is no different from when it occurs within a
stylesheet. Thus, a literal result element used as a stylesheet cannot
contain <termref def="dt-top-level">top-level</termref> elements.</p>

<p>In some situations, the only way that a system can recognize that an
XML document needs to be processed by an XSLT processor as an XSLT
stylesheet is by examining the XML document itself.  Using the
simplified syntax makes this harder.</p>

<note><p>For example, another XML language (AXL) might also use an
<code>axl:version</code> on the document element to indicate that an
XML document was an AXL document that required processing by an AXL
processor; if a document had both an <code>axl:version</code>
attribute and an <code>xsl:version</code> attribute, it would be
unclear whether the document should be processed by an XSLT processor
or an AXL processor.</p></note>

<p>Therefore, the simplified syntax should not be used for XSLT
stylesheets that may be used in such a situation.  This situation can,
for example, arise when an XSLT stylesheet is transmitted as a message
with a MIME media type of <code>text/xml</code> or
<code>application/xml</code> to a recipient that will use the MIME
media type to determine how the message is processed.</p>

</div2>

<div2 id="qname">
<head>Qualified Names</head>

<p>The name of an internal XSLT object, specifically a named template
(see <specref ref="named-templates"/>), a mode (see <specref
ref="modes"/>), an attribute set (see <specref
ref="attribute-sets"/>), a key (see <specref ref="key"/>), a
decimal-format (see <specref ref="format-number"/>), a variable or a
parameter (see <specref ref="variables"/>) is specified as a <xnt
href="&XMLNames;#NT-QName">QName</xnt>.  If it has a prefix, then the
prefix is expanded into a URI reference using the namespace
declarations in effect on the attribute in which the name occurs.  The
<xtermref href="&XPath;#dt-expanded-name">expanded-name</xtermref>
consisting of the local part of the name and the possibly null URI
reference is used as the name of the object.  The default namespace is
<emph>not</emph> used for unprefixed names.</p>

</div2>

<div2 id="forwards">
<head>Forwards-Compatible Processing</head>

<p>An element enables forwards-compatible mode for itself, its
attributes, its descendants and their attributes if either it is an
<code>xsl:stylesheet</code> element whose <code>version</code>
attribute is not equal to <code>1.0</code>, or it is a literal result
element that has an <code>xsl:version</code> attribute whose value is
not equal to <code>1.0</code>, or it is a literal result element that
does not have an <code>xsl:version</code> attribute and that is the
document element of a stylesheet using the simplified syntax (see
<specref ref="result-element-stylesheet"/>).  A literal result element
that has an <code>xsl:version</code> attribute whose value is equal to
<code>1.0</code> disables forwards-compatible mode for itself, its
attributes, its descendants and their attributes.</p>

<p>If an element is processed in forwards-compatible mode, then:</p>

<ulist>

<item><p>if it is a <termref def="dt-top-level">top-level</termref>
element and XSLT 1.0 does not allow such elements as top-level
elements, then the element must be ignored along with its
content;</p></item>

<item><p>if it is an element in a template and XSLT 1.0 does not allow
such elements to occur in templates, then if the element is not
instantiated, an error must not be signaled, and if the element is
instantiated, the XSLT must perform fallback for the element as
specified in <specref ref="fallback"/>;</p></item>

<item><p>if the element has an attribute that XSLT 1.0 does not allow
the element to have or if the element has an optional attribute with a
value that the XSLT 1.0 does not allow the attribute to have, then the
attribute must be ignored.</p></item>

</ulist>

<p>Thus, any XSLT 1.0 processor must be able to process the following
stylesheet without error, although the stylesheet includes elements
from the XSLT namespace that are not defined in this
specification:</p>

<eg>&lt;xsl:stylesheet version="1.1"
                xmlns:xsl="&XSLT.ns;"><![CDATA[
  <xsl:template match="/">
    <xsl:choose>
      <xsl:when test="system-property('xsl:version') >= 1.1">
        <xsl:exciting-new-1.1-feature/>
      </xsl:when>
      <xsl:otherwise>
        <html>
        <head>
          <title>XSLT 1.1 required</title>
        </head>
        <body>
          <p>Sorry, this stylesheet requires XSLT 1.1.</p>
        </body>
        </html>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>]]></eg>

<note><p>If a stylesheet depends crucially on a top-level element
introduced by a version of XSL after 1.0, then the stylesheet can use
an <code>xsl:message</code> element with <code>terminate="yes"</code>
(see <specref ref="message"/>) to ensure that XSLT processors
implementing earlier versions of XSL will not silently ignore the
top-level element. For example,</p>

<eg>&lt;xsl:stylesheet version="1.5"
                xmlns:xsl="&XSLT.ns;"><![CDATA[

  <xsl:important-new-1.1-declaration/>

  <xsl:template match="/">
    <xsl:choose>
      <xsl:when test="system-property('xsl:version') &lt; 1.1">
        <xsl:message terminate="yes">
          <xsl:text>Sorry, this stylesheet requires XSLT 1.1.</xsl:text>
        </xsl:message>
      </xsl:when>
      <xsl:otherwise>
        ...
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  ...
</xsl:stylesheet>]]></eg>
</note>

<p>If an <termref def="dt-expression">expression</termref> occurs in
an attribute that is processed in forwards-compatible mode, then an
XSLT processor must recover from errors in the expression as
follows:</p>

<ulist>

<item><p>if the expression does not match the syntax allowed by the
XPath grammar, then an error must not be signaled unless the
expression is actually evaluated;</p></item>

<item><p>if the expression calls a function with an unprefixed name
that is not part of the XSLT library, then an error must not be
signaled unless the function is actually called;</p></item>

<item><p>if the expression calls a function with a number of arguments
that XSLT does not allow or with arguments of types that XSLT does not
allow, then an error must not be signaled unless the function is
actually called.</p></item>

</ulist>


</div2>

<div2>
<head>Combining Stylesheets</head>

<p>XSLT provides two mechanisms to combine stylesheets:</p>

<slist>

<sitem>an inclusion mechanism that allows stylesheets to be combined
without changing the semantics of the stylesheets being combined,
and</sitem>

<sitem>an import mechanism that allows stylesheets to override each
other.</sitem>

</slist>

<div3 id="include">
<head>Stylesheet Inclusion</head>

<e:element-syntax name="include">
  <e:in-category name="top-level-element"/>
  <e:attribute name="href" required="yes">
    <e:data-type name="uri-reference"/>
  </e:attribute>
  <e:empty/>
</e:element-syntax>

<p>An XSLT stylesheet may include another XSLT stylesheet using an
<code>xsl:include</code> element. The <code>xsl:include</code> element
has an <code>href</code> attribute whose value is a URI reference
identifying the stylesheet to be included.  A relative URI is resolved
relative to the base URI of the <code>xsl:include</code> element (see
<specref ref="base-uri"/>).</p>

<p>The <code>xsl:include</code> element is only allowed as a <termref
def="dt-top-level">top-level</termref> element.</p>

<p>The inclusion works at the XML tree level.  The resource located by
the <code>href</code> attribute value is parsed as an XML document,
and the children of the <code>xsl:stylesheet</code> element in this
document replace the <code>xsl:include</code> element in the including
document.  The fact that template rules or definitions are included
does not affect the way they are processed.</p>

<p>The included stylesheet may use the simplified syntax described in
<specref ref="result-element-stylesheet"/>.  The included stylesheet
is treated the same as the equivalent <code>xsl:stylesheet</code>
element.</p>

<p>It is an error if a stylesheet directly or indirectly includes
itself.</p>

<note><p>Including a stylesheet multiple times can cause errors
because of duplicate definitions.  Such multiple inclusions are less
obvious when they are indirect. For example, if stylesheet
<var>B</var> includes stylesheet <var>A</var>, stylesheet <var>C</var>
includes stylesheet <var>A</var>, and stylesheet <var>D</var> includes
both stylesheet <var>B</var> and stylesheet <var>C</var>, then
<var>A</var> will be included indirectly by <var>D</var> twice.  If
all of <var>B</var>, <var>C</var> and <var>D</var> are used as
independent stylesheets, then the error can be avoided by separating
everything in <var>B</var> other than the inclusion of <var>A</var>
into a separate stylesheet <var>B'</var> and changing <var>B</var> to
contain just inclusions of <var>B'</var> and <var>A</var>, similarly
for <var>C</var>, and then changing <var>D</var> to include
<var>A</var>, <var>B'</var>, <var>C'</var>.</p></note>

</div3>

<div3 id="import">
<head>Stylesheet Import</head>

<e:element-syntax name="import">
  <e:attribute name="href" required="yes">
    <e:data-type name="uri-reference"/>
  </e:attribute>
  <e:empty/>
</e:element-syntax>

<p>An XSLT stylesheet may import another XSLT stylesheet using an
<code>xsl:import</code> element.  Importing a stylesheet is the same
as including it (see <specref ref="include"/>) except that definitions
and template rules in the importing stylesheet take precedence over
template rules and definitions in the imported stylesheet; this is
described in more detail below.  The <code>xsl:import</code> element
has an <code>href</code> attribute whose value is a URI reference
identifying the stylesheet to be imported.  A relative URI is resolved
relative to the base URI of the <code>xsl:import</code> element (see
<specref ref="base-uri"/>).</p>

<p>The <code>xsl:import</code> element is only allowed as a <termref
def="dt-top-level">top-level</termref> element.  The
<code>xsl:import</code> element children must precede all other
element children of an <code>xsl:stylesheet</code> element, including
any <code>xsl:include</code> element children.  When
<code>xsl:include</code> is used to include a stylesheet, any
<code>xsl:import</code> elements in the included document are moved up
in the including document to after any existing
<code>xsl:import</code> elements in the including document.</p>

<p>For example,</p>

<eg>&lt;xsl:stylesheet version="1.0"
                xmlns:xsl="&XSLT.ns;"><![CDATA[
  <xsl:import href="article.xsl"/>
  <xsl:import href="bigfont.xsl"/>
  <xsl:attribute-set name="note-style">
    <xsl:attribute name="font-style">italic</xsl:attribute>
  </xsl:attribute-set>
</xsl:stylesheet>]]></eg>

<p><termdef id="dt-import-tree" term="Import Tree">The
<code>xsl:stylesheet</code> elements encountered during processing of
a stylesheet that contains <code>xsl:import</code> elements are
treated as forming an <term>import tree</term>.  In the import tree,
each <code>xsl:stylesheet</code> element has one import child for each
<code>xsl:import</code> element that it contains. Any
<code>xsl:include</code> elements are resolved before constructing the
import tree.</termdef> <termdef id="dt-import-precedence" term="Import
Precedence">An <code>xsl:stylesheet</code> element in the import tree
is defined to have lower <term>import precedence</term> than another
<code>xsl:stylesheet</code> element in the import tree if it would be
visited before that <code>xsl:stylesheet</code> element in a
post-order traversal of the import tree (i.e. a traversal of the
import tree in which an <code>xsl:stylesheet</code> element is visited
after its import children).</termdef> Each definition and template
rule has import precedence determined by the
<code>xsl:stylesheet</code> element that contains it.</p>

<p>For example, suppose</p>

<ulist>

<item><p>stylesheet <var>A</var> imports stylesheets <var>B</var>
and <var>C</var> in that order;</p></item>

<item><p>stylesheet <var>B</var> imports stylesheet
<var>D</var>;</p></item>

<item><p>stylesheet <var>C</var> imports stylesheet
<var>E</var>.</p></item>

</ulist>

<p>Then the order of import precedence (lowest first) is
<var>D</var>, <var>B</var>, <var>E</var>, <var>C</var>,
<var>A</var>.</p>

<note><p>Since <code>xsl:import</code> elements are required to occur
before any definitions or template rules, an implementation that
processes imported stylesheets at the point at which it encounters the
<code>xsl:import</code> element will encounter definitions and
template rules in increasing order of import precedence.</p></note>

<p>In general, a definition or template rule with higher import
precedence takes precedence over a definition or template rule with
lower import precedence.  This is defined in detail for each kind of
definition and for template rules.</p>

<p>It is an error if a stylesheet directly or indirectly imports
itself. Apart from this, the case where a stylesheet with a particular
URI is imported in multiple places is not treated specially. The
<termref def="dt-import-tree">import tree</termref> will have a
separate <code>xsl:stylesheet</code> for each place that it is
imported.</p>

<note><p>If <code>xsl:apply-imports</code> is used (see <specref
ref="apply-imports"/>), the behavior may be different from the
behavior if the stylesheet had been imported only at the place with
the highest <termref def="dt-import-precedence">import
precedence</termref>.</p></note>

</div3>

</div2>

<div2>
<head>Embedding Stylesheets</head>

<p>Normally an XSLT stylesheet is a complete XML document with the
<code>xsl:stylesheet</code> element as the document element. However,
an XSLT stylesheet may also be embedded in another resource. Two forms
of embedding are possible:</p>

<slist>

<sitem>the XSLT stylesheet may be textually embedded in a non-XML
resource, or</sitem>

<sitem>the <code>xsl:stylesheet</code> element may occur in an XML
document other than as the document element.</sitem>

</slist>

<p>To facilitate the second form of embedding, the
<code>xsl:stylesheet</code> element is allowed to have an ID attribute
that specifies a unique identifier.</p>

<note><p>In order for such an attribute to be used with the XPath
<xfunction>id</xfunction> function, it must actually be declared in
the DTD as being an ID.</p></note>

<p>The following example shows how the <code>xml-stylesheet</code>
processing instruction <bibref ref="XMLSTYLE"/> can be used to allow a
document to contain its own stylesheet.  The URI reference uses a
relative URI with a fragment identifier to locate the
<code>xsl:stylesheet</code> element:</p>

<eg><![CDATA[<?xml-stylesheet type="text/xml" href="#style1"?>
<!DOCTYPE doc SYSTEM "doc.dtd">
<doc>
<head>
<xsl:stylesheet id="style1"
                version="1.0"]]>
                xmlns:xsl="&XSLT.ns;"
                xmlns:fo="&XSLFO.ns;"><![CDATA[
<xsl:import href="doc.xsl"/>
<xsl:template match="id('foo')">
  <fo:block font-weight="bold"><xsl:apply-templates/></fo:block>
</xsl:template>
<xsl:template match="xsl:stylesheet">
  <!-- ignore -->
</xsl:template>
</xsl:stylesheet>
</head>
<body>
<para id="foo">
...
</para>
</body>
</doc>
]]></eg>

<note><p>A stylesheet that is embedded in the document to which it is
to be applied or that may be included or imported into an stylesheet
that is so embedded typically needs to contain a template rule that
specifies that <code>xsl:stylesheet</code> elements are to be
ignored.</p></note>

</div2>

</div1>

<div1 id="data-model">
<head>Data Model</head>

<p>The data model used by XSLT is the same as that used by <xspecref
href="&XPath;#data-model">XPath</xspecref> with the additions
described in this section.  XSLT operates on source, result and
stylesheet documents using the same data model.  Any two XML documents
that have the same tree will be treated the same by XSLT.</p>

<p>Processing instructions and comments in the stylesheet are ignored:
the stylesheet is treated as if neither processing instruction nodes
nor comment nodes were included in the tree that represents the
stylesheet.</p>

<div2 id="root-node-children">
<head>Root Node Children</head>

<p>The normal restrictions on the children of the root node are
relaxed for the result tree.  The result tree may have any sequence of
nodes as children that would be possible for an element node. In
particular, it may have text node children, and any number of element
node children. When written out using the XML output method (see
<specref ref="output"/>), it is possible that a result tree will not
be a well-formed XML document; however, it will always be a
well-formed external general parsed entity.</p>

<p>When the source tree is created by parsing a well-formed XML
document, the root node of the source tree will automatically satisfy
the normal restrictions of having no text node children and exactly
one element child.  When the source tree is created in some other way,
for example by using the DOM, the usual restrictions are relaxed for
the source tree as for the result tree.</p>

</div2>

<div2 id="base-uri">
<head>Base URI</head>

<p>Every node also has an associated URI called its base URI, which is
used for resolving attribute values that represent relative URIs into
absolute URIs.  If an element or processing instruction occurs in an
external entity, the base URI of that element or processing
instruction is the URI of the external entity; otherwise, the base URI
is the base URI of the document.  The base URI of the document node is
the URI of the document entity.  The base URI for a text node, a
comment node, an attribute node or a namespace node is the base URI of
the parent of the node.</p>

</div2>

<div2 id="unparsed-entities">
<head>Unparsed Entities</head>

<p>The root node has a mapping that gives the URI for each unparsed
entity declared in the document's DTD.  The URI is generated from the
system identifier and public identifier specified in the entity
declaration. The XSLT processor may use the public identifier to
generate a URI for the entity instead of the URI specified in the
system identifier.  If the XSLT processor does not use the public
identifier to generate the URI, it must use the system identifier; if
the system identifier is a relative URI, it must be resolved into an
absolute URI using the URI of the resource containing the entity
declaration as the base URI <bibref ref="RFC2396"/>.</p>

</div2>

<div2 id="strip">
<head>Whitespace Stripping</head>

<p>After the tree for a source document or stylesheet document has
been constructed, but before it is otherwise processed by XSLT,
some text nodes are stripped.  A text node is never stripped
unless it contains only whitespace characters.  Stripping the text
node removes the text node from the tree.  The stripping process takes
as input a set of element names for which whitespace must be
preserved.  The stripping process is applied to both stylesheets and
source documents, but the set of whitespace-preserving element names
is determined differently for stylesheets and for source
documents.</p>

<p>A text node is preserved if any of the following apply:</p>

<ulist>

<item><p>The element name of the parent of the text node is in the set
of whitespace-preserving element names.</p></item>

<item><p>The text node contains at least one non-whitespace character.
As in XML, a whitespace character is #x20, #x9, #xD or #xA.</p></item>

<item><p>An ancestor element of the text node has an
<code>xml:space</code> attribute with a value of
<code>preserve</code>, and no closer ancestor element has
<code>xml:space</code> with a value of
<code>default</code>.</p></item>

</ulist>

<p>Otherwise, the text node is stripped.</p>

<p>The <code>xml:space</code> attributes are not stripped from the
tree.</p>

<note><p>This implies that if an <code>xml:space</code> attribute is
specified on a literal result element, it will be included in the
result.</p></note>

<p>For stylesheets, the set of whitespace-preserving element names
consists of just <code>xsl:text</code>.</p>

<e:element-syntax name="strip-space">
  <e:in-category name="top-level-element"/>
  <e:attribute name="elements" required="yes">
    <e:data-type name="tokens"/>
  </e:attribute>
  <e:empty/>
</e:element-syntax>

<e:element-syntax name="preserve-space">
  <e:in-category name="top-level-element"/>
  <e:attribute name="elements" required="yes">
    <e:data-type name="tokens"/>
  </e:attribute>
  <e:empty/>
</e:element-syntax>

<p>For source documents, the set of whitespace-preserving element
names is specified by <code>xsl:strip-space</code> and
<code>xsl:preserve-space</code> <termref
def="dt-top-level">top-level</termref> elements.  These elements each
have an <code>elements</code> attribute whose value is a
whitespace-separated list of <xnt
href="&XPath;#NT-NameTest">NameTest</xnt>s.  Initially, the
set of whitespace-preserving element names contains all element names.
If an element name matches a <xnt
href="&XPath;#NT-NameTest">NameTest</xnt> in an
<code>xsl:strip-space</code> element, then it is removed from the set
of whitespace-preserving element names.  If an element name matches a
<xnt href="&XPath;#NT-NameTest">NameTest</xnt> in an
<code>xsl:preserve-space</code> element, then it is added to the set
of whitespace-preserving element names.  An element matches a <xnt
href="&XPath;#NT-NameTest">NameTest</xnt> if and only if the
<xnt href="&XPath;#NT-NameTest">NameTest</xnt> would be true
for the element as an <xspecref href="&XPath;#node-tests">XPath node
test</xspecref>.  Conflicts between matches to
<code>xsl:strip-space</code> and <code>xsl:preserve-space</code>
elements are resolved the same way as conflicts between template rules
(see <specref ref="conflict"/>).  Thus, the applicable match for a
particular element name is determined as follows:</p>

<ulist>

<item><p>First, any match with lower <termref
def="dt-import-precedence">import precedence</termref> than another
match is ignored.</p></item>

<item><p>Next, any match with a <xnt
href="&XPath;#NT-NameTest">NameTest</xnt> that has a lower
<termref def="dt-default-priority">default priority</termref> than the
<termref def="dt-default-priority">default priority</termref> of the
<xnt href="&XPath;#NT-NameTest">NameTest</xnt> of another
match is ignored.</p></item>

</ulist>

<p>It is an error if this leaves more than one match.  An XSLT
processor may signal the error; if it does not signal the error, it
must recover by choosing, from amongst the matches that are left, the
one that occurs last in the stylesheet.</p>

</div2>

</div1>

<div1>
<head>Expressions</head>

<p>XSLT uses the expression language defined by XPath <bibref
ref="XPATH"/>.  Expressions are used in XSLT for a variety of purposes
including:</p>

<slist>
<sitem>selecting nodes for processing;</sitem>
<sitem>specifying conditions for different ways of processing a node;</sitem>
<sitem>generating text to be inserted in the result tree.</sitem>
</slist>

<p><termdef id="dt-expression" term="Expression">An
<term>expression</term> must match the XPath production <xnt
href="&XPath;#NT-Expr">Expr</xnt>.</termdef></p>

<p>Expressions occur as the value of certain attributes on
XSLT-defined elements and within curly braces in <termref
def="dt-attribute-value-template">attribute value
template</termref>s.</p>

<p>In XSLT, an outermost expression (i.e. an expression that is not
part of another expression) gets its context as follows:</p>

<ulist>

<item><p>the context node comes from the <termref
def="dt-current-node">current node</termref></p></item>

<item><p>the context position comes from the position of the <termref
def="dt-current-node">current node</termref> in the <termref
def="dt-current-node-list">current node list</termref>; the first
position is 1</p></item>

<item><p>the context size comes from the size of the <termref
def="dt-current-node-list">current node list</termref></p></item>

<item><p>the variable bindings are the bindings in scope on the
element which has the attribute in which the expression occurs (see
<specref ref="variables"/>)</p></item>

<item><p>the set of namespace declarations are those in scope on the
element which has the attribute in which the expression occurs;
this includes the implicit declaration of the prefix <code>xml</code>
required by the the XML Namespaces Recommendation <bibref ref="XMLNAMES"/>;
the default
namespace (as declared by <code>xmlns</code>) is not part of this
set</p></item>

<item><p>the function library consists of the core function library
together with the additional functions defined in <specref
ref="add-func"/> and extension functions as described in <specref
ref="extension"/>; it is an error for an expression to include a call
to any other function</p></item>

</ulist>

</div1>

<div1 id="rules">
<head>Template Rules</head>

<div2>
<head>Processing Model</head>

<p>A list of source nodes is processed to create a result tree
fragment.  The result tree is constructed by processing a list
containing just the root node.  A list of source nodes is processed by
appending the result tree structure created by processing each of the
members of the list in order.  A node is processed by finding all the
template rules with patterns that match the node, and choosing the
best amongst them; the chosen rule's template is then instantiated
with the node as the <termref def="dt-current-node">current
node</termref> and with the list of source nodes as the <termref
def="dt-current-node-list">current node list</termref>.  A template
typically contains instructions that select an additional list of
source nodes for processing.  The process of matching, instantiation
and selection is continued recursively until no new source nodes are
selected for processing.</p>

<p>Implementations are free to process the source document in any way
that produces the same result as if it were processed using this
processing model.</p>

</div2>

<div2 id="patterns">
<head>Patterns</head>

<p><termdef id="dt-pattern" term="Pattern">Template rules identify the
nodes to which they apply by using a <term>pattern</term>.  As well as
being used in template rules, patterns are used for numbering (see
<specref ref="number"/>) and for declaring keys (see <specref
ref="key"/>).  A pattern specifies a set of conditions on a node.  A
node that satisfies the conditions matches the pattern; a node that
does not satisfy the conditions does not match the pattern.  The
syntax for patterns is a subset of the syntax for expressions. In
particular, location paths that meet certain restrictions can be used
as patterns.  An expression that is also a pattern always evaluates to
an object of type node-set.  A node matches a pattern if the node is a
member of the result of evaluating the pattern as an expression with
respect to some possible context; the possible contexts are those
whose context node is the node being matched or one of its
ancestors.</termdef></p>

<p>Here are some examples of patterns:</p>

<ulist>

<item><p><code>para</code> matches any <code>para</code> element</p></item>

<item><p><code>*</code> matches any element</p></item>

<item><p><code>chapter|appendix</code> matches any
<code>chapter</code> element and any <code>appendix</code>
element</p></item>

<item><p><code>olist/item</code> matches any <code>item</code> element with
an <code>olist</code> parent</p></item>

<item><p><code>appendix//para</code> matches any <code>para</code> element with
an <code>appendix</code> ancestor element</p></item>

<item><p><code>/</code> matches the root node</p></item>

<item><p><code>text()</code> matches any text node</p></item>

<item><p><code>processing-instruction()</code> matches any processing
instruction</p></item>

<item><p><code>node()</code> matches any node other than an attribute
node and the root node</p></item>

<item><p><code>id("W11")</code> matches the element with unique ID
<code>W11</code></p></item>

<item><p><code>para[1]</code> matches any <code>para</code> element
that is the first <code>para</code> child element of its
parent</p></item>

<item><p><code>*[position()=1 and self::para]</code> matches any
<code>para</code> element that is the first child element of its
parent</p></item>

<item><p><code>para[last()=1]</code> matches any <code>para</code>
element that is the only <code>para</code> child element of its
parent</p></item>

<item><p><code>items/item[position()>1]</code> matches any
<code>item</code> element that has a <code>items</code> parent and
that is not the first <code>item</code> child of its parent</p></item>

<item><p><code>item[position() mod 2 = 1]</code> would be true for any
<code>item</code> element that is an odd-numbered <code>item</code>
child of its parent.</p></item>

<item><p><code>div[@class="appendix"]//p</code> matches any
<code>p</code> element with a <code>div</code> ancestor element that
has a <code>class</code> attribute with value
<code>appendix</code></p></item>

<item><p><code>@class</code> matches any <code>class</code> attribute
(<emph>not</emph> any element that has a <code>class</code>
attribute)</p></item>

<item><p><code>@*</code> matches any attribute</p></item>

</ulist>

<p>A pattern must match the grammar for <nt
def="NT-Pattern">Pattern</nt>.  A <nt def="NT-Pattern">Pattern</nt> is
a set of location path patterns separated by <code>|</code>.  A
location path pattern is a location path whose steps all use only the
<code>child</code> or <code>attribute</code> axes.  Although patterns
must not use the <code>descendant-or-self</code> axis, patterns may
use the <code>//</code> operator as well as the <code>/</code>
operator.  Location path patterns can also start with an
<xfunction>id</xfunction> or <function>key</function> function call
with a literal argument.  Predicates in a pattern can use arbitrary
expressions just like predicates in a location path.</p>

<scrap>
<head>Patterns</head>
<prodgroup pcw5="1" pcw2="10">
<prod id="NT-Pattern">
<lhs>Pattern</lhs>
<rhs><nt def="NT-LocationPathPattern">LocationPathPattern</nt></rhs>
<rhs>| <nt def="NT-Pattern">Pattern</nt> '|' <nt def="NT-LocationPathPattern">LocationPathPattern</nt></rhs>
</prod>
<prod id="NT-LocationPathPattern">
<lhs>LocationPathPattern</lhs>
<rhs>'/' <nt def="NT-RelativePathPattern">RelativePathPattern</nt>?</rhs>
<rhs>| <nt def="NT-IdKeyPattern">IdKeyPattern</nt> (('/' | '//') <nt def="NT-RelativePathPattern">RelativePathPattern</nt>)?</rhs>
<rhs>| '//'? <nt def="NT-RelativePathPattern">RelativePathPattern</nt></rhs>
</prod>
<prod id="NT-IdKeyPattern">
<lhs>IdKeyPattern</lhs>
<rhs>'id' '(' <xnt href="&XPath;#NT-Literal">Literal</xnt> ')'</rhs>
<rhs>| 'key' '(' <xnt href="&XPath;#NT-Literal">Literal</xnt> ',' <xnt href="&XPath;#NT-Literal">Literal</xnt> ')'</rhs>
</prod>
<prod id="NT-RelativePathPattern">
<lhs>RelativePathPattern</lhs>
<rhs><nt def="NT-StepPattern">StepPattern</nt></rhs>
<rhs>| <nt def="NT-RelativePathPattern">RelativePathPattern</nt> '/' <nt def="NT-StepPattern">StepPattern</nt></rhs>
<rhs>| <nt def="NT-RelativePathPattern">RelativePathPattern</nt> '//' <nt def="NT-StepPattern">StepPattern</nt></rhs>
</prod>
<prod id="NT-StepPattern">
<lhs>StepPattern</lhs>
<rhs>
<nt def="NT-ChildOrAttributeAxisSpecifier">ChildOrAttributeAxisSpecifier</nt>
<xnt href="&XPath;#NT-NodeTest">NodeTest</xnt>
<xnt href="&XPath;#NT-Predicate">Predicate</xnt>*
</rhs>
</prod>
<prod id="NT-ChildOrAttributeAxisSpecifier">
<lhs>ChildOrAttributeAxisSpecifier</lhs>
<rhs><xnt href="&XPath;#NT-AbbreviatedAxisSpecifier">AbbreviatedAxisSpecifier</xnt></rhs>
<rhs>| ('child' | 'attribute') '::'</rhs>
</prod>
</prodgroup>
</scrap>

<p>A pattern is defined to match a node if and only if there is
possible context such that when the pattern is evaluated as an
expression with that context, the node is a member of the resulting
node-set.  When a node is being matched, the possible contexts have a
context node that is the node being matched or any ancestor of that
node, and a context node list containing just the context node.</p>

<p>For example, <code>p</code> matches any <code>p</code> element,
because for any <code>p</code> if the expression <code>p</code> is
evaluated with the parent of the <code>p</code> element as context the
resulting node-set will contain that <code>p</code> element as one of
its members.</p>

<note><p>This matches even a <code>p</code> element that is the
document element, since the document root is the parent of the
document element.</p></note>

<p>Although the semantics of patterns are specified indirectly in
terms of expression evaluation, it is easy to understand the meaning
of a pattern directly without thinking in terms of expression
evaluation.  In a pattern, <code>|</code> indicates alternatives; a
pattern with one or more <code>|</code> separated alternatives matches
if any one of the alternative matches.  A pattern that consists of a
sequence of <nt def="NT-StepPattern">StepPattern</nt>s separated by
<code>/</code> or <code>//</code> is matched from right to left.  The
pattern only matches if the rightmost <nt
def="NT-StepPattern">StepPattern</nt> matches and a suitable element
matches the rest of the pattern; if the separator is <code>/</code>
then only the parent is a suitable element; if the separator is
<code>//</code>, then any ancestor is a suitable element.  A <nt
def="NT-StepPattern">StepPattern</nt> that uses the child axis matches
if the <xnt href="&XPath;#NT-NodeTest">NodeTest</xnt> is true for the
node and the node is not an attribute node.  A <nt
def="NT-StepPattern">StepPattern</nt> that uses the attribute axis
matches if the <xnt href="&XPath;#NT-NodeTest">NodeTest</xnt> is true
for the node and the node is an attribute node.  When <code>[]</code>
is present, then the first <xnt
href="&XPath;#NT-PredicateExpr">PredicateExpr</xnt> in a <nt
def="NT-StepPattern">StepPattern</nt> is evaluated with the node being
matched as the context node and the siblings of the context node that
match the <xnt href="&XPath;#NT-NodeTest">NodeTest</xnt> as the
context node list, unless the node being matched is an attribute node,
in which case the context node list is all the attributes that have
the same parent as the attribute being matched and that match the <xnt
href="&XPath;#NT-NameTest">NameTest</xnt>.</p>

<p>For example</p>

<eg>appendix//ulist/item[position()=1]</eg>

<p>matches a node if and only if all of the following are true:</p>

<ulist>

<item><p>the <xnt href="&XPath;#NT-NodeTest">NodeTest</xnt> <code>item</code> is
true for the node and the node is not an attribute; in other words the
node is an <code>item</code> element</p></item>

<item><p>evaluating the <xnt href="&XPath;#NT-PredicateExpr">PredicateExpr</xnt>
<code>position()=1</code> with the node as context node and the
siblings of the node that are <code>item</code> elements as the
context node list yields true</p></item>

<item><p>the node has a parent that matches
<code>appendix//ulist</code>; this will be true if the parent is a
<code>ulist</code> element that has an <code>appendix</code> ancestor
element.</p></item>

</ulist>

</div2>

<div2>
<head>Defining Template Rules</head>

<e:element-syntax name="template">
  <e:in-category name="top-level-element"/>
  <e:attribute name="match">
    <e:data-type name="pattern"/>
  </e:attribute>
  <e:attribute name="name">
    <e:data-type name="qname"/>
  </e:attribute>
  <e:attribute name="priority">
    <e:data-type name="number"/>
  </e:attribute>
  <e:attribute name="mode">
    <e:data-type name="qname"/>
  </e:attribute>
  <e:sequence>
    <e:element repeat="zero-or-more" name="param"/>
    <e:model name="template"/>
  </e:sequence>
</e:element-syntax>

<p>A template rule is specified with the <code>xsl:template</code>
element. The <code>match</code> attribute is a <nt
def="NT-Pattern">Pattern</nt> that identifies the source node or nodes
to which the rule applies.  The <code>match</code> attribute is
required unless the <code>xsl:template</code> element has a
<code>name</code> attribute (see <specref ref="named-templates"/>).
It is an error for the value of the <code>match</code> attribute to
contain a <xnt
href="&XPath;#NT-VariableReference">VariableReference</xnt>. The
content of the <code>xsl:template</code> element is the template that
is instantiated when the template rule is applied.</p>

<p>For example, an XML document might contain:</p>

<eg><![CDATA[This is an <emph>important</emph> point.]]></eg>

<p>The following template rule matches <code>emph</code> elements and
produces a <code>fo:inline-sequence</code> formatting object with a
<code>font-weight</code> property of <code>bold</code>.</p>

<eg><![CDATA[<xsl:template match="emph">
  <fo:inline-sequence font-weight="bold">
    <xsl:apply-templates/>
  </fo:inline-sequence>
</xsl:template>
]]></eg>

<note><p>Examples in this document use the <code>fo:</code> prefix for
the namespace <code>&XSLFO.ns;</code>, which is
the namespace of the formatting objects defined in <bibref
ref="XSL"/>.</p></note>

<p>As described next, the <code>xsl:apply-templates</code> element
recursively processes the children of the source element.</p>

</div2>

<div2>
<head>Applying Template Rules</head>

<e:element-syntax name="apply-templates">
  <e:in-category name="instruction"/>
  <e:attribute name="select">
    <e:data-type name="node-set-expression"/>
  </e:attribute>
  <e:attribute name="mode">
    <e:data-type name="qname"/>
  </e:attribute>
  <e:choice repeat="zero-or-more">
    <e:element name="sort"/>
    <e:element name="with-param"/>
  </e:choice>
</e:element-syntax>

<p>This example creates a block for a <code>chapter</code> element and
then processes its immediate children.</p>

<eg><![CDATA[<xsl:template match="chapter">
  <fo:block>
    <xsl:apply-templates/>
  </fo:block>
</xsl:template>]]></eg>

<p>In the absence of a <code>select</code> attribute, the
<code>xsl:apply-templates</code> instruction processes all of the
children of the current node, including text nodes.  However, text
nodes that have been stripped as specified in <specref ref="strip"/>
will not be processed.  If stripping of whitespace nodes has not been
enabled for an element, then all whitespace in the content of the
element will be processed as text, and thus whitespace
between child elements will count in determining the position of a
child element as returned by the <xfunction>position</xfunction>
function.</p>

<p>A <code>select</code> attribute can be used to process nodes
selected by an expression instead of processing all children.  The
value of the <code>select</code> attribute is an <termref
def="dt-expression">expression</termref>.  The expression must
evaluate to a node-set.  The selected set of nodes is processed in
document order, unless a sorting specification is present (see
<specref ref="sorting"/>).  The following example processes all of the
<code>author</code> children of the <code>author-group</code>:</p>

<eg><![CDATA[<xsl:template match="author-group">
  <fo:inline-sequence>
    <xsl:apply-templates select="author"/>
  </fo:inline-sequence>
</xsl:template>]]></eg>

<p>The following example processes all of the <code>given-name</code>s
of the <code>author</code>s that are children of
<code>author-group</code>:</p>

<eg><![CDATA[<xsl:template match="author-group">
  <fo:inline-sequence>
    <xsl:apply-templates select="author/given-name"/>
  </fo:inline-sequence>
</xsl:template>]]></eg>

<p>This example processes all of the <code>heading</code> descendant
elements of the <code>book</code> element.</p>

<eg><![CDATA[<xsl:template match="book">
  <fo:block>
    <xsl:apply-templates select=".//heading"/>
  </fo:block>
</xsl:template>]]></eg>

<p>It is also possible to process elements that are not descendants of
the current node.  This example assumes that a <code>department</code>
element has <code>group</code> children and <code>employee</code>
descendants. It finds an employee's department and then processes
the <code>group</code> children of the <code>department</code>.</p>

<eg><![CDATA[<xsl:template match="employee">
  <fo:block>
    Employee <xsl:apply-templates select="name"/> belongs to group
    <xsl:apply-templates select="ancestor::department/group"/>
  </fo:block>
</xsl:template>]]></eg>

<p>Multiple <code>xsl:apply-templates</code> elements can be used within a
single template to do simple reordering.  The following example
creates two HTML tables. The first table is filled with domestic sales
while the second table is filled with foreign sales.</p>

<eg><![CDATA[<xsl:template match="product">
  <table>
    <xsl:apply-templates select="sales/domestic"/>
  </table>
  <table>
    <xsl:apply-templates select="sales/foreign"/>
  </table>
</xsl:template>]]></eg>

<note>

<p>It is possible for there to be two matching descendants where one
is a descendant of the other.  This case is not treated specially:
both descendants will be processed as usual. For example, given a
source document</p>

<eg><![CDATA[<doc><div><div></div></div></doc>]]></eg>

<p>the rule</p>

<eg><![CDATA[<xsl:template match="doc">
  <xsl:apply-templates select=".//div"/>
</xsl:template>]]></eg>

<p>will process both the outer <code>div</code> and inner <code>div</code>
elements.</p>

</note>

<note><p>Typically, <code>xsl:apply-templates</code> is used to
process only nodes that are descendants of the current node.  Such use
of <code>xsl:apply-templates</code> cannot result in non-terminating
processing loops.  However, when <code>xsl:apply-templates</code> is
used to process elements that are not descendants of the current node,
the possibility arises of non-terminating loops. For example,</p>

<eg role="error"><![CDATA[<xsl:template match="foo">
  <xsl:apply-templates select="."/>
</xsl:template>]]></eg>

<p>Implementations may be able to detect such loops in some cases, but
the possibility exists that a stylesheet may enter a non-terminating
loop that an implementation is unable to detect. This may present a
denial of service security risk.</p></note>

</div2>

<div2 id="conflict">
<head>Conflict Resolution for Template Rules</head>

<p>It is possible for a source node to match more than one template
rule. The template rule to be used is determined as follows:</p>

<olist>

<item><p>First, all matching template rules that have lower <termref
def="dt-import-precedence">import precedence</termref> than the
matching template rule or rules with the highest import precedence are
eliminated from consideration.</p></item>

<item><p>Next, all matching template rules that have lower priority
than the matching template rule or rules with the highest priority are
eliminated from consideration.  The priority of a template rule is
specified by the <code>priority</code> attribute on the template rule.
The value of this must be a real number (positive or negative),
matching the production <xnt href="&XPath;#NT-Number">Number</xnt>
with an optional leading minus sign (<code>-</code>).  <termdef
id="dt-default-priority" term="Default Priority">The <term>default
priority</term> is computed as follows:</termdef></p>

<ulist>

<item><p>If the pattern contains multiple alternatives separated by
<code>|</code>, then it is treated equivalently to a set of template
rules, one for each alternative.</p></item>

<item><p>If the pattern has the form of a <xnt
href="&XMLNames;#NT-QName">QName</xnt> preceded by a <nt
def="NT-ChildOrAttributeAxisSpecifier">ChildOrAttributeAxisSpecifier</nt>
or has the form
<code>processing-instruction(</code><xnt href="&XPath;#NT-Literal"
>Literal</xnt><code>)</code> preceded by a <nt
def="NT-ChildOrAttributeAxisSpecifier">ChildOrAttributeAxisSpecifier</nt>,
then the priority is 0.</p></item>

<item><p>If the pattern has the form <xnt
href="&XMLNames;#NT-NCName">NCName</xnt><code>:*</code> preceded by a
<nt
def="NT-ChildOrAttributeAxisSpecifier">ChildOrAttributeAxisSpecifier</nt>,
then the priority is -0.25.</p></item>

<item><p>Otherwise, if the pattern consists of just a <xnt
href="&XPath;#NT-NodeTest">NodeTest</xnt> preceded by a <nt
def="NT-ChildOrAttributeAxisSpecifier">ChildOrAttributeAxisSpecifier</nt>,
then the priority is -0.5.</p></item>

<item><p>Otherwise, the priority is 0.5.</p></item>

</ulist>

<p>Thus, the most common kind of pattern (a pattern that tests for a
node with a particular type and a particular expanded-name) has
priority 0. The next less specific kind of pattern (a pattern that
tests for a node with a particular type and an expanded-name with a
particular namespace URI) has priority -0.25.  Patterns less specific
than this (patterns that just tests for nodes with particular types)
have priority -0.5.  Patterns more specific than the most common kind
of pattern have priority 0.5.</p>

</item>

</olist>

<p>It is an error if this leaves more than one matching template
rule.  An XSLT processor may signal the error; if it does not signal
the error, it must recover by choosing, from amongst the matching
template rules that are left, the one that occurs last in the
stylesheet.</p>

</div2>

<div2 id="apply-imports">
<head>Overriding Template Rules</head>

<e:element-syntax name="apply-imports">
  <e:in-category name="instruction"/>
  <e:empty/>
</e:element-syntax>

<p>A template rule that is being used to override a template rule in
an imported stylesheet (see <specref ref="conflict"/>) can use the
<code>xsl:apply-imports</code> element to invoke the overridden
template rule.</p>

<p><termdef id="dt-current-template-rule" term="Current Template
Rule">At any point in the processing of a stylesheet, there is a
<term>current template rule</term>.  Whenever a template rule is
chosen by matching a pattern, the template rule becomes the current
template rule for the instantiation of the rule's template. When an
<code>xsl:for-each</code> element is instantiated, the current
template rule becomes null for the instantiation of the content of the
<code>xsl:for-each</code> element.</termdef></p>

<p><code>xsl:apply-imports</code> processes the current node using
only template rules that were imported into the stylesheet element
containing the current template rule; the node is processed in the
current template rule's mode.  It is an error if
<code>xsl:apply-imports</code> is instantiated when the current
template rule is null.</p>

<p>For example, suppose the stylesheet <code>doc.xsl</code> contains a
template rule for <code>example</code> elements:</p>

<eg><![CDATA[<xsl:template match="example">
  <pre><xsl:apply-templates/></pre>
</xsl:template>]]></eg>

<p>Another stylesheet could import <code>doc.xsl</code> and modify the
treatment of <code>example</code> elements as follows:</p>

<eg><![CDATA[<xsl:import href="doc.xsl"/>

<xsl:template match="example">
  <div style="border: solid red">
     <xsl:apply-imports/>
  </div>
</xsl:template>]]></eg>

<p>The combined effect would be to transform an <code>example</code>
into an element of the form:</p>

<eg><![CDATA[<div style="border: solid red"><pre>...</pre></div>]]></eg>

</div2>

<div2 id="modes">
<head>Modes</head>

<p>Modes allow an element to be processed multiple times, each time
producing a different result.</p>

<p>Both <code>xsl:template</code> and <code>xsl:apply-templates</code>
have an optional <code>mode</code> attribute.  The value of the
<code>mode</code> attribute is a <xnt
href="&XMLNames;#NT-QName">QName</xnt>, which is expanded as described
in <specref ref="qname"/>. If <code>xsl:template</code> does not have
a <code>match</code> attribute, it must not have a <code>mode</code>
attribute.  If an <code>xsl:apply-templates</code> element has a
<code>mode</code> attribute, then it applies only to those template
rules from <code>xsl:template</code> elements that have a
<code>mode</code> attribute with the same value; if an
<code>xsl:apply-templates</code> element does not have a
<code>mode</code> attribute, then it applies only to those template
rules from <code>xsl:template</code> elements that do not have a
<code>mode</code> attribute.</p>

</div2>

<div2 id="built-in-rule">
<head>Built-in Template Rules</head>

<p>There is a built-in template rule to allow recursive processing to
continue in the absence of a successful pattern match by an explicit
template rule in the stylesheet.  This template rule applies to both
element nodes and the root node.  The following shows the equivalent
of the built-in template rule:</p>

<eg><![CDATA[<xsl:template match="*|/">
  <xsl:apply-templates/>
</xsl:template>]]></eg>

<p>There is also a built-in template rule for each mode, which allows
recursive processing to continue in the same mode in the absence of a
successful pattern match by an explicit template rule in the
stylesheet.  This template rule applies to both element nodes and the
root node.  The following shows the equivalent of the built-in
template rule for mode <code><var>m</var></code>.</p>

<eg>&lt;xsl:template match="*|/" mode="<var>m</var>">
  &lt;xsl:apply-templates mode="<var>m</var>"/>
&lt;/xsl:template></eg>

<p>There is also a built-in template rule for text and attribute nodes
that copies text through:</p>

<eg><![CDATA[<xsl:template match="text()|@*">
  <xsl:value-of select="."/>
</xsl:template>]]></eg>

<p>The built-in template rule for processing instructions and comments
is to do nothing.</p>

<eg><![CDATA[<xsl:template match="processing-instruction()|comment()"/>]]></eg>

<p>The built-in template rule for namespace nodes is also to do
nothing. There is no pattern that can match a namespace node; so, the
built-in template rule is the only template rule that is applied for
namespace nodes.</p>

<p>The built-in template rules are treated as if they were imported
implicitly before the stylesheet and so have lower <termref
def="dt-import-precedence">import precedence</termref> than all other
template rules.  Thus, the author can override a built-in template
rule by including an explicit template rule.</p>

</div2>


</div1>

<div1 id="named-templates">
<head>Named Templates</head>

<e:element-syntax name="call-template">
  <e:in-category name="instruction"/>
  <e:attribute name="name" required="yes">
    <e:data-type name="qname"/>
  </e:attribute>
  <e:element repeat="zero-or-more" name="with-param"/>
</e:element-syntax>

<p>Templates can be invoked by name.  An <code>xsl:template</code>
element with a <code>name</code> attribute specifies a named template.
The value of the <code>name</code> attribute is a <xnt
href="&XMLNames;#NT-QName">QName</xnt>, which is expanded as described
in <specref ref="qname"/>. If an <code>xsl:template</code> element has
a <code>name</code> attribute, it may, but need not, also have a
<code>match</code> attribute.  An <code>xsl:call-template</code>
element invokes a template by name; it has a required
<code>name</code> attribute that identifies the template to be
invoked.  Unlike <code>xsl:apply-templates</code>,
<code>xsl:call-template</code> does not change the current node or the
current node list.</p>

<p>The <code>match</code>, <code>mode</code> and <code>priority</code> attributes on an
<code>xsl:template</code> element do not affect whether the template
is invoked by an <code>xsl:call-template</code> element.  Similarly,
the <code>name</code> attribute on an <code>xsl:template</code>
element does not affect whether the template is invoked by an
<code>xsl:apply-templates</code> element.</p>

<p>It is an error if a stylesheet contains more than one template with
the same name and same <termref def="dt-import-precedence">import
precedence</termref>.</p>

</div1>


<div1>
<head>Creating the Result Tree</head>

<p>This section describes instructions that directly create nodes in
the result tree.</p>

<div2>
<head>Creating Elements and Attributes</head>

<div3 id="literal-result-element">
<head>Literal Result Elements</head>

<p>In a template, an element in the stylesheet that does not belong to
the XSLT namespace and that is not an extension element (see <specref
ref="extension-element"/>) is instantiated to create an element node
with the same <xtermref
href="&XPath;#dt-expanded-name">expanded-name</xtermref>.  The content
of the element is a template, which is instantiated to give the
content of the created element node. The created element node will
have the attribute nodes that were present on the element node in the
stylesheet tree, other than attributes with names in the XSLT
namespace.</p>

<p>The created element node will also have a copy of the namespace
nodes that were present on the element node in the stylesheet tree
with the exception of any namespace node whose string-value is the
XSLT namespace URI (<code>&XSLT.ns;</code>), a
namespace URI declared as an extension namespace (see <specref
ref="extension-element"/>), or a namespace URI designated as an
excluded namespace.  A namespace URI is designated as an excluded
namespace by using an <code>exclude-result-prefixes</code> attribute
on an <code>xsl:stylesheet</code> element or an
<code>xsl:exclude-result-prefixes</code> attribute on a literal result
element.  The value of both these attributes is a whitespace-separated
list of namespace prefixes. The namespace bound to each of the
prefixes is designated as an excluded namespace.  It is an error if
there is no namespace bound to the prefix on the element bearing the
<code>exclude-result-prefixes</code> or
<code>xsl:exclude-result-prefixes</code> attribute.  The default
namespace (as declared by <code>xmlns</code>) may be designated as an
excluded namespace by including <code>#default</code> in the list of
namespace prefixes.  The designation of a namespace as an excluded
namespace is effective within the subtree of the stylesheet rooted at
the element bearing the <code>exclude-result-prefixes</code> or
<code>xsl:exclude-result-prefixes</code> attribute;
a subtree rooted at an <code>xsl:stylesheet</code> element
does not include any stylesheets imported or included by children
of that <code>xsl:stylesheet</code> element.</p>

<note><p>When a stylesheet uses a namespace declaration only for the
purposes of addressing the source tree, specifying the prefix in the
<code>exclude-result-prefixes</code> attribute will avoid superfluous
namespace declarations in the result tree.</p></note>

<p>The value of an attribute of a literal result element is
interpreted as an <termref def="dt-attribute-value-template">attribute
value template</termref>: it can contain expressions contained
in curly braces (<code>{}</code>).</p>

<p><termdef id="dt-literal-namespace-uri" term="Literal Namespace
URI">A namespace URI in the stylesheet tree that is being used to
specify a namespace URI in the result tree is called a <term>literal
namespace URI</term>.</termdef> This applies to:</p>

<ulist>

<item><p>the namespace URI in the expanded-name of a literal
result element in the stylesheet</p></item>

<item><p>the namespace URI in the expanded-name of an attribute
specified on a literal result element in the stylesheet</p></item>

<item><p>the string-value of a namespace node on a literal result
element in the stylesheet</p></item>

</ulist>

<e:element-syntax name="namespace-alias">
  <e:in-category name="top-level-element"/>
  <e:attribute name="stylesheet-prefix" required="yes">
    <e:data-type name="prefix"/>
    <e:constant value="#default"/>
  </e:attribute>
  <e:attribute name="result-prefix" required="yes">
    <e:data-type name="prefix"/>
    <e:constant value="#default"/>
  </e:attribute>
  <e:empty/>
</e:element-syntax>

<p><termdef id="dt-alias" term="Alias">A stylesheet can use the
<code>xsl:namespace-alias</code> element to declare that one namespace
URI is an <term>alias</term> for another namespace URI.</termdef> When
a <termref def="dt-literal-namespace-uri">literal namespace
URI</termref> has been declared to be an alias for another namespace
URI, then the namespace URI in the result tree will be the namespace
URI that the literal namespace URI is an alias for, instead of the
literal namespace URI itself.  The <code>xsl:namespace-alias</code>
element declares that the namespace URI bound to the prefix specified
by the <code>stylesheet-prefix</code> attribute is an alias for the
namespace URI bound to the prefix specified by the
<code>result-prefix</code> attribute.  Thus, the
<code>stylesheet-prefix</code> attribute specifies the namespace URI
that will appear in the stylesheet, and the
<code>result-prefix</code> attribute specifies the corresponding
namespace URI that will appear in the result tree.  The default
namespace (as declared by <code>xmlns</code>) may be specified by
using <code>#default</code> instead of a prefix.  If a namespace URI
is declared to be an alias for multiple different namespace URIs, then
the declaration with the highest <termref
def="dt-import-precedence">import precedence</termref> is used. It is
an error if there is more than one such declaration.  An XSLT
processor may signal the error; if it does not signal the error, it
must recover by choosing, from amongst the declarations with the
highest import precedence, the one that occurs last in the
stylesheet.</p>

<p>When literal result elements are being used to create element,
attribute, or namespace nodes that use the XSLT namespace URI, the
stylesheet must use an alias.  For example, the stylesheet</p>

<eg>&lt;xsl:stylesheet
  version="1.0"
  xmlns:xsl="&XSLT.ns;"
  xmlns:fo="&XSLFO.ns;"
  xmlns:axsl="&XSLTA.ns;"><![CDATA[

<xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/>

<xsl:template match="/">
  <axsl:stylesheet>
    <xsl:apply-templates/>
  </axsl:stylesheet>
</xsl:template>

<xsl:template match="block">
  <axsl:template match="{.}">
     <fo:block><axsl:apply-templates/></fo:block>
  </axsl:template>
</xsl:template>

</xsl:stylesheet>]]></eg>

<p>will generate an XSLT stylesheet from a document of the form:</p>

<eg><![CDATA[<elements>
<block>p</block>
<block>h1</block>
<block>h2</block>
<block>h3</block>
<block>h4</block>
</elements>]]></eg>

<note><p>It may be necessary also to use aliases for namespaces other
than the XSLT namespace URI.  For example, literal result elements
belonging to a namespace dealing with digital signatures might cause
XSLT stylesheets to be mishandled by general-purpose security
software; using an alias for the namespace would avoid the possibility
of such mishandling.</p></note>

</div3>

<div3>
<head>Creating Elements with <code>xsl:element</code></head>

<e:element-syntax name="element">
  <e:in-category name="instruction"/>
  <e:attribute name="name" required="yes">
    <e:attribute-value-template>
      <e:data-type name="qname"/>
    </e:attribute-value-template>
  </e:attribute>
  <e:attribute name="namespace">
    <e:attribute-value-template>
      <e:data-type name="uri-reference"/>
    </e:attribute-value-template>
  </e:attribute>
  <e:attribute name="use-attribute-sets">
    <e:data-type name="qnames"/>
  </e:attribute>
  <e:model name="template"/>
</e:element-syntax>

<p>The <code>xsl:element</code> element allows an element to be
created with a computed name.  The <xtermref
href="&XPath;#dt-expanded-name">expanded-name</xtermref> of the
element to be created is specified by a required <code>name</code>
attribute and an optional <code>namespace</code> attribute.  The
content of the <code>xsl:element</code> element is a template for the
attributes and children of the created element.</p>

<p>The <code>name</code> attribute is interpreted as an <termref
def="dt-attribute-value-template">attribute value template</termref>.
It is an error if the string that results from instantiating the
attribute value template is not a <xnt
href="&XMLNames;#NT-QName">QName</xnt>.  An XSLT processor may signal
the error; if it does not signal the error, then it must recover
by making the the result of instantiating the <code>xsl:element</code>
element be the sequence of nodes created by instantiating
the content of the  <code>xsl:element</code> element, excluding
any initial attribute nodes. If the <code>namespace</code> attribute is
not present then the <xnt href="&XMLNames;#NT-QName">QName</xnt> is
expanded into an expanded-name using the namespace declarations in
effect for the <code>xsl:element</code> element, including any default
namespace declaration.</p>

<p>If the <code>namespace</code> attribute is present, then it also is
interpreted as an <termref def="dt-attribute-value-template">attribute
value template</termref>. The string that results from instantiating
the attribute value template should be a URI reference.  It is not an
error if the string is not a syntactically legal URI reference.  If
the string is empty, then the expanded-name of the element has a null
namespace URI.  Otherwise, the string is used as the namespace URI of
the expanded-name of the element to be created. The local part of the
<xnt href="&XMLNames;#NT-QName">QName</xnt> specified by the
<code>name</code> attribute is used as the local part of the
expanded-name of the element to be created.</p>

<p>XSLT processors may make use of the prefix of the <xnt
href="&XMLNames;#NT-QName">QName</xnt> specified in the
<code>name</code> attribute when selecting the prefix used for
outputting the created element as XML; however, they are not required
to do so.</p>

</div3>

<div3 id="creating-attributes">
<head>Creating Attributes with <code>xsl:attribute</code></head>

<e:element-syntax name="attribute">
  <e:in-category name="instruction"/>
  <e:attribute name="name" required="yes">
    <e:attribute-value-template>
      <e:data-type name="qname"/>
    </e:attribute-value-template>
  </e:attribute>
  <e:attribute name="namespace">
    <e:attribute-value-template>
      <e:data-type name="uri-reference"/>
    </e:attribute-value-template>
  </e:attribute>
  <e:model name="template"/>
</e:element-syntax>

<p>The <code>xsl:attribute</code> element can be used to add
attributes to result elements whether created by literal result
elements in the stylesheet or by instructions such as
<code>xsl:element</code>. The <xtermref
href="&XPath;#dt-expanded-name">expanded-name</xtermref> of the
attribute to be created is specified by a required <code>name</code>
attribute and an optional <code>namespace</code> attribute.
Instantiating an <code>xsl:attribute</code> element adds an attribute
node to the containing result element node. The content of the
<code>xsl:attribute</code> element is a template for the value of the
created attribute.</p>

<p>The <code>name</code> attribute is interpreted as an <termref
def="dt-attribute-value-template">attribute value template</termref>.
It is an error if the string that results from instantiating the
attribute value template is not a <xnt
href="&XMLNames;#NT-QName">QName</xnt> or is the string
<code>xmlns</code>.  An XSLT processor may signal the error; if it
does not signal the error, it must recover by not adding the attribute
to the result tree. If the <code>namespace</code> attribute is not
present, then the <xnt href="&XMLNames;#NT-QName">QName</xnt> is
expanded into an expanded-name using the namespace declarations in
effect for the <code>xsl:attribute</code> element, <emph>not</emph>
including any default namespace declaration.</p>

<p>If the <code>namespace</code> attribute is present, then it also is
interpreted as an <termref def="dt-attribute-value-template">attribute
value template</termref>. The string that results from instantiating
it should be a URI reference.  It is not an error if the string is not
a syntactically legal URI reference.  If the string is empty, then the
expanded-name of the attribute has a null namespace URI.  Otherwise,
the string is used as the namespace URI of the expanded-name of the
attribute to be created. The local part of the <xnt
href="&XMLNames;#NT-QName">QName</xnt> specified by the
<code>name</code> attribute is used as the local part of the
expanded-name of the attribute to be created.</p>

<p>XSLT processors may make use of the prefix of the <xnt
href="&XMLNames;#NT-QName">QName</xnt> specified in the
<code>name</code> attribute when selecting the prefix used for
outputting the created attribute as XML; however, they are not
required to do so and, if the prefix is <code>xmlns</code>, they must
not do so. Thus, although it is not an error to do:</p>

<eg>&lt;xsl:attribute name="xmlns:xsl" namespace="whatever">&XSLT.ns;&lt;/xsl:attribute></eg>

<p>it will not result in a namespace declaration being output.</p>

<p>Adding an attribute to an element replaces any existing attribute
of that element with the same expanded-name.</p>

<p>The following are all errors:</p>

<ulist>

<item><p>Adding an attribute to an element after children have been
added to it; implementations may either signal the error or ignore the
attribute.</p></item>

<item><p>Adding an attribute to a node that is not an element;
implementations may either signal the error or ignore the
attribute.</p></item>

<item><p>Creating nodes other than text nodes during the
instantiation of the content of the <code>xsl:attribute</code>
element; implementations may either signal the error or ignore the
offending nodes.</p></item>

</ulist>

<note><p>When an <code>xsl:attribute</code> contains a text node with
a newline, then the XML output must contain a character reference.
For example,</p>

<eg><![CDATA[<xsl:attribute name="a">x
y</xsl:attribute>]]></eg>

<p>will result in the output</p>

<eg><![CDATA[a="x&#xA;y"]]></eg>

<p>(or with any equivalent character reference). The XML output cannot
be</p>

<eg><![CDATA[a="x
y"]]></eg>

<p>This is because XML 1.0 requires newline characters in attribute
values to be normalized into spaces but requires character references
to newline characters not to be normalized.  The attribute values in
the data model represent the attribute value after normalization.  If
a newline occurring in an attribute value in the tree were output as a
newline character rather than as character reference, then the
attribute value in the tree created by reparsing the XML would contain
a space not a newline, which would mean that the tree had not been
output correctly.</p></note>

</div3>

<div3 id="attribute-sets">

<head>Named Attribute Sets</head>

<e:element-syntax name="attribute-set">
  <e:in-category name="top-level-element"/>
  <e:attribute name="name" required="yes">
    <e:data-type name="qname"/>
  </e:attribute>
  <e:attribute name="use-attribute-sets">
    <e:data-type name="qnames"/>
  </e:attribute>
  <e:element repeat="zero-or-more" name="attribute"/>
</e:element-syntax>

<p>The <code>xsl:attribute-set</code> element defines a named set of
attributes.  The <code>name</code> attribute specifies the name of the
attribute set.  The value of the <code>name</code> attribute is a <xnt
href="&XMLNames;#NT-QName">QName</xnt>, which is expanded as described
in <specref ref="qname"/>. The content of the <code>xsl:attribute-set</code>
element consists of zero or more <code>xsl:attribute</code> elements
that specify the attributes in the set.</p>

<p>Attribute sets are used by specifying a
<code>use-attribute-sets</code> attribute on <code>xsl:element</code>,
<code>xsl:copy</code> (see <specref ref="copying"/>) or
<code>xsl:attribute-set</code> elements.  The value of the
<code>use-attribute-sets</code> attribute is a whitespace-separated
list of names of attribute sets.  Each name is specified as a <xnt
href="&XMLNames;#NT-QName">QName</xnt>, which is expanded as described
in <specref ref="qname"/>.  Specifying a
<code>use-attribute-sets</code> attribute is equivalent to adding
<code>xsl:attribute</code> elements for each of the attributes in each
of the named attribute sets to the beginning of the content of the
element with the <code>use-attribute-sets</code> attribute, in the
same order in which the names of the attribute sets are specified in
the <code>use-attribute-sets</code> attribute.  It is an error if use
of <code>use-attribute-sets</code> attributes on
<code>xsl:attribute-set</code> elements causes an attribute set to
directly or indirectly use itself.</p>

<p>Attribute sets can also be used by specifying an
<code>xsl:use-attribute-sets</code> attribute on a literal result
element.  The value of the <code>xsl:use-attribute-sets</code>
attribute is a whitespace-separated list of names of attribute sets.
The <code>xsl:use-attribute-sets</code> attribute has the same effect
as the <code>use-attribute-sets</code> attribute on
<code>xsl:element</code> with the additional rule that attributes
specified on the literal result element itself are treated as if they
were specified by <code>xsl:attribute</code> elements before any
actual <code>xsl:attribute</code> elements but after any
<code>xsl:attribute</code> elements implied by the
<code>xsl:use-attribute-sets</code> attribute.  Thus, for a literal
result element, attributes from attribute sets named in an
<code>xsl:use-attribute-sets</code> attribute will be added first, in
the order listed in the attribute; next, attributes specified on the
literal result element will be added; finally, any attributes
specified by <code>xsl:attribute</code> elements will be added.  Since
adding an attribute to an element replaces any existing attribute of
that element with the same name, this means that attributes specified
in attribute sets can be overridden by attributes specified on the
literal result element itself.</p>

<p>The template within each <code>xsl:attribute</code> element in an
<code>xsl:attribute-set</code> element is instantiated each time the
attribute set is used; it is instantiated using the same current node
and current node list as is used for instantiating the element bearing
the <code>use-attribute-sets</code> or
<code>xsl:use-attribute-sets</code> attribute. However, it is the
position in the stylesheet of the <code>xsl:attribute</code> element
rather than of the element bearing the <code>use-attribute-sets</code>
or <code>xsl:use-attribute-sets</code> attribute that determines which
variable bindings are visible (see <specref ref="variables"/>); thus,
only variables and parameters declared by <termref
def="dt-top-level">top-level</termref> <code>xsl:variable</code> and
<code>xsl:param</code> elements are visible.</p>

<p>The following example creates a named attribute set
<code>title-style</code> and uses it in a template rule.</p>

<eg><![CDATA[<xsl:template match="chapter/heading">
  <fo:block quadding="start" xsl:use-attribute-sets="title-style">
    <xsl:apply-templates/>
  </fo:block>
</xsl:template>

<xsl:attribute-set name="title-style">
  <xsl:attribute name="font-size">12pt</xsl:attribute>
  <xsl:attribute name="font-weight">bold</xsl:attribute>
</xsl:attribute-set>]]></eg>

<p>Multiple definitions of an attribute set with the same
expanded-name are merged.  An attribute from a definition that has
higher <termref def="dt-import-precedence">import precedence</termref>
takes precedence over an attribute from a definition that has lower
<termref def="dt-import-precedence">import precedence</termref>.  It
is an error if there are two attribute sets that have the same
expanded-name and equal import precedence and that both contain
the same attribute, unless there is a definition of the attribute set
with higher <termref def="dt-import-precedence">import
precedence</termref> that also contains the attribute.  An XSLT
processor may signal the error; if it does not signal the error, it
must recover by choosing from amongst the definitions that specify the
attribute that have the highest import precedence the one that was
specified last in the stylesheet.  Where the attributes in an
attribute set were specified is relevant only in merging the
attributes into the attribute set; it makes no difference when the
attribute set is used.</p>

</div3>

</div2>

<div2>

<head>Creating Text</head>

<p>A template can also contain text nodes.  Each text node in a
template remaining after whitespace has been stripped as specified in
<specref ref="strip"/> will create a text node with the same
string-value in the result tree.  Adjacent text nodes in the result
tree are automatically merged.</p>

<p>Note that text is processed at the tree level. Thus, markup of
<code>&amp;lt;</code> in a template will be represented in the
stylesheet tree by a text node that includes the character
<code>&lt;</code>. This will create a text node in the result tree
that contains a <code>&lt;</code> character, which will be represented
by the markup <code>&amp;lt;</code> (or an equivalent character
reference) when the result tree is externalized as an XML document
(unless output escaping is disabled as described in <specref
ref="disable-output-escaping"/>).</p>

<e:element-syntax name="text">
  <e:in-category name="instruction"/>
  <e:attribute name="disable-output-escaping">
    <e:constant value="yes"/>
    <e:constant value="no"/>
  </e:attribute>
  <e:text/>  
</e:element-syntax>

<p>Literal data characters may also be wrapped in an
<code>xsl:text</code> element.  This wrapping may change what
whitespace characters are stripped (see <specref ref="strip"/>) but
does not affect how the characters are handled by the XSLT processor
thereafter.</p>

<note><p>The <code>xml:lang</code> and <code>xml:space</code>
attributes are not treated specially by XSLT. In particular,</p>

<ulist>
<item><p>it is the responsibility of the stylesheet author explicitly
to generate any <code>xml:lang</code> or <code>xml:space</code>
attributes that are needed in the result;</p></item>

<item><p>specifying an <code>xml:lang</code> or <code>xml:space</code>
attribute on an element in the XSLT namespace will not cause any
<code>xml:lang</code> or <code>xml:space</code> attributes to appear
in the result.</p></item>
</ulist>
</note>

</div2>


<div2>
<head>Creating Processing Instructions</head>


<e:element-syntax name="processing-instruction">
  <e:in-category name="instruction"/>
  <e:attribute name="name" required="yes">
    <e:attribute-value-template>
      <e:data-type name="ncname"/>
    </e:attribute-value-template>
  </e:attribute>
  <e:model name="template"/>
</e:element-syntax>

<p>The <code>xsl:processing-instruction</code> element is instantiated
to create a processing instruction node.  The content of the
<code>xsl:processing-instruction</code> element is a template for the
string-value of the processing instruction node.  The
<code>xsl:processing-instruction</code> element has a required
<code>name</code> attribute that specifies the name of the processing
instruction node.  The value of the <code>name</code> attribute is
interpreted as an <termref def="dt-attribute-value-template">attribute
value template</termref>.</p>

<p>For example, this</p>

<eg><![CDATA[<xsl:processing-instruction name="xml-stylesheet">href="book.css" type="text/css"</xsl:processing-instruction>]]></eg>

<p>would create the processing instruction</p>

<eg><![CDATA[<?xml-stylesheet href="book.css" type="text/css"?>]]></eg>

<p>It is an error if the string that results from instantiating the
<code>name</code> attribute is not both an <xnt
href="&XMLNames;#NT-NCName">NCName</xnt> and a <xnt
href="&XML;#NT-PITarget">PITarget</xnt>.  An XSLT processor may signal
the error; if it does not signal the error, it must recover by not
adding the processing instruction to the result tree.</p>

<note><p>This means that <code>xsl:processing-instruction</code>
cannot be used to output an XML declaration.  The
<code>xsl:output</code> element should be used instead (see <specref
ref="output"/>).</p></note>

<p>It is an error if instantiating the content of
<code>xsl:processing-instruction</code> creates nodes other than
text nodes.  An XSLT processor may signal the error; if it does not
signal the error, it must recover by ignoring the offending nodes
together with their content.</p>

<p>It is an error if the result of instantiating the content of the
<code>xsl:processing-instruction</code> contains the string
<code>?&gt;</code>.  An XSLT processor may signal the error; if it does
not signal the error, it must recover by inserting a space after any
occurrence of <code>?</code> that is followed by a <code>&gt;</code>.</p>

</div2>

<div2>
<head>Creating Comments</head>

<e:element-syntax name="comment">
  <e:in-category name="instruction"/>
  <e:model name="template"/>
</e:element-syntax>

<p>The <code>xsl:comment</code> element is instantiated to create a
comment node in the result tree.  The content of the
<code>xsl:comment</code> element is a template for the string-value of
the comment node.</p>

<p>For example, this</p>

<eg><![CDATA[<xsl:comment>This file is automatically generated. Do not edit!</xsl:comment>]]></eg>

<p>would create the comment</p>

<eg><![CDATA[<!--This file is automatically generated. Do not edit!-->]]></eg>

<p>It is an error if instantiating the content of
<code>xsl:comment</code> creates nodes other than text nodes.  An
XSLT processor may signal the error; if it does not signal the error,
it must recover by ignoring the offending nodes together with their
content.</p>

<p>It is an error if the result of instantiating the content of the
<code>xsl:comment</code> contains the string <code>--</code> or ends
with <code>-</code>.  An XSLT processor may signal the error; if it
does not signal the error, it must recover by inserting a space after
any occurrence of <code>-</code> that is followed by another
<code>-</code> or that ends the comment.</p>

</div2>

<div2 id="copying">
<head>Copying</head>

<e:element-syntax name="copy">
  <e:in-category name="instruction"/>
  <e:attribute name="use-attribute-sets">
    <e:data-type name="qnames"/>
  </e:attribute>
  <e:model name="template"/>
</e:element-syntax>

<p>The <code>xsl:copy</code> element provides an easy way of copying
the current node. Instantiating the <code>xsl:copy</code> element
creates a copy of the current node.  The namespace nodes of the
current node are automatically copied as well, but the attributes and
children of the node are not automatically copied.  The content of the
<code>xsl:copy</code> element is a template for the attributes and
children of the created node; the content is instantiated only for
nodes of types that can have attributes or children (i.e. root
nodes and element nodes).</p>

<p>The <code>xsl:copy</code> element may have a
<code>use-attribute-sets</code> attribute (see <specref
ref="attribute-sets"/>). This is used only when copying element
nodes.</p>

<p>The root node is treated specially because the root node of the
result tree is created implicitly.  When the current node is the root
node, <code>xsl:copy</code> will not create a root node, but will just
use the content template.</p>

<p>For example, the identity transformation can be written using
<code>xsl:copy</code> as follows:</p>

<eg><![CDATA[<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>]]></eg>

<p>When the current node is an attribute, then if it would be an error
to use <code>xsl:attribute</code> to create an attribute with the same
name as the current node, then it is also an error to use
<code>xsl:copy</code> (see <specref ref="creating-attributes"/>).</p>

<p>The following example shows how <code>xml:lang</code> attributes
can be easily copied through from source to result. If a stylesheet
defines the following named template:</p>

<eg><![CDATA[<xsl:template name="apply-templates-copy-lang">
 <xsl:for-each select="@xml:lang">
   <xsl:copy/>
 </xsl:for-each>
 <xsl:apply-templates/>
</xsl:template>]]></eg>

<p>then it can simply do</p>

<eg><![CDATA[<xsl:call-template name="apply-templates-copy-lang"/>]]></eg>

<p>instead of</p>

<eg><![CDATA[<xsl:apply-templates/>]]></eg>

<p>when it wants to copy the <code>xml:lang</code> attribute.</p>

</div2>

<div2>
<head>Computing Generated Text</head>

<p>Within a template, the <code>xsl:value-of</code> element can be
used to compute generated text, for example by extracting text from
the source tree or by inserting the value of a variable.  The
<code>xsl:value-of</code> element does this with an <termref
def="dt-expression">expression</termref> that is specified as the
value of the <code>select</code> attribute.  Expressions can
also be used inside attribute values of literal result elements by
enclosing the expression in curly braces (<code>{}</code>).</p>

<div3 id="value-of">
<head>Generating Text with <code>xsl:value-of</code></head>

<e:element-syntax name="value-of">
  <e:in-category name="instruction"/>
  <e:attribute name="select" required="yes">
    <e:data-type name="string-expression"/>
  </e:attribute>
  <e:attribute name="disable-output-escaping">
    <e:constant value="yes"/>
    <e:constant value="no"/>
  </e:attribute>
  <e:empty/>
</e:element-syntax>

<p>The <code>xsl:value-of</code> element is instantiated to create a
text node in the result tree.  The required <code>select</code>
attribute is an <termref def="dt-expression">expression</termref>;
this expression is evaluated and the resulting object is converted to
a string as if by a call to the <xfunction>string</xfunction>
function. The string specifies the string-value of the created text
node.  If the string is empty, no text node will be created.  The
created text node will be merged with any adjacent text nodes.</p>

<p>The <code>xsl:copy-of</code> element can be used to copy a node-set
over to the result tree without converting it to a string. See <specref
ref="copy-of"/>.</p>

<p>For example, the following creates an HTML paragraph from a
<code>person</code> element with <code>given-name</code> and
<code>family-name</code> attributes.  The paragraph will contain the value
of the <code>given-name</code> attribute of the current node followed
by a space and the value of the <code>family-name</code> attribute of the
current node.</p>

<eg><![CDATA[<xsl:template match="person">
  <p>
   <xsl:value-of select="@given-name"/>
   <xsl:text> </xsl:text>
   <xsl:value-of select="@family-name"/>
  </p>
</xsl:template>]]></eg>

<p>For another example, the following creates an HTML paragraph from a
<code>person</code> element with <code>given-name</code> and
<code>family-name</code> children elements.  The paragraph will
contain the string-value of the first <code>given-name</code> child
element of the current node followed by a space and the string-value
of the first <code>family-name</code> child element of the current
node.</p>

<eg><![CDATA[<xsl:template match="person">
  <p>
   <xsl:value-of select="given-name"/>
   <xsl:text> </xsl:text>
   <xsl:value-of select="family-name"/>
  </p>
</xsl:template>]]></eg>

<p>The following precedes each <code>procedure</code> element with a
paragraph containing the security level of the procedure.  It assumes
that the security level that applies to a procedure is determined by a
<code>security</code> attribute on the procedure element or on an
ancestor element of the procedure. It also assumes that if more than
one such element has a <code>security</code> attribute then the
security level is determined by the element that is closest to the
procedure.</p>

<eg><![CDATA[<xsl:template match="procedure">
  <fo:block>
    <xsl:value-of select="ancestor-or-self::*[@security][1]/@security"/>
  </fo:block>
  <xsl:apply-templates/>
</xsl:template>]]></eg>

</div3>

<div3 id="attribute-value-templates">
<head>Attribute Value Templates</head>

<p><termdef id="dt-attribute-value-template" term="Attribute Value
Template">In an attribute value that is interpreted as an
<term>attribute value template</term>, such as an attribute of a
literal result element, an <termref
def="dt-expression">expression</termref> can be used by surrounding
the expression with curly braces (<code>{}</code>)</termdef>.  The
attribute value template is instantiated by replacing the expression
together with surrounding curly braces by the result of evaluating the
expression and converting the resulting object to a string as if by a
call to the <xfunction>string</xfunction> function.  Curly braces are
not recognized in an attribute value in an XSLT stylesheet unless the
attribute is specifically stated to be one that is interpreted as an
attribute value template; in an element syntax summary, the value
of such attributes is surrounded by curly braces.</p>

<note><p>Not all attributes are interpreted as attribute value
templates.  Attributes whose value is an expression or pattern,
attributes of <termref def="dt-top-level">top-level</termref> elements
and attributes that refer to named XSLT objects are not interpreted as
attribute value templates. In addition, <code>xmlns</code> attributes
are not interpreted as attribute value templates; it would not be
conformant with the XML Namespaces Recommendation to do
this.</p></note>

<p>The following example creates an <code>img</code> result element
from a <code>photograph</code> element in the source; the value of the
<code>src</code> attribute of the <code>img</code> element is computed
from the value of the <code>image-dir</code> variable and the
string-value of the <code>href</code> child of the
<code>photograph</code> element; the value of the <code>width</code>
attribute of the <code>img</code> element is computed from the value
of the <code>width</code> attribute of the <code>size</code> child of
the <code>photograph</code> element:</p>

<eg><![CDATA[<xsl:variable name="image-dir">/images</xsl:variable>

<xsl:template match="photograph">
<img src="{$image-dir}/{href}" width="{size/@width}"/>
</xsl:template>]]></eg>

<p>With this source</p>

<eg><![CDATA[<photograph>
  <href>headquarters.jpg</href>
  <size width="300"/>
</photograph>]]></eg>

<p>the result would be</p>

<eg><![CDATA[<img src="/images/headquarters.jpg" width="300"/>]]></eg>

<p>When an attribute value template is instantiated, a double left or
right curly brace outside an expression will be replaced by a single
curly brace.  It is an error if a right curly brace occurs in an
attribute value template outside an expression without being followed
by a second right curly brace.  A right curly brace inside a <xnt
href="&XPath;#NT-Literal">Literal</xnt> in an expression is not
recognized as terminating the expression.</p>

<p>Curly braces are <emph>not</emph> recognized recursively inside
expressions.  For example:</p>

<eg role="error"><![CDATA[<a href="#{id({@ref})/title}">]]></eg>

<p>is <emph>not</emph> allowed.  Instead, use simply:</p>

<eg><![CDATA[<a href="#{id(@ref)/title}">]]></eg>

</div3>

</div2>

<div2 id="number">
<head>Numbering</head>

<e:element-syntax name="number">
  <e:in-category name="instruction"/>
  <e:attribute name="level">
    <e:constant value="single"/>
    <e:constant value="multiple"/>
    <e:constant value="any"/>
  </e:attribute>
  <e:attribute name="count">
    <e:data-type name="pattern"/>
  </e:attribute>
  <e:attribute name="from">
    <e:data-type name="pattern"/>
  </e:attribute>
  <e:attribute name="value">
    <e:data-type name="number-expression"/>
  </e:attribute>
  <e:attribute name="format">
    <e:attribute-value-template>
      <e:data-type name="string"/>
    </e:attribute-value-template>
  </e:attribute>
  <e:attribute name="lang">
    <e:attribute-value-template>
      <e:data-type name="nmtoken"/>
    </e:attribute-value-template>
  </e:attribute>
  <e:attribute name="letter-value">
    <e:attribute-value-template>
      <e:constant value="alphabetic"/>
      <e:constant value="traditional"/>
    </e:attribute-value-template>
  </e:attribute>
  <e:attribute name="grouping-separator">
    <e:attribute-value-template>
      <e:data-type name="char"/>
    </e:attribute-value-template>
  </e:attribute>
  <e:attribute name="grouping-size">
    <e:attribute-value-template>
      <e:data-type name="number"/>
    </e:attribute-value-template>
  </e:attribute>
  <e:empty/>
</e:element-syntax>

<p>The <code>xsl:number</code> element is used to insert a formatted
number into the result tree.  The number to be inserted may be
specified by an expression. The <code>value</code> attribute contains
an <termref def="dt-expression">expression</termref>.  The expression
is evaluated and the resulting object is converted to a number as if
by a call to the <xfunction>number</xfunction> function.  The number is
rounded to an integer and then converted to a string using the
attributes specified in <specref ref="convert"/>; in this
context, the value of each of these attributes is
interpreted as an <termref def="dt-attribute-value-template">attribute
value template</termref>.  After conversion, the resulting string is
inserted in the result tree. For example, the following example
numbers a sorted list:</p>

<eg><![CDATA[<xsl:template match="items">
  <xsl:for-each select="item">
    <xsl:sort select="."/>
    <p>
      <xsl:number value="position()" format="1. "/>
      <xsl:value-of select="."/>
    </p>
  </xsl:for-each>
</xsl:template>]]></eg>

<p>If no <code>value</code> attribute is specified, then the
<code>xsl:number</code> element inserts a number based on the position
of the current node in the source tree. The following attributes
control how the current node is to be numbered:</p>

<ulist>

<item><p>The <code>level</code> attribute specifies what levels of the
source tree should be considered; it has the values
<code>single</code>, <code>multiple</code> or <code>any</code>. The
default is <code>single</code>.</p></item>

<item><p>The <code>count</code> attribute is a pattern that specifies
what nodes should be counted at those levels.  If <code>count</code>
attribute is not specified, then it defaults to the pattern that
matches any node with the same node type as the current node and, if
the current node has an expanded-name, with the same expanded-name as
the current node.</p></item>

<item><p>The <code>from</code> attribute is a pattern that specifies
where counting starts.</p></item>

</ulist>

<p>In addition, the attributes specified in <specref ref="convert"/>
are used for number to string conversion, as in the case when the
<code>value</code> attribute is specified.</p>

<p>The <code>xsl:number</code> element first constructs a list of
positive integers using the <code>level</code>, <code>count</code> and
<code>from</code> attributes:</p>

<ulist>

<item><p>When <code>level="single"</code>, it goes up to the first
node in the ancestor-or-self axis that matches
the <code>count</code> pattern, and constructs a list of length one
containing one plus the number of preceding siblings of that ancestor
that match the <code>count</code> pattern. If there is no such
ancestor, it constructs an empty list.  If the <code>from</code>
attribute is specified, then the only ancestors that are searched are
those that are descendants of the nearest ancestor that matches the
<code>from</code> pattern. Preceding siblings has the same meaning
here as with the <code>preceding-sibling</code> axis.</p></item>

<item><p>When <code>level="multiple"</code>, it constructs a list of all
ancestors of the current node in document order followed by the
element itself; it then selects from the list those nodes that match
the <code>count</code> pattern; it then maps each node in the list to
one plus the number of preceding siblings of that node that match the
<code>count</code> pattern.  If the <code>from</code> attribute is
specified, then the only ancestors that are searched are those that
are descendants of the nearest ancestor that matches the
<code>from</code> pattern. Preceding siblings has the same meaning
here as with the <code>preceding-sibling</code> axis.</p></item>

<item><p>When <code>level="any"</code>, it constructs a list of length
one containing the number of nodes that match the <code>count</code>
pattern and belong to the set containing the current node and all
nodes at any level of the document that are before the current node in
document order, excluding any namespace and attribute nodes (in other
words the union of the members of the <code>preceding</code> and
<code>ancestor-or-self</code> axes). If the <code>from</code>
attribute is specified, then only nodes after the first node before
the current node that match the <code>from</code> pattern are
considered.</p></item>

</ulist>

<p>The list of numbers is then converted into a string using the
attributes specified in <specref ref="convert"/>; in this
context, the value of each of these attributes is
interpreted as an <termref def="dt-attribute-value-template">attribute
value template</termref>.  After conversion, the resulting string is
inserted in the result tree.</p>

<p>The following would number the items in an ordered list:</p>

<eg><![CDATA[<xsl:template match="ol/item">
  <fo:block>
    <xsl:number/><xsl:text>. </xsl:text><xsl:apply-templates/>
  </fo:block>
<xsl:template>]]></eg>

<p>The following two rules would number <code>title</code> elements.
This is intended for a document that contains a sequence of chapters
followed by a sequence of appendices, where both chapters and
appendices contain sections, which in turn contain subsections.
Chapters are numbered 1, 2, 3; appendices are numbered A, B, C;
sections in chapters are numbered 1.1, 1.2, 1.3; sections in
appendices are numbered A.1, A.2, A.3.</p>

<eg><![CDATA[<xsl:template match="title">
  <fo:block>
     <xsl:number level="multiple"
                 count="chapter|section|subsection"
                 format="1.1 "/>
     <xsl:apply-templates/>
  </fo:block>
</xsl:template>

<xsl:template match="appendix//title" priority="1">
  <fo:block>
     <xsl:number level="multiple"
                 count="appendix|section|subsection"
                 format="A.1 "/>
     <xsl:apply-templates/>
  </fo:block>
</xsl:template>]]></eg>

<p>The following example numbers notes sequentially within a
chapter:</p>

<eg><![CDATA[<xsl:template match="note">
  <fo:block>
     <xsl:number level="any" from="chapter" format="(1) "/>
     <xsl:apply-templates/>
  </fo:block>
</xsl:template>]]></eg>

<p>The following example would number <code>H4</code> elements in HTML
with a three-part label:</p>

<eg><![CDATA[<xsl:template match="H4">
 <fo:block>
   <xsl:number level="any" from="H1" count="H2"/>
   <xsl:text>.</xsl:text>
   <xsl:number level="any" from="H2" count="H3"/>
   <xsl:text>.</xsl:text>
   <xsl:number level="any" from="H3" count="H4"/>
   <xsl:text> </xsl:text>
   <xsl:apply-templates/>
 </fo:block>
</xsl:template>]]></eg>

<div3 id="convert">
<head>Number to String Conversion Attributes</head>

<p>The following attributes are used to control conversion of a list
of numbers into a string. The numbers are integers greater than
0. The attributes are all optional.</p>

<p>The main attribute is <code>format</code>.  The default value for
the <code>format</code> attribute is <code>1</code>.  The
<code>format</code> attribute is split into a sequence of tokens where
each token is a maximal sequence of alphanumeric characters or a
maximal sequence of non-alphanumeric characters.  Alphanumeric means
any character that has a Unicode category of Nd, Nl, No, Lu, Ll, Lt,
Lm or Lo.  The alphanumeric tokens (format tokens) specify the format
to be used for each number in the list.  If the first token is a
non-alphanumeric token, then the constructed string will start with
that token; if the last token is non-alphanumeric token, then the
constructed string will end with that token.  Non-alphanumeric tokens
that occur between two format tokens are separator tokens that are
used to join numbers in the list.  The <var>n</var>th format token
will be used to format the <var>n</var>th number in the list.  If
there are more numbers than format tokens, then the last format token
will be used to format remaining numbers.  If there are no format
tokens, then a format token of <code>1</code> is used to format all
numbers.  The format token specifies the string to be used to
represent the number 1.  Each number after the first will be separated
from the preceding number by the separator token preceding the format
token used to format that number, or, if there are no separator
tokens, then by <code>.</code> (a period character).</p>

<p>Format tokens are a superset of the allowed values for the
<code>type</code> attribute for the <code>OL</code> element in HTML
4.0 and are interpreted as follows:</p>

<ulist>

<item><p>Any token where the last character has a decimal digit value
of 1 (as specified in the Unicode character property database),
and the Unicode value of preceding characters is one less than the
Unicode value of the last character generates a decimal
representation of the number where each number is at least as long as
the format token.  Thus, a format token <code>1</code> generates the
sequence <code>1 2 ... 10 11 12 ...</code>, and a format token
<code>01</code> generates the sequence <code>01 02 ... 09 10 11 12
... 99 100 101</code>.</p></item>

<item><p>A format token <code>A</code> generates the sequence <code>A
B C ... Z AA AB AC...</code>.</p></item>

<item><p>A format token <code>a</code> generates the sequence <code>a
b c ... z aa ab ac...</code>.</p></item>

<item><p>A format token <code>i</code> generates the sequence <code>i
ii iii iv v vi vii viii ix x ...</code>.</p></item>

<item><p>A format token <code>I</code> generates the sequence <code>I
II III IV V VI VII VIII IX X ...</code>.</p></item>

<item><p>Any other format token indicates a numbering sequence that
starts with that token.  If an implementation does not support a
numbering sequence that starts with that token, it must use a format
token of <code>1</code>.</p></item>

</ulist>

<p>When numbering with an alphabetic sequence, the <code>lang</code>
attribute specifies which language's alphabet is to be used; it has
the same range of values as <code>xml:lang</code> <bibref ref="XML"/>;
if no <code>lang</code> value is specified, the language should be
determined from the system environment.  Implementers should document
for which languages they support numbering.</p>

<note><p>Implementers should not make any assumptions about how
numbering works in particular languages and should properly research
the languages that they wish to support.  The numbering conventions of
many languages are very different from English.</p></note>

<p>The <code>letter-value</code> attribute disambiguates between
numbering sequences that use letters.  In many languages there are two
commonly used numbering sequences that use letters.  One numbering
sequence assigns numeric values to letters in alphabetic sequence, and
the other assigns numeric values to each letter in some other manner
traditional in that language.  In English, these would correspond to
the numbering sequences specified by the format tokens <code>a</code>
and <code>i</code>.  In some languages, the first member of each
sequence is the same, and so the format token alone would be
ambiguous.  A value of <code>alphabetic</code> specifies the
alphabetic sequence; a value of <code>traditional</code> specifies the
other sequence.  If the <code>letter-value</code> attribute is not
specified, then it is implementation-dependent how any ambiguity is
resolved.</p>

<note><p>It is possible for two conforming XSLT processors not to
convert a number to exactly the same string.  Some XSLT processors may not
support some languages.  Furthermore, there may be variations possible
in the way conversions are performed for any particular language that
are not specifiable by the attributes on <code>xsl:number</code>.
Future versions of XSLT may provide additional attributes to provide
control over these variations.  Implementations may also use
implementation-specific namespaced attributes on
<code>xsl:number</code> for this.</p></note>

<p>The <code>grouping-separator</code> attribute gives the separator
used as a grouping (e.g. thousands) separator in decimal numbering
sequences, and the optional <code>grouping-size</code> specifies the
size (normally 3) of the grouping.  For example,
<code>grouping-separator=","</code> and <code>grouping-size="3"</code>
would produce numbers of the form <code>1,000,000</code>.  If only one
of the <code>grouping-separator</code> and <code>grouping-size</code>
attributes is specified, then it is ignored.</p>

<p>Here are some examples of conversion specifications:</p>

<ulist>

<item><p><code>format="&amp;#x30A2;"</code> specifies Katakana
numbering</p></item>

<item><p><code>format="&amp;#x30A4;"</code> specifies Katakana
numbering in the <quote>iroha</quote> order</p></item>

<item><p><code>format="&amp;#x0E51;"</code> specifies numbering with
Thai digits</p></item>

<item><p><code>format="&amp;#x05D0;" letter-value="traditional"</code>
specifies <quote>traditional</quote> Hebrew numbering</p></item>

<item><p><code>format="&amp;#x10D0;" letter-value="traditional"</code>
specifies Georgian numbering</p></item>

<item><p><code>format="&amp;#x03B1;" letter-value="traditional"</code>
specifies <quote>classical</quote> Greek numbering</p></item>

<item><p><code>format="&amp;#x0430;" letter-value="traditional"</code>
specifies Old Slavic numbering</p></item>

</ulist>

</div3>
</div2>
</div1>

<div1 id="for-each">

<head>Repetition</head>

<e:element-syntax name="for-each">
  <e:in-category name="instruction"/>
  <e:attribute name="select" required="yes">
    <e:data-type name="node-set-expression"/>
  </e:attribute>
  <e:sequence>
    <e:element repeat="zero-or-more" name="sort"/>
    <e:model name="template"/>
  </e:sequence>
</e:element-syntax>

<p>When the result has a known regular structure, it is useful to be
able to specify directly the template for selected nodes.  The
<code>xsl:for-each</code> instruction contains a template, which is
instantiated for each node selected by the <termref
def="dt-expression">expression</termref> specified by the
<code>select</code> attribute. The <code>select</code> attribute is
required.  The expression must evaluate to a node-set.  The template
is instantiated with the selected node as the <termref
def="dt-current-node">current node</termref>, and with a list of all
of the selected nodes as the <termref
def="dt-current-node-list">current node list</termref>.  The nodes are
processed in document order, unless a sorting specification is present
(see <specref ref="sorting"/>).</p>

<p>For example, given an XML document with this structure</p>

<eg><![CDATA[<customers>
  <customer>
    <name>...</name>
    <order>...</order>
    <order>...</order>
  </customer>
  <customer>
    <name>...</name>
    <order>...</order>
    <order>...</order>
  </customer>
</customers>]]></eg>

<p>the following would create an HTML document containing a table with
a row for each <code>customer</code> element</p>

<eg><![CDATA[<xsl:template match="/">
  <html>
    <head>
      <title>Customers</title>
    </head>
    <body>
      <table>
	<tbody>
	  <xsl:for-each select="customers/customer">
	    <tr>
	      <th>
		<xsl:apply-templates select="name"/>
	      </th>
	      <xsl:for-each select="order">
		<td>
		  <xsl:apply-templates/>
		</td>
	      </xsl:for-each>
	    </tr>
	  </xsl:for-each>
	</tbody>
      </table>
    </body>
  </html>
</xsl:template>]]></eg>

</div1>

<div1>
<head>Conditional Processing</head>

<p>There are two instructions in XSLT that support conditional
processing in a template: <code>xsl:if</code> and
<code>xsl:choose</code>. The <code>xsl:if</code> instruction provides
simple if-then conditionality; the <code>xsl:choose</code> instruction
supports selection of one choice when there are several
possibilities.</p>

<div2>
<head>Conditional Processing with <code>xsl:if</code></head>

<e:element-syntax name="if">
  <e:in-category name="instruction"/>
  <e:attribute name="test" req