<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Id: WD-xslt-19990421.xml,v 1.1 1999/04/21 09:43:42 quint Exp $ -->
<!DOCTYPE spec PUBLIC
          "-//W3C//DTD Specification::19980521//EN"
          "/XML/1998/06/xmlspec-19980521.dtd" [
<!ENTITY XML "http://www.w3.org/TR/REC-xml">
<!ENTITY XMLNames "http://www.w3.org/TR/REC-xml-names">
<!ENTITY year "1999">
<!ENTITY month "Apr">
<!ENTITY day "21">
<!ENTITY MMDD "0421">
]>
<spec>
<header>
<title>XSL Transformations (XSLT) Specification</title>
<version>Version 1.0</version>
<w3c-designation>WD-xslt-&year;&MMDD;</w3c-designation>
<w3c-doctype>W3C Working Draft</w3c-doctype>
<pubdate><day>&day;</day><month>&month;</month><year>&year;</year></pubdate>
<publoc>
<loc href="http://www.w3.org/TR/&year;/WD-xslt-&year;&MMDD;"
          >http://www.w3.org/TR/&year;/WD-xslt-&year;&MMDD;</loc>
<loc href="http://www.w3.org/TR/&year;/WD-xslt-&year;&MMDD;.xml"
          >http://www.w3.org/TR/&year;/WD-xslt-&year;&MMDD;.xml</loc>
<loc href="http://www.w3.org/TR/&year;/WD-xslt-&year;&MMDD;.html"
          >http://www.w3.org/TR/&year;/WD-xslt-&year;&MMDD;.html</loc>
<!--
<loc href="http://www.w3.org/TR/&year;/WD-xslt-&year;&MMDD;.pdf"
          >http://www.w3.org/TR/&year;/WD-xslt-&year;&MMDD;.pdf</loc>
-->
</publoc>
<latestloc>
<loc href="http://www.w3.org/TR/WD-xslt"
          >http://www.w3.org/TR/WD-xslt</loc>
</latestloc>
<prevlocs>
<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 is a W3C Working Draft for review by W3C members and other
interested parties.  It is a draft document and may be updated,
replaced, or obsoleted by other documents at any time.  The material
in this draft was previously part of the XSL Working Draft. The XSL
Working Group will not allow early implementation to constrain its
ability to make changes to this specification prior to final release.
It is inappropriate to use W3C Working Drafts as reference material or
to cite them as other than <quote>work in progress</quote>. A list of
current W3C working drafts can be found at <loc
href="http://www.w3.org/TR">http://www.w3.org/TR</loc>.</p>

<p>This draft is intended to be <quote>feature complete</quote>.  The
Working Group plans to use future drafts to stabilize the current
functionality; it does not intend to add any new functionality in
version 1.0.</p>

<p>The XSL WG and the XML Linking WG have agreed to unify XSLT
expressions and XPointers <bibref ref="XPTR"/>. A common core
semantic model for querying has been agreed upon, and this draft
follows this model (see <specref ref="location-paths"/>). However,
further changes particularily in the syntax will probably be
necessary.</p>

<p>This is part of the <loc
href="http://www.w3.org/Style/Activity">Style activity</loc>.</p>

<p>Comments 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>

</status>

<abstract>

<p>XSLT 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 transformation 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>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"/>.</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 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>XSLT uses XML namespaces <bibref ref="XMLNAMES"/> to distinguish
elements that are instructions to the XSLT processor from elements that
specify literal result tree structure.  Instruction elements all
belong to the XSLT namespace.  The examples in this document use a
prefix of <code>xsl:</code> for elements in the XSLT namespace.</p>

<p>XSLT includes an expression language (see <specref
ref="expressions"/>) that is used for selecting elements for
processing, for conditional processing and for generating text.  The
expression language is not a complete programming language.  XSLT
provides an extension mechanism to allow access from the expression
language to a complete programming language such as ECMAScript or
Java.  XSLT does not require support for any programming
language. Therefore XSLT stylesheets that must be portable across all
XSLT implementations cannot depend on this extension mechanism.</p>

</div1>

<div1>
<head>Stylesheet Structure</head>

<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>XSLT processors must use the XML namespaces mechanism <bibref
ref="XMLNAMES"/> for both source documents and stylesheets.  All XSLT
defined elements, that is those specified in this document with a
prefix of <code>xsl:</code>, will only be recognized by the XSLT
processor if they belong to a namespace with the URI
<code>http://www.w3.org/XSL/Transform/1.0</code>; XSLT defined
elements are recognized only in the stylesheet not in the source
document.</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:key</code></p></item>
<item><p><code>xsl:functions</code></p></item>
<item><p><code>xsl:locale</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-variable</code></p></item>
<item><p><code>xsl:template</code></p></item>
</ulist>

<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><![CDATA[<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform/1.0">
  <xsl:import href="..."/>

  <xsl:include href="..."/>

  <xsl:strip-space elements="..."/>
  
  <xsl:preserve-space elements="..."/>

  <xsl:key name="..." match="..." use="..."/>

  <xsl:functions ns="...">
  ...
  </xsl:functions>

  <xsl:locale name="...">
  ...
  </xsl:locale>

  <xsl:attribute-set name="...">
  ...
  </xsl:attribute-set>

  <xsl:variable name="...">...</xsl:variable>

  <xsl:param-variable name="...">...</xsl:param-variable>

  <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>

</div1>

<div1 id="forwards">
<head>Forwards-compatible Processing</head>

<p>An XSLT processor must treat any namespace whose URI starts with
the <code>http://www.w3.org/XSL/Transform/</code> in the same way as
the XSLT 1.0 namespace
(<code>http://www.w3.org/XSL/Transform/1.0</code>) except that it must
recover from errors as follows:</p>

<ulist>

<item><p>Unrecognized attributes on elements in the XSLT namespace
must be ignored</p></item>

<item><p>Unrecognized top-level XSLT elements must be ignored along
with their content</p></item>

<item><p>Error reporting for unrecognized XSLT elements in templates
must be lazy: in other words it's not an error to have an unrecognized
XSLT element unless the element is actually instantiated</p></item>

<item><p>Similarly error reporting for bad expression syntax must be
lazy: it's not an error to have bad expression syntax in an attribute
on some element unless the element containing the bad syntax is
instantiated</p></item>

</ulist>

<ednote><edtext>What happens with stylesheets that mix XSLT namespaces
with different versions?</edtext></ednote>

<p>Thus any XSLT 1.0 processor must be able to process the following
stylesheet without error:</p>

<eg><![CDATA[<xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform/1.1">
  <xsl:template name="/">
    <xsl:choose>
      <xsl:when test="system-property('xsl:version') >= 1.1">
        <xsl:exciting-new-1.1-feature/>
      </xsl:when>
      <xsl:otherwise>
        <p>Sorry this stylesheet requires XSLT 1.1.</p>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>]]></eg>

</div1>

<div1 id="data-model">
<head>Data Model</head>

<p>XSLT operates on an XML document, whether a stylesheet or a source
document, as a tree.  Any two stylesheets or source documents that
have the same tree will be processed the same by XSLT.  The XML
document resulting from the tree construction process is also a tree.
This section describes how XSLT models an XML document as a tree.  This
model is conceptual only and does not mandate any particular
implementation.</p>

<p>XML documents operated on by XSLT must conform to the XML namespaces
specification <bibref ref="XMLNAMES"/>.</p>

<p>The tree contains nodes.  There are seven kinds of node:</p>

<ulist>

<item><p>root nodes</p></item>

<item><p>element nodes</p></item>

<item><p>text nodes</p></item>

<item><p>attribute nodes</p></item>

<item><p>namespace nodes</p></item>

<item><p>processing instruction nodes</p></item>

<item><p>comment nodes</p></item>

</ulist>

<p>Neither processing instruction nodes nor comment nodes are included
in the tree for the stylesheet.</p>

<p>For every type of node there is a way of determining a string
<term>value</term> for a node of that type.  For some types of node,
the value is part of the node; for other types of node, the value is
computed from the value of descendant nodes.</p>

<issue id="issue-data-entity"><p>Should XSLT provide support for
external data entities and notations?</p></issue>

<issue id="issue-entity-ref"><p>Should XSLT provide support for entity
references?</p></issue>

<issue id="issue-dtd"><p>Should XSLT provide support for DTDs in the
data model?</p></issue>


<div2 id="root-node">
<head>Root Node</head>

<p>The root node is the root of the tree.  It does not occur anywhere
else in the tree.  It has a single child which is the element node for
the document element of the document.</p>

<p>The <term>value</term> of the root node is the value of the
document element.</p>

</div2>

<div2 id="element-nodes">
<head>Element Nodes</head>

<p>There is an element node for every element in the document.  An
element has an expanded name consisting of a local name and a possibly
null URI reference (see <bibref ref="XMLNAMES"/>); the URI reference
will be null if the element type name has no prefix and there is no
default namespace in scope.  A relative URI reference should be
resolved into an absolute URI during namespace processing.</p>

<p>The children of an element node are the element nodes, comment
nodes, processing instruction nodes and text nodes for its content.
Entity references to both internal and external entities are expanded.
Character references are resolved.</p>

<p><termdef id="dt-descendants" term="Descendants">The
<term>descendants</term> of an element node are the children of the
element node and the descendants of the children that are element
nodes.</termdef></p>

<p>The <term>value</term> of an element node is the string that
results from concatenating all characters that are <termref
def="dt-descendants">descendants</termref> of the element node in the
order in which they occur in the document.</p>

<p><termdef id="dt-document-order" term="Document Order">The set of
all element nodes in a document can be ordered according to the order
of the start-tags of the elements in the document; this is known as
<term>document order</term>.</termdef></p>

<ednote><edtext>Need a definition of document order that handles
arbitrary node types, including attributes.</edtext></ednote>

<div3 id="unique-id">
<head>Unique IDs</head>

<p>An element object may have a unique identifier (ID).  This is the
value of the attribute which is declared in the DTD as type
<code>ID</code>.  No two elements in a document may have the same
unique ID.  If an XML processor reports two elements in a document as
having the same unique ID (which is possible only if the document is
invalid) then the second element must be treated as not having a
unique ID.</p>

<note><p>If a document does not have a DTD, then no element in the
document will have a unique ID.</p></note>

</div3>

<div3 id="base-uri">

<head>Base URI</head>

<p>An element 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 occurs in an external entity,
the base URI of that element is the URI of the external entity.
Otherwise the base URI is the base URI of the document.</p>

</div3>

</div2>

<div2 id="attribute-nodes">
<head>Attribute Nodes</head>

<p>Each element node has an associated set of attribute nodes.  A
defaulted attribute is treated the same as a specified attribute.  If
an attribute was declared for the element type in the DTD, but the
default was declared as <code>#IMPLIED</code>, and the attribute was
not specified on the element, then the element's attribute set does
not contain a node for the attribute.</p>

<p>An attribute node has an expanded name and has a string value.  The
expanded name consists of a local name and a possibly null URI (see
<bibref ref="XMLNAMES"/>); the URI will be null if the specified
attribute name did not have a prefix.  The value is the normalized
value as specified by the XML Recommendation <bibref ref="XML"/>.  An
attribute whose normalized value is a zero-length string is not
treated specially: it results in an attribute node whose value is a
zero-length string.</p>

<p>There are no attribute nodes for attributes that declare namespaces
(see <bibref ref="XMLNAMES"/>).</p>

<issue id="issue-external-dtd"><p>Should we specify something about
how we expect XSLT processors to process external DTDs and parameter
entities?  For example, what happens if an attribute default is
declared in an external DTD?</p></issue>

</div2>

<div2 id="namespace-nodes">
<head>Namespace Nodes</head>

<p>Each element has an associated set of namespace nodes, one for each
namespace prefix that is in scope for the element and one for the default
namespace if one is in scope for the element.  This means that an
element will have a namespace node:</p>

<ulist>

<item><p>for every attribute on the element whose name starts with
<code>xmlns:</code>;</p></item>

<item><p>for every attribute on an ancestor element whose name starts
<code>xmlns:</code> unless the element itself or a nearer ancestor
redeclares the prefix;</p></item>

<item>

<p>for an <code>xmlns</code> attribute, unless its value is the empty
string.</p>

<note><p>An attribute <code>xmlns=""</code> <quote>undeclares</quote>
the default namespace (see <bibref ref="XMLNAMES"/>).</p></note>

</item>

</ulist>

<p>A namespace node has a name which is a string giving the prefix.
This is empty if the namespace node is for the default namespace.  A
namespace node also has a value which is the namespace URI.  If the
namespace declaration specifies a relative URI, then the resolved
absolute URI is used as the value.</p>

<p>When writing an element node in the result tree out as XML, an XSLT
processor must add sufficient namespace-declaring attributes to the
start-tag to ensure that if a tree were recreated from the XML, then
the set of namespace nodes on the element node in the recreated tree
would be equal to or a superset of the set of namespace nodes of the
element node in the result tree.</p>

<note><p>The semantics of a document type may treat parts of attribute
values or data content as namespace prefixes.  The presence of
namespace nodes ensures that the semantics can be preserved when the
tree is written out as XML.</p></note>

</div2>


<div2>
<head>Processing Instruction Nodes</head>

<p>There is a processing instruction node for every processing
instruction.</p>

<ednote><edtext>What about processing instructions in the internal
subset or elsewhere in the DTD?</edtext></ednote>

<p>A processing instruction has a name.  This is a string equal to
the processing instruction's target.  It also has a value.  This is a
string equal to the part of the processing instruction following the
target and any whitespace.  It does not include the terminating
<code>?&gt;</code>.</p>

</div2>

<div2>
<head>Comment Nodes</head>

<p>There is a comment node for every comment.</p>

<ednote><edtext>What about comments in the internal subset or
elsewhere in the DTD?</edtext></ednote>

<p>A comment has a value.  This is a string equal to the text of the
comment not including the opening <code>&lt;!--</code> or the closing
<code>--&gt;</code>.</p>

</div2>

<div2>
<head>Text Nodes</head>

<p>Character data is grouped into text nodes.  As much character data
as possible is grouped into each text node: a text node never has an
immediately following or preceding sibling that is a text node.  The
value of a text node is the character data.</p>

<p>Each character within a CDATA section is treated as character data.
Thus <code>&lt;![CDATA[&lt;]]&gt;</code> in the source document will
treated the same as <code>&amp;lt;</code>.  Both will result in a
single <code>&lt;</code> character in a text node in the tree.</p>

<note><p>When a text node that contains a <code>&lt;</code> character
is written out as XML, the <code>&lt;</code> character must be escaped
by, for example, using <code>&amp;lt;</code>, or including it in a
CDATA section.</p></note>

<p>Characters inside comments or processing instructions are not
character data. Line-endings in external entities are normalized to
#xA as specified in the XML Recommendation <bibref ref="XML"/>.</p>

</div2>

<div2 id="strip">
<head>Whitespace Stripping</head>

<p>After the tree has been constructed, but before it is otherwise
processed by XSLT, some text nodes may be stripped.  The
stripping process takes as input a set of element types for which
whitespace must be preserved.  The stripping process is applied to
both stylesheets and source documents, but the set of
whitespace-preserving element types 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 type of the parent of the text node is in the set
of whitespace-preserving element types.</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 types
consists of just <code>xsl:text</code><!-- and <code>fo:text</code>-->.</p>

<!--<note><p><code>fo:text</code> is a formatting object that can contain
only characters.  Whereas characters outside <code>fo:text</code> are
subject to the XSLT-defined whitespace-related formatting properties
(such as collapsing adjacent whitespace characters) in the same way as
characters in the source document, whitespace characters occurring in
<code>fo:text</code> will not be collapsed or stripped by the
formatter.</p></note>-->

<p>For source documents, the set of whitespace-preserving element
types is determined using the stylesheet as follows:</p>

<ulist>

<item><p>If the <code>xsl:stylesheet</code> element specifies a
<code>default-space</code> attribute with a value of
<code>strip</code>, then the set is initially empty.  Otherwise the
set initially contains all element types that occur in the
document.</p></item>

<item><p>The <code>xsl:strip-space</code> element causes element types
to be removed from the set of whitespace-preserving element types.
The <code>elements</code> attribute gives a white-space separated list
of the names of the element types.</p></item>

<item><p>The <code>xsl:preserve-space</code> element causes element
types to be added to the set of whitespace-preserving element
types. The <code>elements</code> attribute gives a white-space
separated list of the names of the element types.</p></item>

</ulist>

<ednote><edtext>Clarify how these declarations interact with each
other and with xsl:import.</edtext></ednote>

</div2>

</div1>

<div1>
<head>Using the Result Tree</head>

<p>The <code>xsl:stylesheet</code> element has an optional
<code>result-ns</code> attribute; the value must be a namespace
prefix.  If there is a namespace declared as the default namespace,
then an empty string may be used as the value to specify that the
default namespace is the result namespace.</p>

<p>The <code>result-ns</code> attribute is a hint to the XSLT
processor that it should do something with the result tree other than
simply output it as XML.  XSLT processors are not required to pay
attention to the hint and may simply output the result tree as XML.
If the <code>result-ns</code> attribute is not specified, then the
result tree must be output as XML.  If the <code>result-ns</code>
attribute is specified, all elements in the result tree must belong to
the namespace identified by this prefix (the <term>result
namespace</term>).</p>

<p>When an XSLT processor outputs the result tree as a sequence of
bytes that represents the result tree in XML, it must do so in such a
way that the sequence of bytes is a well-formed XML document
conforming to the XML Namespaces Recommendation <bibref
ref="XMLNAMES"/> and that if a new tree was constructed from the
sequence of bytes as specified in <specref ref="data-model"/>, the new
tree would be the same as the result tree, with the following possible
exceptions:</p>

<ulist>

<item><p>The order of attributes in the two trees may be
different.</p></item>

<item><p>The new tree may contain namespace nodes that were not
present in the result tree.</p>
<note><p>An XSLT processor may need to add
namespace declarations in the course of outputting the result tree as
XML.</p></note>
</item>

</ulist>

<p>A result namespace of <code>http://www.w3.org/XSL/Format/1.0</code>
indicates that the result tree should be interpreted according to the
semantics defined in <bibref ref="XSL"/>.  XSL requires that XSL
processors respect this hint.  The examples in this document use the
<code>fo:</code> prefix for this namespace.</p>

<p>A result namespace of <code>http://www.w3.org/TR/REC-html40</code>
indicates that the result tree should be output as HTML that conforms
to the HTML 4.0 Recommendation rather than as XML; for example,</p>

<eg><![CDATA[<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/XSL/Transform/1.0"
  xmlns="http://www.w3.org/TR/REC-html40"
  result-ns="">

<xsl:template match="/">
  <html>
   <xsl:apply-templates/>
  </html>
</xsl:template>

...

</xsl:stylesheet>]]></eg>

<p>The <code>xsl:stylesheet</code> element can include an
<code>indent-result</code> attribute with values <code>yes</code> or
<code>no</code>.  If the stylesheet specifies
<code>indent-result="yes"</code>, then the XSLT processor may add
whitespace to the result tree (possibly based on whitespace stripped
from either the source document or the stylesheet) in order to indent
the result nicely; if <code>indent-result="no"</code>, it must not add
any whitespace to the result.  When adding whitespace with
<code>indent-result="yes"</code>, the XSLT processor can use any
algorithm provided that the result is the same as the result with
<code>indent-result="no"</code> after whitespace is stripped from both
using the process described with the set of whitespace-preserving
element types consisting of just <code>xsl:text</code>.</p>

</div1>

<div1 id="expressions">
<head>Expressions and Patterns</head>

<ednote><edtext>The XSL WG and the XML Linking WG have agreed to unify
XSLT expressions and XPointers. A common core semantic model for
querying has been agreed upon, and this draft follows this
model. However, further changes particularily in the syntax will
probably be necessary.</edtext></ednote>

<p>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>An expression is evaluated to yield an object which has one of the
following types:</p>

<slist>
<sitem>boolean (true or false)</sitem>
<sitem>number (a floating point number)</sitem>
<sitem>string (a sequence of UCS characters)</sitem>
<sitem>node-set (a set of nodes from source trees)</sitem>
<sitem>result tree fragment</sitem>
</slist>

<p>Expression evaluation occurs with respect to a context, which consists
of:</p>

<slist>

<sitem>a node (the context node)</sitem>

<sitem>a node list (the context node list)</sitem>

<sitem>a set of variable bindings</sitem>

<sitem>a node key function</sitem>

<sitem>a set of implementations of extension functions</sitem>

<sitem>the set of namespace declarations in scope for the expression</sitem>

</slist>

<p>The context node is always a member of the context node list.  The
variable bindings consist of a mapping from variable names to variable
values.  The value of a variable is an object which can have any of
the types which are possible for the value of an expression.</p>

<p>The variable bindings, node key function, extension functions and
namespace declarations used to evaluate a subexpression are always the
same as those used to evaluate the containing expression.  The context
node and context node list used to evaluate a subexpression is
sometimes different from the context node and context node list used
to evaluate the containing expression. When the evaluation of a kind
of expression is described, it will always be explicitly stated if the
context node and node list change for the evaluation of
subexpressions; if nothing is said about the context node and context
node list, they remain unchanged for the evaluation of subexpressions
of that kind of expression.</p>

<p>The node key function takes a pair of strings (a key name and a key
value) and a document and returns a set of nodes (the nodes in the
document that have a key with the specified name and value).</p>

<p>In XSLT, expressions occur in attribute values.  The grammar
specified in this section applies to the attribute value after XML 1.0
normalization.  So, for example, if the grammar uses the character
<code>&lt;</code> this must not appear in the XML source for the
stylesheet as <code>&lt;</code> but must be quoted according to XML
1.0 rules by, for example, entering it as <code>&amp;lt;</code>.</p>

<p>A top-level expression (an expression not occurring within an
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 node list comes from 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 node key function is specified by top-level
<code>xsl:key</code> elements (see <specref ref="key"/>)</p></item>

<item><p>the implementations of extension functions are provided by
top-level <code>xsl:functions</code> elements (see <specref
ref="functions"/>), and may also be provided externally to the
stylesheet by means not specified by XSLT</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; the default
namespace (as declared by <code>xmlns</code>) is not part of this
set</p></item>

</ulist>

<p>One important kind of expression is a location path.  A location
path selects a set of nodes relative to the context node.  The result
of evaluating an expression that is a location path is the node-set
containing the nodes selected by the location path.  Location paths
can recursively contain expressions which are used to filter lists of
nodes.</p>

<p>Certain contexts in XSLT make use of a pattern. 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.</p>

<p>In the following grammar, the nonterminals <xnt
href="&XMLNames;#NT-QName">QName</xnt> and <xnt
href="&XMLNames;#NT-NCName">NCName</xnt> are defined in <bibref
ref="XMLNAMES"/>, and <xnt href="&XML;#NT-S">S</xnt> is defined in
<bibref ref="XML"/>.</p>

<p>Expressions (including patterns and location paths) are parsed by
first dividing up the character string to be parsed into tokens and
then parsing the resulting sequence of tokens.  Whitespace can be
freely used between tokens.  The tokenization process is described in
<specref ref="exprlex"/>.</p>

<div2 id="location-paths">
<head>Location Paths</head>

<p>Every location path can be expressed using a straightforward but
rather verbose syntax.  There are also a number of syntactic
abbreviations that allow common cases to be expressed concisely.  This
section will explain the semantics of location paths using the
unabbreviated syntax.  The abbreviated syntax will then be explained
by showing how it expands into the unabbreviated syntax (see <specref
ref="path-abbrev"/>).</p>

<p>Here are some examples of location paths using the unabbreviated
syntax:</p>

<ulist>

<item><p><code>from-children(para)</code> selects the
<code>para</code> element children of the context node</p></item>

<item><p><code>from-children(*)</code> selects all element
children of the context node</p></item>

<item><p><code>from-children(text())</code> selects all text
node children of the context node</p></item>

<item><p><code>from-children(node())</code> selects all the
children of the context node, whatever their node type</p></item>

<item><p><code>from-attributes(name)</code> selects the
<code>name</code> attribute of the context node</p></item>

<item><p><code>from-attributes(*)</code> selects all the
attributes of the context node</p></item>

<item><p><code>from-descendants(para)</code> selects the
<code>para</code> element descendants of the context node</p></item>

<item><p><code>from-ancestors(div)</code> selects all <code>div</code>
ancestors of the context node</p></item>

<item><p><code>from-ancestors-or-self(div)</code> selects the
<code>div</code> ancestors of the context node and, if the context node is a
<code>div</code> element, the context node as well</p></item>

<item><p><code>from-descendants-or-self(para)</code> selects the
<code>para</code> element descendants of the context node and, if the context node is
a <code>para</code> element, the context node as well</p></item>

<item><p><code>from-self(para)</code> selects the context node if it is a
<code>para</code> element, and otherwise selects nothing</p></item>

<item><p><code>from-children(chapter)/from-descendants(para)</code>
selects the <code>para</code> element descendants of the
<code>chapter</code> element children of the context node</p></item>

<item><p><code>from-children(*)/from-children(para)</code> selects
all <code>para</code> grandchildren of the context node</p></item>

<item><p><code>/</code> selects the document root (which is
always the parent of the document element)</p></item>

<item><p><code>/from-descendants(para)</code> selects all the
<code>para</code> elements in the same document as the context node</p></item>

<item><p><code>/from-descendants(olist)/from-children(item)</code>
selects all the <code>item</code> elements in the same document as the
context node that have an <code>olist</code> parent</p></item>

<item><p><code>from-children(para[position()=1])</code> selects the first
<code>para</code> child of the context node</p></item>

<item><p><code>from-children(para[position()=last()])</code> selects the last
<code>para</code> child of the context node</p></item>

<item><p><code>from-children(para[position()=last()-1])</code> selects
the last but one <code>para</code> child of the context node</p></item>

<item><p><code>from-children(para[position()>1])</code> selects all
the <code>para</code> children of the context node other than the
first <code>para</code> child of the context node</p></item>

<item><p><code>from-following-siblings(chapter[position()=1])</code>
selects the next <code>chapter</code> sibling of the context node</p></item>

<item><p><code>from-preceding-siblings(chapter[position()=1])</code>
selects the previous <code>chapter</code> sibling of the
context node</p></item>

<item><p><code>/from-descendants(figure[position()=42])</code> selects
the forty-second <code>figure</code> element in the
document</p></item>

<item><p><code>/from-children(doc)/from-children(chapter[position()=5])/from-children(section[position()=2])</code>
selects the second <code>section</code> of the fifth
<code>chapter</code> of the <code>doc</code> document element</p></item>

<item><p><code>from-children(para[from-attributes(type)="warning"])</code>
selects all <code>para</code> children of the context node that have a
<code>type</code> attribute with value <code>warning</code></p></item>

<item><p><code>from-children(para[from-attributes(type)="warning"][position()=5])</code>
selects the fifth <code>para</code> child of the context node that has a
<code>type</code> attribute with value <code>warning</code></p></item>

<item><p><code>from-children(para[position()=5][from-attributes(type)="warning"])</code>
selects the fifth <code>para</code> child of the context node if that child has
a <code>type</code> attribute with value
<code>warning</code></p></item>

<item><p><code>from-children(chapter[from-children(title)="Introduction"])</code>
selects the <code>chapter</code> children of the context node whose first
<code>title</code> child has value equal to
<code>Introduction</code></p></item>

<item><p><code>from-children(chapter[from-children(title)])</code>
selects the <code>chapter</code> children of the context node that have one
or more <code>title</code> children</p></item>

<item><p><code>from-children(chapter[from-children(title[from-self(*)="Introduction"])])</code>
selects the <code>chapter</code> children of the context node any of whose
<code>title</code> children has value equal to
<code>Introduction</code></p></item>

<item><p><code>from-children(*[from-self(chapter) or
from-self(appendix)])</code> selects the <code>chapter</code> and
<code>appendix</code> children of the context node</p></item>

<item><p><code>from-children(*[from-self(chapter) or
from-self(appendix)][position()=last()])</code> selects the last
<code>chapter</code> or <code>appendix</code> child of the
context node</p></item>

</ulist>

<p>There are two kinds of location path: relative location paths
and absolute location paths.</p>

<p>A relative location path consists of a sequence of one or more
location steps separated by <code>/</code>.  The steps in a relative
location path are composed together from left to right.  Each step in
turn selects a set of nodes relative to a context node. An initial
sequence of steps is composed together with a following step as
follows.  The initial sequence of steps selects a set of nodes
relative to a context node.  Each node in that set is used as a
context node for the following step.  The sets of nodes identified by
the second step are unioned together.  The set of nodes identified by
the composition of the steps is this union. For example,
<code>from-children(div)/from-children(para)</code> selects the
<code>para</code> element children of the <code>div</code> element
children of the context node, or, in other words, the
<code>para</code> element grandchildren that have <code>div</code>
parents.</p>

<p>An absolute location path consists of <code>/</code> optionally
followed by a relative location path.  A <code>/</code> by itself
selects the root node of the document containing the context node.  If
it is followed by a relative location path, then the location path
selects the set of nodes that would be selected by the relative
location path relative to the root node of the document containing the
context node.</p>

<p>A location step consists of</p>

<ulist>
<item><p>an axis identifier;</p></item>
<item><p>a node test;</p></item>
<item><p>zero or more predicates.</p></item>
</ulist>

<p>The axis identifier selects an initial list of nodes relative to
the context node.  The initial list of nodes is filtered first by the node
test; the result of filtering by the node test is then filtered by the
first predicate; the result of that is then filtered by the next
predicate and so on. The node test selects nodes from the initial list
based on the node type and node name.  Each predicate selects nodes
that satisfy a condition specified by an arbitrary expression.  The
result of the location step is the set of nodes that are members of
the list that results from filtering the initial list by the node test
and all the predicates.  Note that although a location step selects a
<emph>set</emph> of nodes, an axis selects a <emph>list</emph> of
nodes and the predicates operate on a <emph>list</emph> of nodes.</p>

<p>The axis identifier is followed by the node test and predicates in
parentheses. For example, <code>from-descendants(para)</code> selects the
descendants of the context node that are <code>para</code> elements:
<code>from-descendants</code> specifies the axis, and <code>para</code>
is a test that is true for elements with name <code>para</code>.  Each
predicate is specified as an expression in square brackets.</p>

<scrap>
<head>Location Paths</head>
<prodgroup pcw5="1" pcw2="10" pcw4="18">
<prod id="NT-LocationPath">
<lhs>LocationPath</lhs>
<rhs><nt def="NT-RelativeLocationPath">RelativeLocationPath</nt></rhs>
<rhs>| <nt def="NT-AbsoluteLocationPath">AbsoluteLocationPath</nt></rhs>
</prod>
<prod id="NT-AbsoluteLocationPath">
<lhs>AbsoluteLocationPath</lhs>
<rhs>'/' <nt def="NT-RelativeLocationPath">RelativeLocationPath</nt>?</rhs>
<rhs>| <nt def="NT-AbbreviatedAbsoluteLocationPath">AbbreviatedAbsoluteLocationPath</nt></rhs>
</prod>
<prod id="NT-RelativeLocationPath">
<lhs>RelativeLocationPath</lhs>
<rhs><nt def="NT-Step">Step</nt></rhs>
<rhs>| <nt def="NT-RelativeLocationPath">RelativeLocationPath</nt> '/' <nt def="NT-Step">Step</nt></rhs>
<rhs>| <nt def="NT-AbbreviatedRelativeLocationPath">AbbreviatedRelativeLocationPath</nt></rhs>
</prod>
<prod id="NT-Step">
<lhs>Step</lhs>
<rhs><nt def="NT-AxisIdentifier">AxisIdentifier</nt> '(' <nt def="NT-NodeTest">NodeTest</nt> <nt def="NT-Predicate">Predicate</nt>* ')'</rhs>
<rhs>| <nt def="NT-AbbreviatedStep">AbbreviatedStep</nt></rhs>
</prod>
</prodgroup>
</scrap>

<div3>
<head>Axes</head>

<p>An axis identifies a list of nodes based on the kind of tree
relationship that the nodes have to the context node.  For example, the
children of the context node are one axis, the ancestors of the context node are
another axis. Note that an axis identifies an ordered list, not a set.
The order of nodes in an axis is in the direction away from the
context node.</p>

<p>The following axes are defined:</p>

<ulist>

<item><p>the <code>children</code> axis contains the children of the
context node in document order</p></item>

<item><p>the <code>descendants</code> axis contains the descendants of
the context node in document order</p></item>

<item><p>the <code>parent</code> axis contains the parent of the
context node, if there is one</p>

<ednote><edtext>Is the parent of an attribute node the element that
the attribute is on?</edtext></ednote>

</item>

<item><p>the <code>following-siblings</code> axis contains the
following siblings of the context node in document order</p></item>

<item><p>the <code>preceding-siblings</code> axis contains the
preceding siblings of the context node in reverse document order; the first
preceding sibling is first on the axis; the sibling preceding that
node is the second on the axis and so on.</p></item>

<item>

<p>the <code>following</code> axis contains all nodes in the same
document as the context node that are after the context node in document order;
the nodes are ordered in document order</p>

<issue id="issue-following-axis"><p>Is the <code>following</code> axis
needed?</p></issue>

<issue id="issue-following-start"><p>Should the <code>following</code> axis
include the descendants of the context node?</p></issue>

</item>

<item>

<p>the <code>preceding</code> axis contains all nodes in the same
document as the context node that are before the context node in document order;
the nodes are ordered in reverse document order</p>

<issue id="issue-preceding-axis"><p>Is the <code>preceding</code> axis
needed?</p></issue>

</item>

<item><p>the <code>ancestors</code> axis contains the ancestors of the
context node; the nodes are ordered in reverse document order; thus the
parent is the first node on the axis, and the parent's parent is the
second node on the axis</p></item>

<item><p>the <code>attributes</code> axis contains the attributes of
the context node; the order of nodes on this axis is
implementation-defined</p></item>

<item><p>the <code>self</code> axis contains just the context node
itself</p></item>

<item><p>the <code>ancestors-or-self</code> axis contains the context node
and ancestors of the context node in reverse document order; thus the context node
is the first node on the axis, and the context node's parent the
second</p></item>

<item><p>the <code>descendants-or-self</code> axis contains the context node
and the descendants of the context node in document order; thus the context node
is the first node on the axis, and the first child of the context node is
the second node on the axis</p></item>

</ulist>

<p>In an axis identifier the name of the axis is preceded by
<code>from-</code> to distinguish it from a function name.</p>

<scrap>
<head>Axes</head>
<prod id="NT-AxisIdentifier">
<lhs>AxisIdentifier</lhs>
<rhs>'from-ancestors'</rhs>
<rhs>| 'from-ancestors-or-self'</rhs>
<rhs>| 'from-attributes'</rhs>
<rhs>| 'from-children'</rhs>
<rhs>| 'from-descendants'</rhs>
<rhs>| 'from-descendants-or-self'</rhs>
<rhs>| 'from-following'</rhs>
<rhs>| 'from-following-siblings'</rhs>
<rhs>| 'from-parent'</rhs>
<rhs>| 'from-preceding'</rhs>
<rhs>| 'from-preceding-siblings'</rhs>
<rhs>| 'from-self'</rhs>
</prod>
</scrap>

</div3>

<div3>
<head>Node Tests</head>

<p>A node test that is a <xnt
href="&XMLNames;#NT-QName">QName</xnt> tests whether the node is an
element or attribute with the specified name.  For example,
<code>from-attributes(href)</code> selects the <code>href</code>
attribute of the context node; if the context node has no <code>href</code>
attribute, it will select an empty set of nodes.</p>

<p>A <xnt href="&XMLNames;#NT-QName">QName</xnt> in the node test
is expanded into a local name and a possibly null URI.  This expansion
is done using the namespace declarations from the expression context.
This is the same way expansion is done for element type names in start
and end-tags except that the default namespace declared with
<code>xmlns</code> is not used: if the <xnt
href="&XMLNames;#NT-QName">QName</xnt> does not have a prefix, then
the URI is null (this is the same way attribute names are expanded).
The expanded names are then compared for equality.  Two expanded names
are equal if they have the same local part, and either both have no
URI or both have the same URI.</p>

<p>A node test <code>*</code> is true for any element or
attribute node.  For example, <code>from-children(*)</code> will
select all element children of the context node, and
<code>from-attributes(*)</code> will select all attributes of the
context node.</p>

<p>A node test can have the form <xnt
href="&XMLNames;#NT-NCName">NCName</xnt><code>:*</code>.  In this case the
prefix is expanded in the same way as with a <xnt
href="&XMLNames;#NT-QName">QName</xnt> using the context namespace
declarations.  The node test will be true for an element or attribute
whose expanded name has the URI to which the prefix expands, whatever
the local part of the name.</p>

<p>The node test <code>text()</code> is true for any text node. For
example <code>from-children(text())</code> will select the text node
children of the context node.  Similarly, the node test
<code>comment()</code> is true for any comment node, and the node test
<code>pi()</code> is true for any processing instruction. The
<code>pi()</code> test may have an argument that is <nt
def="NT-Literal">Literal</nt>; in this case it is true for any
processing instruction that has a name equal to the value of the <nt
def="NT-Literal">Literal</nt>.</p>

<p>A node test <code>node()</code> is true for any node.</p>

<scrap>
<head></head>
<prod id="NT-NodeTest">
<lhs>NodeTest</lhs>
<rhs><nt def="NT-WildcardName">WildcardName</nt></rhs>
<rhs>| <nt def="NT-NodeType">NodeType</nt> '(' ')'</rhs>
<rhs>| 'pi' '(' <nt def="NT-Literal">Literal</nt> ')'</rhs>
</prod>
</scrap>

</div3>

<div3>
<head>Predicates</head>

<p>A predicate filters a list of nodes to produce a new list of nodes.
For each node in the list to be filtered, the <nt
def="NT-PredicateExpr">PredicateExpr</nt> is evaluated with that node
as the context node and with the complete list of nodes to be filtered
as the context node list; if <nt
def="NT-PredicateExpr">PredicateExpr</nt> evaluates to true for that
node, the node is included in the new list; otherwise it is not
included.</p>

<p>A <nt def="NT-PredicateExpr">PredicateExpr</nt> is evaluated by
evaluating the <nt def="NT-Expr">Expr</nt> and converting the result
to a boolean.  If the result is a number, the result will be converted
to true if the number is equal to the position of the context node in
the context node list and will be converted to false otherwise; if the
result is not a number, then the result will be converted as if by a
call to the <code>boolean()</code> function.  Thus a location path
<code>para[3]</code> is equivalent to <code>para[position()=3]</code>.</p>

<scrap>
<head>Predicates</head>
<prod id="NT-Predicate">
<lhs>Predicate</lhs>
<rhs>'[' <nt def="NT-PredicateExpr">PredicateExpr</nt> ']'</rhs>
</prod>
<prod id="NT-PredicateExpr">
<lhs>PredicateExpr</lhs>
<rhs><nt def="NT-Expr">Expr</nt></rhs>
</prod>
</scrap>

</div3>

<div3 id="path-abbrev">
<head>Abbreviated Syntax</head>

<p>Here are some examples of location paths using abbreviated
syntax:</p>

<ulist>

<item><p><code>para</code> selects the <code>para</code> element children of
the context node</p></item>

<item><p><code>*</code> selects all element children of the
context node</p></item>

<item><p><code>text()</code> selects all text node children of the
context node</p></item>

<item><p><code>@name</code> selects the <code>name</code> attribute of
the context node</p></item>

<item><p><code>@*</code> selects all the attributes of the
context node</p></item>

<item><p><code>para[1]</code> selects the first <code>para</code> child of
the context node</p></item>

<item><p><code>para[last()]</code> selects the last <code>para</code> child
of the context node</p></item>

<item><p><code>*/para</code> selects all <code>para</code> grandchildren of
the context node</p></item>

<item><p><code>/doc/chapter[5]/section[2]</code> selects the second
<code>section</code> of the fifth <code>chapter</code> of the
<code>doc</code></p></item>

<item><p><code>chapter//para</code> selects the <code>para</code> element
descendants of the <code>chapter</code> element children of the
context node</p></item>

<item><p><code>//para</code> selects all the <code>para</code> descendants of
the document root and thus selects all <code>para</code> elements in the
same document as the context node</p></item>

<item><p><code>//olist/item</code> selects all the <code>item</code>
elements in the same document as the context node that have an
<code>olist</code> parent</p></item>

<item><p><code>.</code> selects the context node</p></item>

<item><p><code>.//para</code> selects the <code>para</code> element
descendants of the context node</p></item>

<item><p><code>..</code> selects the parent of the context node</p></item>

<item><p><code>../@lang</code> selects the <code>lang</code> attribute
of the parent of the context node</p></item>

<item><p><code>para[@type="warning"]</code> selects all <code>para</code>
children of the context node that have a <code>type</code> attribute with
value <code>warning</code></p></item>

<item><p><code>para[@type="warning"][5]</code> selects the fifth
<code>para</code> child of the context node that has a <code>type</code>
attribute with value <code>warning</code></p></item>

<item><p><code>para[5][@type="warning"]</code> selects the fifth
<code>para</code> child of the context node if that child has a
<code>type</code> attribute with value <code>warning</code></p></item>

<item><p><code>chapter[title="Introduction"]</code> selects the
<code>chapter</code> children of the context node whose first
<code>title</code> child has value equal to
<code>Introduction</code></p></item>

<item><p><code>chapter[title]</code> selects the <code>chapter</code>
children of the context node that have one or more <code>title</code>
children</p></item>

<item><p><code>chapter[title[.="Introduction"]]</code> selects the
<code>chapter</code> children of the context node any of whose
<code>title</code> children has value equal to
<code>Introduction</code></p></item>

<item><p><code>employee[@secretary and @assistant]</code> selects all
the <code>employee</code> children of the context node that have both a
<code>secretary</code> attribute and an <code>assistant</code>
attribute</p></item>

</ulist>

<p>The most important abbreviation is that when the axis is the
children axis, the <code>from-children</code> and surrounding
parentheses can be omitted.  In effect the <code>children</code> axis
is the default axis.  For example, a location path <code>div/para</code>
is short for <code>from-children(div)/from-children(para)</code>.</p>

<p>There's also an abbreviation for the attributes axis.  Instead of
using <code>from-attributes</code> and parentheses around the node
test, the node test can be preceded by <code>@</code> to indicate the
attributes axis. For example, a location path
<code>para[@type="warning"]</code> is short for
<code>from-children(para[from-attributes(type)="warning"])</code> and
so selects <code>para</code> children with a <code>type</code>
attribute with value equal to <code>warning</code>.</p>

<p><code>//</code> is short for
<code>/from-descendants-or-self(node())/</code>.  For example,
<code>//para</code> is short for
<code>/from-descendants-or-self(node())/from-children(para)</code> and so
will select any <code>para</code> element in the document (even a
<code>para</code> element that is a document element will be selected by
<code>//para</code> since the document element node is a child of the
root node); <code>div//para</code> is short for
<code>div/from-descendants-or-self(node())/from-children(para)</code> and
so will select all <code>para</code> descendants of <code>div</code>
children.</p>

<p>A location step of <code>.</code> is short for
<code>from-self(node())</code>. This is particularly useful in
conjunction with <code>//</code>. For example, the location path
<code>.//para</code> is short for</p>

<eg>from-self(node())/from-descendants-or-self(node())/from-children(para)</eg>

<p>and so will select all <code>para</code> descendant elements of the
context node.</p>

<p>Similarly a location step of <code>..</code> is short for
<code>from-parent(node())</code>. For example, <code>../title</code>
is short for <code>from-parent(node())/from-children(title)</code> and
so will select the <code>title</code> children of the parent of the
context node.</p>

<scrap>
<head>Abbreviations</head>
<prodgroup pcw5="1" pcw2="15" pcw4="16">
<prod id="NT-AbbreviatedAbsoluteLocationPath">
<lhs>AbbreviatedAbsoluteLocationPath</lhs>
<rhs>'//' <nt def="NT-RelativeLocationPath">RelativeLocationPath</nt></rhs>
</prod>
<prod id="NT-AbbreviatedRelativeLocationPath">
<lhs>AbbreviatedRelativeLocationPath</lhs>
<rhs><nt def="NT-RelativeLocationPath">RelativeLocationPath</nt> '//' <nt def="NT-Step">Step</nt></rhs>
</prod>
<prod id="NT-AbbreviatedStep">
<lhs>AbbreviatedStep</lhs>
<rhs><nt def="NT-AbbreviatedNodeTestStep">AbbreviatedNodeTestStep</nt></rhs>
<rhs>| '.'</rhs>
<rhs>| '..'</rhs>
</prod>
<prod id="NT-AbbreviatedNodeTestStep">
<lhs>AbbreviatedNodeTestStep</lhs>
<rhs>'@'? <nt def="NT-NodeTest">NodeTest</nt> <nt def="NT-Predicate">Predicate</nt>*</rhs>
</prod>
</prodgroup>
</scrap>

</div3>

</div2>

<div2>
<head>Expressions</head>

<div3>
<head>Basics</head>

<p>A <nt def="NT-VariableReference">VariableReference</nt> evaluates
to the value to which the variable name is bound in the set of
variable bindings in the context.</p>

<p>Parentheses may be used for grouping.</p>

<scrap>
<head></head>
<prod id="NT-Expr">
<lhs>Expr</lhs>
<rhs><nt def="NT-OrExpr">OrExpr</nt></rhs>
</prod>
<prod id="NT-PrimaryExpr">
<lhs>PrimaryExpr</lhs>
<rhs><nt def="NT-VariableReference">VariableReference</nt></rhs>
<rhs>| '(' <nt def="NT-Expr">Expr</nt> ')'</rhs>
<rhs>| <nt def="NT-Literal">Literal</nt></rhs>
<rhs>| <nt def="NT-Number">Number</nt></rhs>
<rhs>| <nt def="NT-FunctionCall">FunctionCall</nt></rhs>
</prod>
<prod id="NT-FunctionCall">
<lhs>FunctionCall</lhs>
<rhs><nt def="NT-NodeSetFunctionCall">NodeSetFunctionCall</nt></rhs>
<rhs>| <nt def="NT-BooleanFunctionCall">BooleanFunctionCall</nt></rhs>
<rhs>| <nt def="NT-StringFunctionCall">StringFunctionCall</nt></rhs>
<rhs>| <nt def="NT-NumberFunctionCall">NumberFunctionCall</nt></rhs>
<rhs>| <nt def="NT-SystemFunctionCall">SystemFunctionCall</nt></rhs>
<rhs>| <nt def="NT-ExtensionFunctionCall">ExtensionFunctionCall</nt></rhs>
</prod>
</scrap>

</div3>

<div3 id="node-sets">
<head>Node-sets</head>

<p>A location path can be used as an expression.  The expression
returns the set of nodes selected by the path.</p>

<p>The <code>|</code> operator computes the union of its operands
which must be node-sets.</p>

<p>Square brackets are used to filter expressions in the same way that
they are used in location paths. It is an error if the expression to
be filtered does not evaluate to a node-set.  The context node list
used for evaluating the expression in square brackets is the node-set
to be filtered listed in document order.</p>

<p>The <code>/</code> operator and <code>//</code> operators combine
an arbitrary expression and a relative location path.  It is an error
if the expression does not evaluate to a node-set.  The <code>/</code>
operator does composition in the same way as when <code>/</code> is
used in a location path. As in location paths, <code>//</code> is
short for <code>/from-descendants-or-self(node())/</code>.</p>

<p>There are no types of objects that can be converted to node-sets.
It is an error if evaluating a <nt
def="NT-NodeSetExpr">NodeSetExpr</nt> yields an object that is not a
node-set.</p>

<p>The <code>last()</code> function returns the number of nodes in
the context node list.  The <code>position()</code> function returns
the position of the context node in the context node list.  The first
position is 1, and so the last position will be equal to
<code>last()</code>.</p>

<p>The <code>count()</code> function returns the number of nodes in the
argument node-set.</p>

<p>The <code>id()</code> and <code>idref()</code> functions select
elements by their unique ID (see <specref ref="unique-id"/>).
<code>id()</code> converts its argument to a string and returns a
node-set containing the element in the same document as the context
node with unique ID equal to that string, if there is such an element,
and otherwise returns an empty node-set.  <code>idref()</code>
requires that its argument be a node-set; for each node in the
node-set, the value is split into a whitespace-separated list of
tokens; <code>idref()</code> returns a node-set containing the
elements in the same document as the context node that have a unique
ID equal to any of the tokens in the value of any of the nodes in the
node-set. For example,</p>

<ulist>
<item><p><code>id("foo")</code> selects the element with unique ID
<code>foo</code></p></item>
<item><p><code>id("foo")/from-children(para[position()=5])</code> selects
the fifth <code>para</code> child of the element with unique ID
<code>foo</code></p></item>
</ulist>

<ednote><edtext>No way to get an ID in another document. Can
workaround with xsl:for-each.  Maybe add optional second argument
which gives document.</edtext></ednote>

<issue id="issue-id-inverse"><p>Should there be a way to get back from
an element to the elements that reference it (eg by IDREF
attributes)?</p></issue>

<p>The <code>key()</code> and <code>keyref()</code> functions select a
set of nodes using the node key function in the expression evaluation
context.  Both functions have a string as the first argument that
specifies the name of the key and have an expression as the second
argument. <code>key()</code> has a second argument that is a string
and returns a node-set containing the nodes in the same document as
the context node that have a value for the named key equal to this
string. <code>keyref()</code> has a second argument that is a node-set
and returns a node-set containing the nodes in the same document as
the context node that have a value for the named key equal to the
value of any of the nodes in the argument node-set.  See <specref
ref="key"/> for how to declare a key.</p>

<p>The <code>doc()</code> and <code>docref()</code> functions allow
access to XML documents other than the initial source document.  They
both rely on the ability to treat a string as a URI reference that is
mapped to a node-set; this mapping always takes places relative to a
node that can be used to resolve a relative URI into an absolute URI.
If the URI reference does not contain a fragment identifier, then it
will be mapped to a node-set containing the root node in a tree
representing the XML document whose document entity is the resource
identified by the URI.  If the URI reference contains a fragment
identifier, then it will first map the URI to a tree representing the
XML document whose document entity is the resource identified by the
URI and then use the fragment identifier to select a set of nodes in
that tree; the semantics of the fragment identifier is dependant on
media type of the result of retrieving the URI.  <code>doc()</code>
takes a string argument which it treats as a URI reference which is
mapped to a node-set relative to the element in the stylesheet
containing the expression.  Note that a zero-length URI reference is a
reference to the document relative to which the URI reference is being
resolved; thus <code>doc("")</code> refers to the root node of the
stylesheet; the tree representation of the stylesheet is exactly the
same as if the XML document containing the stylesheet was the initial
source document.  <code>docref()</code> takes a node-set argument; for
each node in the node-set, <code>docref()</code> treats the value of
the node as a URI reference that is mapped to a node-set relative to
that same node; <code>docref()</code> returns the union of the
resulting node-sets.</p>

<ednote><edtext>What if the fragment identifier identifies something
that isn't a set of nodes (eg a span or a substring within a text
node)?  What are the allowed media types for the returned data?  What
is document order for node sets including nodes from multiple
documents?</edtext></ednote>

<p>The <code>local-part()</code> function returns a string containing
the local part of the name of the first node in the argument
node-set. If the node-set is empty or the first node has no name, an
empty string is returned.  If the argument is omitted it defaults to
the context node.</p>

<p>The <code>namespace()</code> function returns a string containing
namespace of the name of the first node in the argument node-set. If
the node-set is empty or the first node has no name or the name has no
namespace, an empty string is returned.  If the argument is omitted it
defaults to the context node.</p>

<p>The <code>qname()</code> function returns a string containing a
<xnt href="&XMLNames;#NT-QName">QName</xnt> representing the name of
the first node in the argument. The <xnt
href="&XMLNames;#NT-QName">QName</xnt> must represent the name with
respect to the namespace declarations in effect on the node whose name
is being represented.  Typically this will be the form in which the
name occurred in the XML source.  This need not be the case if there
are namespace declarations in effect on the node that associate
multiple prefixes with the same namespace.  However, an implementation
may include information about the original prefix in its
representation of nodes; in this case an implementation can ensure
that the returned string is always the same as the <xnt
href="&XMLNames;#NT-QName">QName</xnt> used in the XML source. If the
argument it omitted it defaults to the context node.</p>

<p>The <code>generate-id()</code> function returns a string that can
be used as a unique identifier for the first node in the argument
node-set.  The unique identifier must consist of ASCII alphanumeric
characters and must start with an alphabetic character.  An
implementation is free to generate an identifier in any convenient way
provided that it always generates the same identifier for the same
node and that different identifiers are always generated from
different nodes. An implementation is under no obligation to generate
the same identifiers each time a a document is transformed. If the
argument node-set is empty, the empty string is returned. If the
argument is omitted, it defaults to the context node.</p>

<scrap>
<head></head>
<prod id="NT-NodeSetExpr">
<lhs>NodeSetExpr</lhs>
<rhs><nt def="NT-Expr">Expr</nt></rhs>
</prod>
<prod id="NT-UnionExpr">
<lhs>UnionExpr</lhs>
<rhs><nt def="NT-PathExpr">PathExpr</nt></rhs>
<rhs>| <nt def="NT-UnionExpr">UnionExpr</nt> '|' <nt def="NT-PathExpr">PathExpr</nt></rhs>
</prod>
<prod id="NT-PathExpr">
<lhs>PathExpr</lhs>
<rhs><nt def="NT-LocationPath">LocationPath</nt></rhs>
<rhs>| <nt def="NT-FilterExpr">FilterExpr</nt></rhs>
<rhs>| <nt def="NT-FilterExpr">FilterExpr</nt> '/' <nt def="NT-RelativeLocationPath">RelativeLocationPath</nt></rhs>
<rhs>| <nt def="NT-FilterExpr">FilterExpr</nt> '//' <nt def="NT-RelativeLocationPath">RelativeLocationPath</nt></rhs>
</prod>
<prod id="NT-FilterExpr">
<lhs>FilterExpr</lhs>
<rhs><nt def="NT-PrimaryExpr">PrimaryExpr</nt></rhs>
<rhs>| <nt def="NT-FilterExpr">FilterExpr</nt> <nt def="NT-Predicate">Predicate</nt></rhs>
</prod>
<prod id="NT-NodeSetFunctionCall">
<lhs>NodeSetFunctionCall</lhs>
<rhs>'last' '(' ')'</rhs>
<rhs>| 'position' '(' ')'</rhs>
<rhs>| 'count' '(' <nt def="NT-NodeSetExpr">NodeSetExpr</nt> ')'</rhs>
<rhs>| 'id' '(' <nt def="NT-StringExpr">StringExpr</nt> ')'</rhs>
<rhs>| 'idref' '(' <nt def="NT-NodeSetExpr">NodeSetExpr</nt> ')'</rhs>
<rhs>| 'key' '(' <nt def="NT-StringExpr">StringExpr</nt> ',' <nt def="NT-StringExpr">StringExpr</nt> ')'</rhs>
<rhs>| 'keyref' '('  <nt def="NT-StringExpr">StringExpr</nt> ',' <nt def="NT-NodeSetExpr">NodeSetExpr</nt> ')'</rhs>
<rhs>| 'doc' '(' <nt def="NT-StringExpr">StringExpr</nt> ')'</rhs>
<rhs>| 'docref' '(' <nt def="NT-NodeSetExpr">NodeSetExpr</nt> ')'</rhs>
<rhs>| 'local-part' '(' <nt def="NT-NodeSetExpr">NodeSetExpr</nt>? ')'</rhs>
<rhs>| 'namespace' '(' <nt def="NT-NodeSetExpr">NodeSetExpr</nt>? ')'</rhs>
<rhs>| 'qname' '(' <nt def="NT-NodeSetExpr">NodeSetExpr</nt>? ')'</rhs>
<rhs>| 'generate-id' '(' <nt def="NT-NodeSetExpr">NodeSetExpr</nt>? ')'</rhs>
</prod>
</scrap>

</div3>

<div3 id="booleans">
<head>Booleans</head>

<p>An object of type boolean can have two values, true and false.</p>

<p>The <code>boolean()</code> function converts its argument to a
boolean as follows:</p>

<ulist>

<item><p>a number is true if and only if it is neither positive or
negative zero nor NaN</p></item>

<item><p>a node-list is true if and only if it is non-empty</p></item>

<item><p>a result fragment is true if and only if it is
non-empty</p></item>

<item><p>a string is true if and only if its length is non-zero</p></item>

</ulist>

<p>If the argument is omitted, it defaults to the context node.</p>

<p>A <nt def="NT-BooleanExpr">BooleanExpr</nt> is evaluated by
converting the result of evaluating the <nt def="NT-Expr">Expr</nt> to
a boolean as if by a call to the <code>boolean()</code> function.</p>

<p>An <code>=</code> expression is evaluated as follows. If at least
one operand is a boolean, then each operand is converted to a boolean
as if by applying the <code>boolean()</code> function and the operands
are compared as booleans.  Otherwise, if at least one operand is a
number, then each operand is converted to a number as if by applying
the <code>number()</code> function and the operands are compared as
numbers; positive and negative zero compare equal.  Otherwise both
operands are converted to strings as if by applying the
<code>string()</code> function and the operands are compared as
strings; two strings are equal if they contain the same sequence of
UCS characters.</p>

<p>A <code>&lt;</code>, <code>&gt;</code>, <code>&lt;=</code> or
<code>&gt;=</code> expression is evaluated by first converting each
operand to a number as if by a call to the <code>number()</code>
function and then comparing the two numbers.</p>

<issue id="issue-node-set-comparision"><p>What should the semantics of
comparison operators be when either or both of the operands are
node-sets?  Should there be an <quote>any</quote> or
<quote>all</quote> semantic?</p></issue>

<p>An <code>or</code> expression is evaluated by evaluating each
operand and converting its value to a boolean.  The result is true if
either value is true and false otherwise.</p>

<p>An <code>and</code> expression is evaluated by evaluating each
operand and converting its value to a boolean.  The result is true if
both values are true and false otherwise.</p>

<p>The <code>not()</code> function returns true if its argument is
false, and false otherwise.</p>

<p>The <code>true()</code> function returns true.</p>

<p>The <code>false()</code> function returns false.</p>

<p>The <code>lang()</code> function returns true or false depending on
whether the language of the context node as specified by
<code>xml:lang</code> attributes is the same as or is a sublanguage of
the language specified by the argument string.  The language of the
context node is determined by the value of the <code>xml:lang</code>
attribute on the context node, or, if the context node has no
<code>xml:lang</code> attribute, by the value of the
<code>xml:lang</code> attribute on the nearest ancestor of the context
node that has an <code>xml:lang</code> attribute.  If there is no such
attribute, then <code>lang()</code> returns false. If there is such an
attribute, then <code>lang()</code> returns true if the attribute
value is equal to the argument ignoring case, or if there is some
suffix starting with <code>-</code> such that the attribute value is
equal to the argument ignoring that suffix of the attribute value and
ignoring case. For example, <code>lang("en")</code> would return true
if the context node is any of these five elements:</p>

<eg><![CDATA[<para xml:lang="en"/>
<div xml:lang="en"><para/></div>
<para xml:lang="EN"/>
<para xml:lang="en-us"/>]]></eg>

<scrap>
<head></head>
<prod id="NT-BooleanExpr">
<lhs>BooleanExpr</lhs>
<rhs><nt def="NT-Expr">Expr</nt></rhs>
</prod>
<prod id="NT-OrExpr">
<lhs>OrExpr</lhs>
<rhs><nt def="NT-AndExpr">AndExpr</nt></rhs>
<rhs>| <nt def="NT-OrExpr">OrExpr</nt> 'or' <nt def="NT-AndExpr">AndExpr</nt></rhs>
</prod>
<prod id="NT-AndExpr">
<lhs>AndExpr</lhs>
<rhs><nt def="NT-EqualityExpr">EqualityExpr</nt></rhs>
<rhs>| <nt def="NT-AndExpr">AndExpr</nt> 'and' <nt def="NT-EqualityExpr">EqualityExpr</nt></rhs>
</prod>
<prod id="NT-EqualityExpr">
<lhs>EqualityExpr</lhs>
<rhs><nt def="NT-RelationalExpr">RelationalExpr</nt></rhs>
<rhs>| <nt def="NT-EqualityExpr">EqualityExpr</nt> '=' <nt def="NT-RelationalExpr">RelationalExpr</nt></rhs>
</prod>
<prod id="NT-RelationalExpr">
<lhs>RelationalExpr</lhs>
<rhs><nt def="NT-AdditiveExpr">AdditiveExpr</nt></rhs>
<rhs>| <nt def="NT-RelationalExpr">RelationalExpr</nt> '&lt;' <nt def="NT-AdditiveExpr">AdditiveExpr</nt></rhs>
<rhs>| <nt def="NT-RelationalExpr">RelationalExpr</nt> '>' <nt def="NT-AdditiveExpr">AdditiveExpr</nt></rhs>
<rhs>| <nt def="NT-RelationalExpr">RelationalExpr</nt> '&lt;=' <nt def="NT-AdditiveExpr">AdditiveExpr</nt></rhs>
<rhs>| <nt def="NT-RelationalExpr">RelationalExpr</nt> '>=' <nt def="NT-AdditiveExpr">AdditiveExpr</nt></rhs>
</prod>
<prod id="NT-BooleanFunctionCall">
<lhs>BooleanFunctionCall</lhs>
<rhs>'not' '(' <nt def="NT-BooleanExpr">BooleanExpr</nt> ')'</rhs>
<rhs>| 'true' '(' ')'</rhs>
<rhs>| 'false' '(' ')'</rhs>
<rhs>| 'boolean' '(' <nt def="NT-Expr">Expr</nt>? ')'</rhs>
<rhs>| 'lang' '(' <nt def="NT-StringExpr">StringExpr</nt> ')'</rhs>
</prod>
</scrap>

</div3>

<div3 id="numbers">
<head>Numbers</head>

<p>A number represents a floating point number.  A number can have any
double-precision 64-bit format IEEE 754 value.  These include a
special <quote>Not-a-Number</quote> (NaN) value, positive and negative
infinity, and positive and negative zero.</p>

<p>A <nt def="NT-NumberExpr">NumberExpr</nt> is evaluated by
converting the result of evaluating the <nt def="NT-Expr">Expr</nt> to
a number as if by a call to the <code>number()</code> function.</p>

<p>The <code>number()</code> function converts its argument to a
number as follows:</p>

<ulist>

<item>

<p>if a string parses as <nt def="NT-Number">Number</nt> possibly
preceded or followed by whitespace, then it is converted to that
number; otherwise it is converted to the number 0</p>

<ednote><edtext>Would NaN be better than 0 here?</edtext></ednote>

</item>

<item><p>boolean true is converted to 1; boolean false is converted to
0</p></item>

<item>

<p>a node-set is first converted to a string as if by a call to the
<code>string()</code> function and then converted in the same way as a
string argument</p>

<ednote><edtext>Should we take advantage of xml:lang here?</edtext></ednote>

</item>

<item><p>a result tree fragment is first converted to a string as if
by a call to the <code>string()</code> function and then converted in
the same way as a string argument</p></item>

</ulist>

<p>If the argument is omitted, it defaults to the context node.</p>

<p>The <code>div</code> operator performs floating point division
according to IEEE 754.</p>

<p>The <code>quo</code> operator performs a floating point division
and then truncates the result to an integer. For example,</p>

<ulist>
<item><p><code>5 quo 2</code> returns <code>2</code></p></item>
<item><p><code>5 quo -2</code> returns <code>-2</code></p></item>
<item><p><code>-5 quo 2</code> returns <code>-2</code></p></item>
<item><p><code>-5 quo -2</code> returns <code>2</code></p></item>
</ulist>

<p>The <code>mod</code> operator returns the remainder from the
<code>quo</code> operation.  For example,</p>

<ulist>
<item><p><code>5 mod 2</code> returns <code>1</code></p></item>
<item><p><code>5 mod -2</code> returns <code>1</code></p></item>
<item><p><code>-5 mod 2</code> returns <code>-1</code></p></item>
<item><p><code>-5 mod -2</code> returns <code>-1</code></p></item>
</ulist>

<note><p>This is the same as the <code>%</code> operator in Java and
ECMAScript.</p></note>

<note><p>This is not the same as the IEEE remainder operation which
returns the remainder from a rounding division.</p></note>

<p>The <code>sum()</code> function returns the sum of the values of
the nodes in the argument node-set.</p>

<p>The <code>floor()</code> function returns the largest (closest to
positive infinity) number that is not greater than the argument and
that is an integer.</p>

<p>The <code>ceiling()</code> function returns the smallest (closest
to negative infinity) number that is not less than the argument and
that is an integer.</p>

<p>The <code>round()</code> function returns the number that is
closest to the argument and that is an integer.  If there are two such
numbers, then the one that is even is returned.</p>

<scrap>
<head>Numeric Expressions</head>
<prodgroup pcw5="1" pcw2="10" pcw4="21">
<prod id="NT-AdditiveExpr">
<lhs>AdditiveExpr</lhs>
<rhs><nt def="NT-MultiplicativeExpr">MultiplicativeExpr</nt></rhs>
<rhs>| <nt def="NT-AdditiveExpr">AdditiveExpr</nt> '+' <nt def="NT-MultiplicativeExpr">MultiplicativeExpr</nt></rhs>
<rhs>| <nt def="NT-AdditiveExpr">AdditiveExpr</nt> '-' <nt def="NT-MultiplicativeExpr">MultiplicativeExpr</nt></rhs>
</prod>
<prod id="NT-MultiplicativeExpr">
<lhs>MultiplicativeExpr</lhs>
<rhs><nt def="NT-UnaryExpr">UnaryExpr</nt></rhs>
<rhs>| <nt def="NT-MultiplicativeExpr">MultiplicativeExpr</nt> <nt def="NT-MultiplyOperator">MultiplyOperator</nt> <nt def="NT-UnaryExpr">UnaryExpr</nt></rhs>
<rhs>| <nt def="NT-MultiplicativeExpr">MultiplicativeExpr</nt> 'div' <nt def="NT-UnaryExpr">UnaryExpr</nt></rhs>
<rhs>| <nt def="NT-MultiplicativeExpr">MultiplicativeExpr</nt> 'mod' <nt def="NT-UnaryExpr">UnaryExpr</nt></rhs>
<rhs>| <nt def="NT-MultiplicativeExpr">MultiplicativeExpr</nt> 'quo' <nt def="NT-UnaryExpr">UnaryExpr</nt></rhs>
</prod>
<prod id="NT-UnaryExpr">  
<lhs>UnaryExpr</lhs>
<rhs><nt def="NT-UnionExpr">UnionExpr</nt></rhs>
<rhs>| '-' <nt def="NT-UnaryExpr">UnaryExpr</nt></rhs>
</prod>
<prod id="NT-NumberExpr">
<lhs>NumberExpr</lhs>
<rhs><nt def="NT-Expr">Expr</nt></rhs>
</prod>
<prod id="NT-NumberFunctionCall">
<lhs>NumberFunctionCall</lhs>
<rhs>'number' '(' <nt def="NT-Expr">Expr</nt>? ')'</rhs>
<rhs>| 'floor' '(' <nt def="NT-NumberExpr">NumberExpr</nt> ')'</rhs>
<rhs>| 'ceiling' '(' <nt def="NT-NumberExpr">NumberExpr</nt> ')'</rhs>
<rhs>| 'round' '(' <nt def="NT-NumberExpr">NumberExpr</nt> ')'</rhs>
<rhs>| 'sum' '(' <nt def="NT-NodeSetExpr">NodeSetExpr</nt> ')'</rhs>
</prod>
</prodgroup>
</scrap>

</div3>

<div3 id="strings">
<head>Strings</head>

<p>A string consists of a sequence of UCS characters.</p>

<p>A <nt def="NT-StringExpr">StringExpr</nt> is evaluated by
converting the result of evaluating the <nt def="NT-Expr">Expr</nt> to
a string as if by a call to the <code>string()</code> function.</p>

<p>The <code>string()</code> function converts an object to a string
as follows:</p>

<ulist>

<item><p>A node-set is converted to a string by returning the value of
the node in the node-set that is first in document order.  If the
node-set is empty, an empty string is returned.</p></item>

<item><p>A result tree fragment is converted to a string by treating
it as a single document fragment node that contains the nodes of the
fragment, and then converting that document fragment node to a string
in the same was as if the document fragment node were a source tree
node.</p></item>

<item><p>A number is converted to a string by returning a string in
the form of a <nt def="NT-Number">Number</nt>, preceded by a
<code>-</code> character if the number is negative.</p>

<ednote><edtext>What about positive zero, negative zero, NaN and
infinities?</edtext></ednote>

</item>

<item><p>The boolean false value is converted to the string
<code>false</code>.  The boolean true value is converted to the
string <code>true</code>.</p></item>

</ulist>

<p>If the argument is omitted, it defaults to the context node.</p>

<p>The <code>concat()</code> function returns the concatenation of its
arguments.</p>

<p>The <code>starts-with()</code> function returns true if the first
argument string starts with the second argument string, and
otherwise returns false.</p>

<p>The <code>contains()</code> function returns true if the first
argument string contains the second argument string, and otherwise
returns false.</p>

<p>The <code>substring-before()</code> function returns the substring
of the first argument string that precedes the first occurrence of the
second argument string in the first argument string, or the empty
string if the first argument string does not contain the second
argument string.  For example,
<code>substring-before("1999/04/01","/")</code> returns
<code>1999</code>.</p>

<p>The <code>substring-after()</code> function returns the substring
of the first argument string that follows the first occurrence of the
second argument string in the first argument string, or the empty
string if the first argument string does not contain the second
argument string. For example,
<code>substring-after("1999/04/01","/")</code> returns
<code>04/01</code>.</p>

<ednote><edtext>Should the first argument of the above functions
default to the value of the current node?</edtext></ednote>

<p>The <code>normalize()</code> function returns the argument string
with white space normalized by stripping leading and trailing
whitespace and replacing sequences of whitespace characters by a
single space.  Whitespace characters are the same allowed by the <xnt
href="&XML;#NT-S">S</xnt> production in XML.  If the argument is
omitted, it defaults to the context node converted to a string, in
other words the value of the context node.</p>

<p>The <code>translate()</code> function returns the first argument
string with occurrences of characters in the second argument string
replaced by the corresponding characters from the third argument
string.  For example, <code>translate("bar","abc","ABC")</code> returns
the string <code>BAr</code>.</p>

<p>The <code>format-number()</code> function converts its first
argument to a string using the format pattern string specified by the
second argument and the locale named by the third argument, or the
default locale, if there is no third argument.  The format pattern
string is in the syntax specified by the JDK 1.1 <loc href=
"http://java.sun.com/products/jdk/1.1/docs/api/java.text.DecimalFormat.html"
>DecimalFormat</loc> class. The format pattern string is in a
localized notation: the locale determines what characters have a
special meaning in the pattern (with the exception of the quote
character, which is not localized).  The format pattern must not
contain the currency sign (#x00A4); support for this feature was added
after the initial release of JDK 1.1.  See <specref ref="locales"/>
for how to declare a locale.</p>

<scrap>
<head></head>
<prod id="NT-StringExpr">
<lhs>StringExpr</lhs>
<rhs><nt def="NT-Expr">Expr</nt></rhs>
</prod>
<prod id="NT-StringFunctionCall">
<lhs>StringFunction </lhs>
<rhs>'string' '(' <nt def="NT-Expr">Expr</nt>? ')'</rhs>
<rhs>| 'starts-with' '(' <nt def="NT-StringExpr">StringExpr</nt> ',' <nt def="NT-StringExpr">StringExpr</nt> ')'</rhs>
<rhs>| 'contains' '(' <nt def="NT-StringExpr">StringExpr</nt> ',' <nt def="NT-StringExpr">StringExpr</nt> ')'</rhs>
<rhs>| 'substring-before '(' <nt def="NT-StringExpr">StringExpr</nt> ',' <nt def="NT-StringExpr">StringExpr</nt> ')'</rhs>
<rhs>| 'substring-after '(' <nt def="NT-StringExpr">StringExpr</nt> ',' <nt def="NT-StringExpr">StringExpr</nt> ')'</rhs>
<rhs>| 'normalize' '(' <nt def="NT-StringExpr">StringExpr</nt>? ')'</rhs>
<rhs>| 'translate' '(' <nt def="NT-StringExpr">StringExpr</nt> ',' <nt def="NT-StringExpr">StringExpr</nt> ',' <nt def="NT-StringExpr">StringExpr</nt> ')'</rhs>
<rhs>| 'concat' '(' <nt def="NT-StringExpr">StringExpr</nt> (',' <nt def="NT-StringExpr">StringExpr</nt>)+ ')'</rhs>
<rhs>| 'format-number' '(' <nt def="NT-NumberExpr">NumberExpr</nt> ',' <nt def="NT-StringExpr">StringExpr</nt> (',' <nt def="NT-StringExpr">StringExpr</nt>)? ')'</rhs>
</prod>
</scrap>

<issue id="issue-regex"><p>Should XSLT support regular
expressions for matching against any or all of pcdata content,
attribute values, attribute names, element type names?</p></issue>

<issue id="issue-equality-case"><p>Do we need to be able to do
comparisons in a case insensitive way?</p></issue>

<issue id="issue-equality-normalize"><p>Do we need to normalize
strings before comparison?  Does the stylesheet need to specify what
kinds of normalization are required (eg compatibility character
normalization)?</p></issue>

<issue id="issue-resolve-expr"><p>Do we need a
<code>resolve(NodeSetExpr)</code> string expression that treats the
characters as a relative URI and turns it into an absolute URI using
the base URI of the addressed node?</p></issue>

</div3>

<div3>
<head>Result Tree Fragments</head>

<ednote><edtext>Add explanation of what a result tree fragment
is.</edtext></ednote>

<p>The only operations that can be performed on a result tree fragment
are to convert it to a string or a boolean.  In particular, it is not
permitted to use the <code>/</code>, <code>//</code>, and
<code>[]</code> on result tree fragments.</p>

<p>Expressions can only return values of type result tree fragment
by referencing variables of type result tree fragment or calling
extension functions that return a result tree fragment.</p>

</div3>

<div3>
<head>Extension Functions</head>

<scrap>
<head></head>
<prodgroup pcw5="1" pcw2="10">
<prod id="NT-ExtensionFunctionCall">
<lhs>ExtensionFunctionCall</lhs>
<rhs><nt def="NT-CName">CName</nt> '(' ( <nt def="NT-Expr">Expr</nt> ( ',' <nt def="NT-Expr">Expr</nt>)*)? ')'</rhs>
</prod>
</prodgroup>
</scrap>

<p>The <nt def="NT-CName">CName</nt> is expanded to a name using the
namespace declarations from the evaluation context.  The XSLT
processor attempts to locate an implementation of the extension
function with the specified name that it can use.  The implementation
may be provided by an <code>xsl:functions</code> element (see <specref
ref="functions"/>) or the XSLT processor may be able to locate an
implementation by other means not specified by XSLT.  If the XSLT
processor cannot locate such a function, then evaluating the
expression is an error.  Otherwise the implementation is called
passing it the values of the expressions and the value returned by the
function is the value of the expression.  The
<code>function-available()</code> function can be used to test whether
an implementation of a particular function is available (see <specref
ref="system"/>).  A XSLT processor is allowed always to give an error
when evaluating an <nt
def="NT-ExtensionFunctionCall">ExtensionFunctionCall</nt> (with such
an XSLT processor the <code>function-available()</code> function would
always return false).  Therefore if an XSLT stylesheet includes an <nt
def="NT-ExtensionFunctionCall">ExtensionFunctionCall</nt> and does not
use the <code>function-available()</code> function to test for and
handle the possibility that an implementation of the function is not
available, then it may not be portable across all XSLT
implementations.</p>

</div3>

<div3 id="system">
<head>System Functions</head>

<scrap>
<head></head>
<prod id="NT-SystemFunctionCall">
<lhs>SystemFunctionCall</lhs>
<rhs>'system-property' '(' <nt def="NT-StringExpr">StringExpr</nt> ')'</rhs>
<rhs>| 'function-available' '(' <nt def="NT-StringExpr">StringExpr</nt> ')'</rhs>
</prod>
</scrap>

<p>For both these functions, the <nt
def="NT-StringExpr">StringExpr</nt> must evaluate to a string that is
a <xnt href="&XMLNames;#NT-QName">QName</xnt>.  The <xnt
href="&XMLNames;#NT-QName">QName</xnt> is expanded into a name using
the namespace declarations in scope for the expression.</p>

<p>The <code>system-property()</code> function returns an object
representing the value of the system property identified by the name.
If the name has no namespace, then the <code>system-property()</code>
function must return the string value of the operating system
environment variable whose name is equal to the local part of the
name.  If there is no such system property, the empty string should be
returned.</p>

<p>Implementations must provide the following system properties which
are all in the XSLT namespace:</p>

<slist>

<sitem><code>xsl:version</code>, a number giving the version
of XSLT implemented by the processor</sitem>

</slist>

<p>The <code>function-available()</code> function returns true if an
implementation of the named extension function is available. For
example:</p>

<eg><![CDATA[<xsl:if test="function-available('jjc:currentDate')">
  <xsl:value-of select="jjc:currentDate()"/>
</xsl:if>]]></eg>

</div3>

<div3 id="exprlex">
<head>Lexical Structure</head>

<p>When tokenizing, the longest possible token is always returned.</p>

<p>For readability, whitespace may be used in patterns even though not
explicitly allowed by the grammar: <nt
def="NT-ExprWhitespace">ExprWhitespace</nt> may be freely added within
patterns before or after any <nt
def="NT-ExprToken">ExprToken</nt>.</p>

<p>A <nt def="NT-NodeType">NodeType</nt>, <nt
def="NT-FunctionName">FunctionName</nt>, <nt def="NT-CName">CName</nt>
or <nt def="NT-AxisIdentifier">AxisIdentifier</nt> token is recognized
only when the following token is <code>(</code>.  An <nt
def="NT-OperatorName">OperatorName</nt> token or <nt
def="NT-MultiplyOperator">MultiplyOperator</nt> token is recognized as
such only when there is a preceding token and the preceding token is
not one of <code>@</code>, <code>(</code>, <code>[</code>,
<code>,</code> or an <nt def="NT-Operator">Operator</nt>.</p>

<scrap>
<head>Expression Lexical Structure</head>
<prodgroup pcw5="1" pcw2="8" pcw4="21">
<prod id="NT-ExprToken">
<lhs>ExprToken</lhs>
<rhs>'(' | ')' | '[' | ']' | '.' | '..' | '@' | ','</rhs>
<rhs>| <nt def="NT-WildcardName">WildcardName</nt></rhs>
<rhs>| <nt def="NT-NodeType">NodeType</nt></rhs>
<rhs>| <nt def="NT-Operator">Operator</nt></rhs>
<rhs>| <nt def="NT-FunctionName">FunctionName</nt></rhs>
<rhs>| <nt def="NT-AxisIdentifier">AxisIdentifier</nt></rhs>
<rhs>| <nt def="NT-Literal">Literal</nt></rhs>
<rhs>| <nt def="NT-Number">Number</nt></rhs>
<rhs>| <nt def="NT-CName">CName</nt></rhs>
<rhs>| <nt def="NT-VariableReference">VariableReference</nt></rhs>
</prod>
<prod id="NT-Literal">
<lhs>Literal</lhs>
<rhs>'"' [^"]* '"'</rhs>
<rhs>| "'" [^']* "'"</rhs>
</prod>
<prod id="NT-Number">
<lhs>Number</lhs>
<rhs>[0-9]+ ('.' [0-9]+)?</rhs>
<rhs>| '.' [0-9]+</rhs>
</prod>
<prod id="NT-Operator">
<lhs>Operator</lhs>
<rhs><nt def="NT-OperatorName">OperatorName</nt></rhs>
<rhs>| <nt def="NT-MultiplyOperator">MultiplyOperator</nt></rhs>
<rhs>| '/' | '//' | '|' | '+' | '-' | '=' | '&lt;'| '&lt;=' | '&gt;' | '&gt;='</rhs>
</prod>
<prod id="NT-OperatorName">
<lhs>OperatorName</lhs>
<rhs>'and' | 'or' | 'mod' | 'div' | 'quo'</rhs>
</prod>
<prod id="NT-MultiplyOperator">
<lhs>MultiplyOperator</lhs>
<rhs>'*'</rhs>
</prod>
<prod id="NT-FunctionName">
<lhs>FunctionName</lhs>
<rhs>'boolean'</rhs>
<rhs>| 'ceiling'</rhs>
<rhs>| 'concat'</rhs>
<rhs>| 'contains'</rhs>
<rhs>| 'count'</rhs>
<rhs>| 'doc'</rhs>
<rhs>| 'docref'</rhs>
<rhs>| 'false'</rhs>
<rhs>| 'floor'</rhs>
<rhs>| 'format-number'</rhs>
<rhs>| 'function-available'</rhs>
<rhs>| 'generate-id'</rhs>
<rhs>| 'id'</rhs>
<rhs>| 'idref'</rhs>
<rhs>| 'key'</rhs>
<rhs>| 'keyref'</rhs>
<rhs>| 'lang'</rhs>
<rhs>| 'last'</rhs>
<rhs>| 'local-part'</rhs>
<rhs>| 'namespace'</rhs>
<rhs>| 'normalize'</rhs>
<rhs>| 'not'</rhs>
<rhs>| 'number'</rhs>
<rhs>| 'position'</rhs>
<rhs>| 'qname'</rhs>
<rhs>| 'round'</rhs>
<rhs>| 'starts-with'</rhs>
<rhs>| 'string'</rhs>
<rhs>| 'substring-after'</rhs>
<rhs>| 'substring-before'</rhs>
<rhs>| 'sum'</rhs>
<rhs>| 'system-property'</rhs>
<rhs>| 'translate'</rhs>
<rhs>| 'true'</rhs>
</prod>
<prod id="NT-CName">
<lhs>CName</lhs>
<rhs><xnt href="&XMLNames;#NT-NCName">NCName</xnt> ':' <xnt href="&XMLNames;#NT-NCName">NCName</xnt></rhs>
</prod>
<prod id="NT-VariableReference">
<lhs>VariableReference</lhs>
<rhs>'$' <xnt href="&XMLNames;#NT-NCName">NCName</xnt></rhs>
</prod>
<prod id="NT-WildcardName">
<lhs>WildcardName</lhs>
<rhs>'*'</rhs>
<rhs>| <xnt href="&XMLNames;#NT-NCName">NCName</xnt> ':' '*'</rhs>
<rhs>| <xnt href="&XMLNames;#NT-QName">QName</xnt></rhs>
</prod>
<prod id="NT-NodeType">
<lhs>NodeType</lhs>
<rhs>'comment'</rhs>
<rhs>| 'text'</rhs>
<rhs>| 'pi'</rhs>
<rhs>| 'node'</rhs>
</prod>
<prod id="NT-ExprWhitespace">
<lhs>ExprWhitespace</lhs>
<rhs><xnt href="&XML;#NT-S">S</xnt></rhs>
</prod>
</prodgroup>
</scrap>

</div3>

</div2>

<div2>
<head>Patterns</head>

<p>This section explains what expressions are allowed as patterns and
what the semantics of matching a pattern are.</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>pi()</code> matches any processing instruction</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 from-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 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
set of location path patterns separated by <code>|</code>.  A location
path pattern is a location path none of the steps of which use either
<nt def="NT-AxisIdentifier">AxisIdentifier</nt>s or <code>.</code> or
<code>..</code>.  Location path patterns can also start with an
<code>id()</code> or <code>key()</code> function call with a literal
argument (see <specref ref="node-sets"/>).  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' '(' <nt def="NT-Literal">Literal</nt> ')'</rhs>
<rhs>| 'key' '(' <nt def="NT-Literal">Literal</nt> ',' <nt def="NT-Literal">Literal</nt> ')'</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-AbbreviatedNodeTestStep">AbbreviatedNodeTestStep</nt></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's a <nt
def="NT-NodeTest">NodeTest</nt> matches if the <nt
def="NT-NodeTest">NodeTest</nt> is true for the node and the node is
not an attribute node.  A <nt def="NT-StepPattern">StepPattern</nt>
that starts with <code>@</code> matches if the node is an attribute
node and the <nt def="NT-WildcardName">WildcardName</nt> matches the
name of the attribute.  When <code>[]</code> is present, then the
first <nt def="NT-PredicateExpr">PredicateExpr</nt> in a <nt
def="NT-StepPattern">StepPattern</nt> is evaluated with the element
being matched as the context node and the siblings of the elements
that match the <nt def="NT-NodeTest">NodeTest</nt> 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 <nt
def="NT-WildcardName">WildcardName</nt>.</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 <nt def="NT-NodeTest">NodeTest</nt> <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 <nt def="NT-PredicateExpr">PredicateExpr</nt>
<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>

<ednote><edtext>Need to revise above paragraph if we decide not to
call the element to which an attribute is attached the parent of the
attribute.</edtext></ednote>

</div2>

<div2>
<head>Declarations</head>

<div3 id="key">
<head>Declaring Keys</head>

<p>The <code>xsl:key</code> element is used to declare keys.  The
<code>name</code> specifies the name of the key.  The
<code>match</code> attribute is a <nt def="NT-Pattern">Pattern</nt>; a
<code>xsl:key</code> element gives information about the keys of any
node that match the pattern specified in the match attribute.  The
<code>use</code> attribute is a <nt
def="NT-NodeSetExpr">NodeSetExpr</nt>, which specifies the set of
values for which the node has a key of the specified name.  A node
<emph>x</emph> has a key with name <emph>y</emph> and value
<emph>z</emph> if and only if there is a <code>xsl:key</code> element
such that:</p>

<ulist>

<item><p><emph>x</emph> matches the pattern specified in the
<code>match</code> attribute of the <code>xsl:key</code> element;</p></item>

<item><p>the value of the <code>name</code> attribute of the
<code>xsl:key</code> element is equal to <emph>y</emph>;
and</p></item>

<item><p><emph>z</emph> is the value of one or more of the nodes in
the node-set that results from evaluating the <nt
def="NT-NodeSetExpr">NodeSetExpr</nt> specified in the
<code>use</code> attribute of the <code>xsl:key</code> element with
<emph>x</emph> as the current node and with a node list containing
just <emph>x</emph> as the current node list.</p></item>

</ulist>

<p>Note that the <nt def="NT-NodeSetExpr">NodeSetExpr</nt> may return
a node-set with more than one node; all of the returned nodes serve as
a key value.  Note also that there may be more than one
<code>xsl:key</code> element that matches a given node; all of the
matching <code>xsl:key</code> elements are used.</p>

<ednote><edtext>Add some examples.</edtext></ednote>

</div3>

<div3 id="functions">
<head>Declaring Extension Functions</head>

<p>Implementations of the extension functions in a namespace can be
provided using the <code>xsl:functions</code> element.  The required
<code>ns</code> attribute specifies the namespace for which an
implementation is being provided. The value of the <code>ns</code>
attribute is a namespace prefix which is expanded to a namespace URI
using the namespace declarations in effect on the
<code>xsl:functions</code> element.</p>

<p>The implementation may be provided in two ways.  If the
<code>code</code> attribute is present, then its value is a URI that
identifies a resource containing an implementation of the functions in
the namespace; in this case a <code>type</code> attribute giving the
MIME media type of the data providing the implementation may also be
provided, so as to allow the XSLT processor to avoid fetching
resources of types that it is unable to make use of.  If the
<code>code</code> attribute is not present, then the content of the
<code>xsl:functions</code> element contains the implementation of the
functions; in this case the <code>type</code> attribute
<emph>must</emph> be present.</p>

<p>Multiple alternative implementations may be provided for the same
namespace.  For example,</p>

<eg><![CDATA[<xsl:stylesheet
 xmlns:xsl="http://www.w3.org/XSL/Transform/1.0"
 xmlns:jjc="http://www.jclark.com">

<xsl:template match="/">
  <xsl:value-of select="jjc:currentDate()"/>
</xsl:template>

<xsl:functions ns="jjc" type="text/javascript">

function currentDate() {
 return Date().toString()
}

<p>When multiple alternative implementations are provided, it is up to
the XSLT processor to determine which to use.</p>

</xsl:functions>

<xsl:functions ns="jjc" code="clsid:9A447990-E64D-11D2-8C29-00A0CC2428DA"/>

</xsl:stylesheet>]]></eg>

<p>The <code>xsl:functions</code> element may also have an
<code>archive</code> attribute that specifies a whitespace-separated
list of URIs of resources relevant to the provided implementation.</p>

<p>An XSLT processor is not required to be able to make use of
implementations provided by <code>xsl:functions</code> elements.  The
MIME media types that an XSLT processor is able to make use of and the
way the XSLT processor interfaces with implementations is dependent on
the particular XSLT processor.  Therefore if an XSLT stylesheet
includes an <nt
def="NT-ExtensionFunctionCall">ExtensionFunctionCall</nt> of an
extension function in a namespace for which an implementation is
provided by an <code>xsl:functions</code> element, then it may not be
portable across all XSLT implementations.</p>

</div3>

<div3 id="locales">
<head>Declaring Locales</head>

<p>The <code>xsl:locale</code> element declares a locale which
controls the interpretation of a format pattern used by the
<code>format-number()</code> function.  If there is a
<code>name</code> attribute then the element declares a named locale,
otherwise it declares the default locale.</p>

<p>The other attributes on <code>xsl:locale</code> correspond to the
methods on the JDK 1.1 <loc href=
"http://java.sun.com/products/jdk/1.1/docs/api/java.text.DecimalFormatSymbols.html"
>DecimalFormatSymbols</loc> class.  For each
<code>get</code>/<code>set</code> method pair there is an attribute
defined for the <code>xsl:locale</code> element.  The default values
are given in the XSLT DTD (see <specref ref="dtd"/>).</p>

<p>The following attributes specify characters that may appear in the
result of formatting the number and also control the interpretation of
characters in the format pattern:</p>

<ulist>

<item><p><code>decimal-separator</code> specifies the character used
for the decimal sign</p></item>

<item><p><code>grouping-separator</code> specifies the character used
as a grouping (eg thousands) separator</p></item>

<item><p><code>percent</code> specifies the character used as a
percent sign</p></item>

<item><p><code>per-mill</code> specifies the character used as a per
mille sign</p></item>

<item><p><code>zero-digit</code> specifies the character used as the
digit zero</p></item>

</ulist>

<p>The following attributes control the interpretation of characters
in the format pattern:</p>

<ulist>

<item><p><code>digit</code> specifies the character used for a digit
in the format pattern</p></item>

<item><p><code>pattern-separator</code> specifies the character used
to separate positive and negative subpatterns in a pattern</p></item>

</ulist>

<p>The following attributes specify strings that may appear in the
result of formatting the number:</p>

<ulist>

<item><p><code>infinity</code> specifies the string used to represent
infinity</p></item>

<item><p><code>NaN</code> specifies the string used to represent the
NaN value</p></item>

<item><p><code>minus-sign</code> specifies the string used as the
default minus sign</p></item>

</ulist>

</div3>

</div2>

</div1>

<div1>
<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
for the node.  <termdef id="dt-current-node" term="Current
Node">During the instantiation of a template, the node for which the
template is being instantiated is called the <term>current
node</term>;</termdef> <termdef id="dt-current-node-list"
term="Current Node List">the list of source nodes that is being
processed is called the <term>current node list</term>.</termdef> The
current node is always a member of the current node list.  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>
<head>Defining Template Rules</head>

<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"/>).
The content of the <code>xsl:template</code> element is the
template.</p>

<issue id="issue-template-match-default"><p>Should the
<code>match</code> attribute have a default? Any node? Any child
node? The root node?</p></issue>

<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
has a template which 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>

<p>As described later, the <code>xsl:apply-templates</code> element
recursively processes the children of the source element.</p>

</div2>

<div2>
<head>Applying Template Rules</head>

<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.</p>

<ednote><edtext>There is no WG consensus on the use of
xsl:apply-templates without a select attribute to process all children
of a node.</edtext></ednote>

<p>A <code>select</code> attribute can be used to process nodes
selected by an expression instead of all children.  The value of the
<code>select</code> attribute is a <nt
def="NT-NodeSetExpr">NodeSetExpr</nt>.  The selected set of nodes are
processed in <termref def="dt-document-order">document
order</termref>, 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>first-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/first-name"/>
  </fo:inline-sequence>
</xsl:template>]]></eg>

<p>This example processes all of the <code>heading</code> elements
contained in 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 contains <code>group</code> and <code>employee</code> elements
(at some level). 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="from-ancestors(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>

<p>Use of expressions in <code>xsl:apply-templates</code> can lead to
infinite loops.  It is an error if, during the invocation of a rule
for a node, that same rule is invoked again for that node.  An
XSLT processor may signal the error; if it does not signal the error,
it must recover by creating an empty result tree structure for the
nested invocation.</p>

<ednote><edtext>This isn't right with parameters.</edtext></ednote>

<ednote><edtext>Also doesn't apply to built-in rules because
they can be invoked in multiple modes.</edtext></ednote>

</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 are less
<termref def="dt-important">important</termref> than the most
important matching template rule or rules are eliminated from
consideration.</p></item>

<item><p>Next, all matching template rules that have a 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).  The
default priority is computed as follows:</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> optionally preceded by an
<code>@</code> character, then the priority is 0.</p></item>

<item><p>Otherwise if the pattern consists of just a <nt
def="NT-NodeTest">NodeTest</nt>, then the priority is -1.</p></item>

<item><p>Otherwise the priority is 1.</p></item>

</ulist>

<p>The idea is that the most common kind of pattern (a pattern that
just tests for an element with a specific name) has priority 0; a
pattern more specific than this has priority 1; a pattern less
specific than this has priority -1.</p>

<ednote><edtext>Say exactly what syntax is allowed for real
numbers.</edtext></ednote>

</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>
<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
rule in the stylesheet.  This 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 text nodes that copies
text through:</p>

<eg><![CDATA[<xsl:template match="text()">
  <xsl:value-of select="."/>
</xsl:template>]]></eg>

<p>The built-in rule does not apply to processing instructions and
comments.  When a comment or processing instruction is processed, and
no rule is matched, nothing is created.</p>

<p>The built-in template rules are treated as if they were imported
implicitly before the stylesheet and so are considered less <termref
def="dt-important">important</termref> than all other template rules.
Thus the author can override a built-in rule by including an
explicit rule with <code>match="*|/"</code> or
<code>match="text()"</code>.</p>

</div2>

<div2>
<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 whose value is a name.
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>

<p>If there is no matching template, then the built-in template rules
are applied, even if a <code>mode</code> attribute was specified in
<code>xsl:apply-templates</code>.</p>

<ednote><edtext>Add some examples.</edtext></ednote>

</div2>

</div1>

<div1 id="named-templates">
<head>Named Templates</head>

<p>Templates can be invoked by name.  An <code>xsl:template</code>
element with a <code>name</code> attribute specifies a named template.
If a <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>

<ednote><edtext>Expand this.</edtext></ednote>

<p>It is an error if a stylesheet contains more than one template with
the same name and same <termref
def="dt-important">importance</termref>.  An XSLT processor may signal
the error; if it does not signal the error, it must recover by
choosing from amongst the templates with highest importance the one
that occurs last in the stylesheet.</p>

</div1>


<div1>
<head>Creating the Result Tree</head>

<p>This section describes instructions that directly create nodes in
the result tree.</p>

<issue id="issue-multiple-results"><p>Should it be possible to create
multiple result trees?</p></issue>

<div2>
<head>Creating Elements and Attributes</head>

<div3>
<head>Literal Result Elements</head>

<p>In a template an element in the stylesheet that does not belong to
the XSLT namespace is instantiated to create an element node of the
same type.  The created element node will have the attribute nodes
that were present on the element node in the stylesheet tree.  The
created element node will also have the namespace nodes that were
present on the element node in the stylesheet tree with the exception
of any namespace node whose value is the XSLT namespace URI
(<code>http://www.w3.org/XSL/Transform/1.0</code>).</p>

<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 string expressions contained
in curly braces (<code>{}</code>).</p>

<p>Namespace URIs that occur literally in the stylesheet and that are
being used to create nodes in the result tree can be quoted.  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 value of a namespace node on a literal result element in
the stylesheet</p></item>

</ulist>

<p>A namespace URI is quoted by prefixing it with the string
<code>quote:</code>.  This prefix will be removed when the template is
instantiated to create the result element node with its associated
attribute nodes and namespace nodes.</p>

<p>When literal result elements are being used to create element,
attribute, or namespace nodes that use the XSLT namespace URI, the
namespace must be quoted to avoid misinterpretation by the XSLT
processor.</p>

<note><p>It may be necessary also to quote other namespaces.  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; quoting the namespace would avoid
the possibility of such mishandling.</p></note>

<p>For example, the stylesheet</p>

<eg><![CDATA[<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/XSL/Transform/1.0"
  xmlns:fo="http://www.w3.org/XSL/Format/1.0"
  xmlns:qxsl="quote:http://www.w3.org/XSL/Transform/1.0">

<xsl:template match="/">
  <qxsl:stylesheet result-ns="fo">
    <xsl:apply-templates/>
  </qxsl:stylesheet>
</xsl:template>

<xsl:template match="block">
  <qxsl:template match="{.}">
     <fo:block><qxsl:apply-templates/></fo:block>
  </qxsl: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>

</div3>

<div3>
<head>Creating Elements with <code>xsl:element</code></head>

<p>The <code>xsl:element</code> element allows an element to be
created with a computed name.  The name 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>;
the string value from instantiating it must be a <xnt
href="&XMLNames;#NT-QName">QName</xnt>.  If the <code>namespace</code>
attribute is not present, then the <xnt
href="&XMLNames;#NT-QName">QName</xnt> is expanded into a name using
the namespace declarations in effect for the <code>xsl:element</code>
element, including any default namespace declaration.</p>

<ednote><edtext>What should happen if the name is not a valid
QName?</edtext></ednote>

<p>If the <code>namespace</code> attribute is present, then it also
interpreted as an <termref def="dt-attribute-value-template">attribute
value template</termref>; the string value from instantiating it
should be a URI reference. This is used as the namespace of the 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 name of
the element to be created.</p>

<ednote><edtext>What should happen if the namespace is not a
syntactically legal URI reference? Should URI escaping automatically
be done?</edtext></ednote>

<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.  They are not however required
to do so.</p>

</div3>

<div3 id="creating-attributes">
<head>Creating Attributes with <code>xsl:attribute</code></head>

<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 name 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>;
the string value from instantiating it must be a <xnt
href="&XMLNames;#NT-QName">QName</xnt>.  If the <code>namespace</code>
attribute is not present, then the <xnt
href="&XMLNames;#NT-QName">QName</xnt> is expanded into a name using
the namespace declarations in effect for the
<code>xsl:attribute</code> element, <emph>not</emph> including any
default namespace declaration.</p>

<ednote><edtext>What should happen if the name is not a valid
QName?</edtext></ednote>

<p>If the <code>namespace</code> attribute is present, then it also
interpreted as an <termref def="dt-attribute-value-template">attribute
value template</termref>; the string value from instantiating it
should be a URI reference. This is used as the namespace of the 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 name of
the attribute to be created.</p>

<ednote><edtext>What should happen if the namespace is not a
syntactically legal URI reference? Should URI escaping automatically
be done?</edtext></ednote>

<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.  They are not however
required to do so.</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 that has the same name as an attribute
already added; implementations may either signal the error or ignore
the duplicate 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 anything other than characters 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>

</div3>

<div3>

<head>Named Attribute Sets</head>

<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 <code>xsl:use</code> element adds a named set of
attributes to an element.  It has a required
<code>attribute-set</code> attribute that specifies the name of the
attribute set.  <code>xsl:use</code> is allowed in the same places as
<code>xsl:attribute</code>.  The content of the
<code>xsl:attribute-set</code> consists of <code>xsl:attribute</code>
elements that specify attributes; it may also contain
<code>xsl:use</code> elements.  The value of attributes in an attribute
set is determined when the attribute set is used rather than when the
attribute set is defined.</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:attribute-set name="title-style">
  <xsl:attribute name="font-size">12pt</xsl:attribute>
  <xsl:attribute name="font-weight">bold</xsl:attribute>
</xsl:attribute-set>

<xsl:template match="chapter/heading">
  <fo:block quadding="start">
    <xsl:use attribute-set="title-style"/>
    <xsl:apply-templates/>
  </fo:block>
</xsl:template>]]></eg>

<p>Any attribute in a named attribute set specified by
<code>xsl:use</code> is not added to an element if the element already
has an attribute of that name.</p>

<p>Multiple definitions of an attribute set with the same name are
merged.  An attribute from a definition that is more <termref
def="dt-important">important</termref> takes precedence over an
attribute from a definition that is less <termref
def="dt-important">important</termref>.  It is an error if there are
two attribute sets with the same name that are equally important and
that both contain the same attribute unless there is a more <termref
def="dt-important">important</termref> definition of the attribute set
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 most important definitions that specify the attribute
the one that was specified last in the stylesheet.</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 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.</p>

<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>

</div2>


<div2>
<head>Creating Processing Instructions</head>

<p>The <code>xsl:pi</code> element is instantiated to create a
processing instruction node.  The content of the <code>xsl:pi</code>
element is a template for the value of the processing instruction
node.  The <code>xsl:pi</code> element has a required
<code>name</code> attribute that specifies the name of the processing
instruction node.  The value of the name attribute is interpreted as
an <termref def="dt-attribute-value-template">attribute value
template</termref>.</p>

<p>For example, this</p>

<eg><![CDATA[<xsl:pi name="xml-stylesheet">href="book.css" type="text/css"</xsl:pi>]]></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 instantiating the content of <code>xsl:pi</code>
creates anything other than characters.  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:pi</code> contains the string <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 an <code>></code>.</p>

<ednote><edtext>What should happen if the name is not a valid
NCName?</edtext></ednote>

</div2>

<div2>
<head>Creating Comments</head>

<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 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 anything other than characters.  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>

<p>The <code>xsl:copy</code> element provides an easy way of copying
the current node. The <code>xsl:copy</code> element is replaced by 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 not used for nodes of
types that do not have attributes or children (attributes, text,
comments and processing instructions).</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="*|@*|comment()|pi()|text()">
  <xsl:copy>
    <xsl:apply-templates select="*|@*|comment()|pi()|text()"/>
  </xsl:copy>
</xsl:template>]]></eg>

<p>The <code>xsl:copy-of</code> element copies a list of nodes
specified by an expression. The required <code>select</code> attribute
contains an expression.  The result of evaluating the expression must
be a node-set or a result tree fragment.  When it is node-set, all the
nodes in the set together with their content are copied in document
order over into the result tree; when it is a result tree fragment;
the complete fragment is copied over into the result tree.</p>

<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>

</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 expression that is
specified as the value of the <code>select</code> attribute.  String
expressions can also be used inside attribute values of literal result
elements by enclosing the string expression in curly brace
(<code>{}</code>).</p>

<div3>
<head>Generating Text with <code>xsl:value-of</code></head>

<p>The <code>xsl:value-of</code> element is replaced by the result of
evaluating the expression specified by the <code>select</code>
attribute.  The <code>select</code> attribute is required. The
expression is a <nt def="NT-StringExpr">StringExpr</nt>, which means
the result of evaluating the expression is converted to a string. The
element is called <code>xsl:value-of</code> because a node-set is
converted to a string by returning the <emph>value of</emph> the first
node.</p>

<issue id="issue-value-of-select-default"><p>Should the
<code>select</code> attribute have a default value of
<code>.</code>?</p></issue>

<p>For example, the following creates an HTML paragraph from a
<code>person</code> element with <code>first-name</code> and
<code>surname</code> attributes.  The paragraph will contain the value
of the <code>first-name</code> attribute of the current node followed
by a space and the value of the <code>surname</code> attribute of the
current node.</p>

<eg><![CDATA[<xsl:template match="person">
  <p>
   <xsl:value-of select="@first-name"/>
   <xsl:text> </xsl:text>
   <xsl:value-of select="@surname"/>
  </p>
</xsl:template>]]></eg>

<p>For example, the following creates an HTML paragraph from a
<code>person</code> element with <code>first-name</code> and
<code>surname</code> children elements.  The paragraph will contain
the value of the first <code>first-name</code> child element of the
current node followed by a space and the value of the first
<code>surname</code> child element of the current node.</p>

<eg><![CDATA[<xsl:template match="person">
  <p>
   <xsl:value-of select="first-name"/>
   <xsl:text> </xsl:text>
   <xsl:value-of select="surname"/>
  </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="from-ancestors-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, a <nt def="NT-StringExpr">StringExpr</nt> can
be used by surrounding the <nt def="NT-StringExpr">StringExpr</nt>
with curly braces (<code>{}</code>)</termdef>.  The attribute value
template is instantiated by replacing the string expression together
with surrounding curly braces by the result of evaluating the string
expression.  Curly braces are not recognized in an attribute value in
an XSLT stylesheet unless the attribute is specifically stated to be
one which is interpreted as an attribute value template.</p>

<note><p>Not all attributes are interpreted as attribute value
templates.  Attributes whose value is an expression or pattern,
attributes of top-level elements (children of a
<code>xsl:stylesheet</code> element) and attributes that refer to
named XSLT objects are not interpreted as attribute value
templates. Also <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> constant and the content
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 a string 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 a string expression without being
followed by a second right curly brace; an XSLT processor may signal
the error or recover by treating the right curly brace as if it had
been doubled.  A right curly brace inside a <nt
def="NT-Literal">Literal</nt> in a string expression is not recognized
as terminating the string expression.</p>

<p>Curly braces are <emph>not</emph> recognized recursively inside
string 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="#{idref(@ref)/title}">]]></eg>

</div3>

</div2>

<div2>
<head>Numbering</head>

<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>expr</code> attribute contains a
<nt def="NT-NumberExpr">NumberExpr</nt>. The value of the <nt
def="NT-NumberExpr">NumberExpr</nt> is rounded to an integer and then
converted to a string using the attributes specified in <specref
ref="convert"/>; when used with <code>xsl:number</code> 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 expr="position()" format="1. "/>
      <xsl:value-of select="."/>
    </p>
  </xsl:for-each>
</xsl:template>]]></eg>

<p>If no <code>expr</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>multi</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 elements should be counted at those levels.  The
<code>count</code> attribute defaults to the element type name of the
current node.</p></item>

<item><p>The <code>from</code> attribute is a pattern that specifies
where counting starts from.</p></item>

</ulist>

<p>In addition the <code>xsl:number</code> element has the attributes
specified in <specref ref="convert"/> for number to string
conversion.</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 nearest
ancestor (including the current node as its own ancestor) 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.</p></item>

<item><p>When <code>level="multi"</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 elements that
match the <code>count</code> pattern; it then maps each element of the
list to one plus the number of preceding siblings of that element 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.</p></item>

<item><p>When <code>level="any"</code>, it constructs a list of length
one containing one plus the number of elements at any level of the
document that start before this node and that match the
<code>count</code> pattern.  If the <code>from</code> attribute is
specified, then only elements after the first element before this
element that match the <code>from</code> pattern are
considered.</p>

<ednote><edtext>Would it be better to return the number of nodes that
match the pattern from the set consisting of the node itself and the
nodes starting before the node?  This would mean that when the node
does not match the pattern, the number of the previous matching node
would be returned rather than the number of the next matching
node.</edtext></ednote>

</item>

</ulist>

<p>The list of numbers is then converted into a string using the
attributes specified in <specref ref="convert"/>; when used with
<code>xsl:number</code> 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>

<ednote><edtext>Allowing them to be attribute value templates isn't
consistent with the current DTD: the declared values would all have to
be CDATA, and we couldn't use xml:lang because the XML spec doesn't
allow the value to be expressed as a template.</edtext></ednote>

<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="multi"
                 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="multi"
                 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 n-th format token will be used
to format the n-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>.</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 2.0 character property database),
and the Unicode value of preceding characters is one less than the
Unicode value of the last character.  This 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 vii ix x ...</code>.</p></item>

<item><p>A format token <code>I</code> generates the sequence <code>I
II III IV V VI VII VII 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 system 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>xml:lang</code> attribute specifies which language's alphabet is
to be used.</p>

<note><p>This can be considered as specifying the language of the
value of the <code>format</code> attribute and hence is consistent
with the semantics of <code>xml:lang</code>.</p></note>

<p>The <code>letter-value</code> attribute disambiguates between
numbering schemes that use letters.  In many languages there are two
commonly used numbering schemes that use letters.  One numbering
scheme assigns numeric values to letters in alphabetic sequence, and
the other assigns numeric values to each letter in some other manner.
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>other</code> specifies the other sequence.</p>

<p>The <code>digit-group-sep</code> attribute gives the separator
between groups of digits, and the optional
<code>n-digits-per-group</code> specifies the number of digits per
group.  For example, <code>digit-group-sep=","</code> and
<code>n-digits-per-group="3"</code> would produce numbers of the form
<code>1,000,000</code>.</p>

<p>The <code>sequence-src</code> attribute gives the URI of a text
resource that contains a whitespace separated list of the members of
the numbering sequence.</p>

<ednote><edtext>Specify what should happen when the sequence runs
out.</edtext></ednote>

<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="other"</code>
specifies <quote>traditional</quote> Hebrew numbering</p></item>

<item><p><code>format="&amp;#x10D0;" letter-value="other"</code>
specifies Georgian numbering</p></item>

<item><p><code>format="&amp;#x03B1;" letter-value="other"</code>
specifies <quote>classical</quote> Greek numbering</p></item>

<item><p><code>format="&amp;#x0430;" letter-value="other"</code>
specifies Old Slavic numbering</p></item>

</ulist>

</div3>
</div2>
</div1>

<div1>

<head>Repetition</head>

<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 <nt
def="NT-NodeSetExpr">NodeSetExpr</nt> specified by the
<code>select</code> attribute, which is required.  The template is
instantiated with the selected node as the current node, and with a
list of all of the selected nodes as the current node list.  The nodes
are processed in <termref def="dt-document-order">document
order</termref>, 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 which 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>

<p>The <code>xsl:if</code> element has a single attribute,
<code>test</code> which specifies a <nt
def="NT-BooleanExpr">BooleanExpr</nt>.  The content is a template.  If
the expression evaluates to true, then the content is instantiated;
otherwise nothing is created.  In the following example, the names in
a group of names are formatted as a comma separated list:</p>

<eg><![CDATA[<xsl:template match="namelist/name">
  <xsl:apply-templates/>
  <xsl:if test="not(position()=last())">, </xsl:if>
</xsl:template>]]></eg>

<p>The following colors every other table row yellow:</p>

<eg><![CDATA[<xsl:template match="item">
  <tr>
    <xsl:if test="position() mod 2 = 0">
       <xsl:attribute name="bgcolor">yellow</xsl:attribute>
    </xsl:if>
    <xsl:apply-templates/>
  </tr>
</xsl:template>]]></eg>

</div2>


<div2>
<head>Conditional Processing with <code>xsl:choose</code></head>

<p>The <code>xsl:choose</code> element selects one among a number of
possible alternatives. It consists of a series of
<code>xsl:when</code> elements followed by an optional
<code>xsl:otherwise</code> element.  Each <code>xsl:when</code>
element has a single attribute, <code>test</code>, which specifies a
<nt def="NT-BooleanExpr">BooleanExpr</nt>.  The content of the
<code>xsl:when</code> and <code>xsl:otherwise</code> elements is a
template.  When an <code>xsl:choose</code> element is processed, each
of the <code>xsl:when</code> elements is tested in turn.  The content
of the first, and only the first, <code>xsl:when</code> element whose
test is true is instantiated.  If no <code>xsl:when</code> is true,
the content of the <code>xsl:otherwise</code> element is
instantiated. If no <code>xsl:when</code> element is true, and no
<code>xsl:otherwise</code> element is present, nothing is created.</p>

<p>The following example enumerates items in an ordered list using
arabic numerals, letters, or roman numerals depending on the depth to
which the ordered lists are nested.</p>

<eg><![CDATA[<xsl:template match="orderedlist/listitem">
  <fo:list-item indent-start='2pi'>
    <fo:list-item-label>
      <xsl:variable name="level"
                    expr="size(from-ancestors(orderedlist)) mod 3"/>
      <xsl:choose>
        <xsl:when test='$level=1'>
          <xsl:number format="i"/>
        </xsl:when>
        <xsl:when test='$level=2'>
          <xsl:number format="a"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:number format="1"/>
        </xsl:otherwise>
      </xsl:choose>
      <xsl:text>. </xsl:text>
    </fo:list-item-label>
    <fo:list-item-body>
      <xsl:apply-templates/>
    </fo:list-item-body>
  </fo:list-item>
</xsl:template>]]></eg>

</div2>
</div1>

<div1 id="sorting">
<head>Sorting</head>

<p>Sorting is specified by adding <code>xsl:sort</code> elements as
children of <code>xsl:apply-templates</code> or
<code>xsl:for-each</code>.  The first <code>xsl:sort</code> child
specifies the primary sort key, the second <code>xsl:sort</code> child
specifies the secondary sort key and so on.  When
<code>xsl:apply-templates</code> or <code>xsl:for-each</code> has one
or more <code>xsl:sort</code> children, then instead of processing the
selected elements in document order, it sorts the elements according
to the specified sort keys and then processes them in sorted order.
When used in <code>xsl:for-each</code>, <code>xsl:sort</code> elements
must occur first.</p>

<ednote><edtext>Say that the current node list is in sorted
order.</edtext></ednote>

<p><code>xsl:sort</code> has a <code>select</code> attribute whose
value is a <nt def="NT-StringExpr">StringExpr</nt>. For each node to
be processed, the <nt def="NT-StringExpr">StringExpr</nt> is evaluated
with that node as the current node. The string that results from
evaluating the expression is used as the sort key for that node. The
default value of the <code>select</code> attribute is <code>.</code>,
which, as a <nt def="NT-StringExpr">StringExpr</nt>, returns the value
of the current node.</p>

<p>This string serves as a sort key for the node.  The following
optional attributes on <code>xsl:sort</code> control how the list of
sort keys are sorted:</p>

<ulist>

<item><p><code>order</code> specifies whether the strings should be
sorted in ascending or descending order; <code>ascending</code>
specifies ascending order; <code>descending</code> specifies
descending order; the default is <code>ascending</code></p></item>

<item><p><code>lang</code> specifies the language of the sort keys; 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</p></item>

<item><p><code>data-type</code> specifies the data type of the
strings; the following values are allowed</p>

<ulist>

<item><p><code>text</code> specifies that the sort keys should be
sorted lexicographically in the culturally correct manner for the
language specified by <code>lang</code></p></item>

<item><p><code>number</code> specifies that the sort keys should be
converted to numbers and then sorted according to the numeric
value; the value specified by <code>lang</code> can be used
to assist in the conversion to numbers</p></item>

</ulist>

<p>The default value is <code>text</code>.</p>

<ednote><edtext>We plan to leverage the work on XML schemas to define
further values in the future.</edtext></ednote>

</item>

<item><p><code>case-order</code> has the value
<code>upper-first</code> or <code>lower-first</code>; this applies
when <code>data-type="text"</code>, and specifies that upper-case
letters should sort before lower-case letters or vice-versa
respectively. For example, if <code>lang="en"</code> then <code>A a B
b</code> are sorted with <code>case-order="upper-first"</code> and
<code>a A b B</code> are sorted with
<code>case-order="lower-first"</code>. The default value is language
dependent.</p></item>

</ulist>

<ednote><edtext>We plan also to add an attribute whose value is a
label identifying the sorting scheme, to be specified by the I18N
WG.</edtext></ednote>

<p>The values of all of the above attributes are interpreted as
<termref def="dt-attribute-value-template">attribute value
templates</termref>.</p>

<note><p>It is recommended that implementors consult <bibref
ref="UNICODE-TR10"/> for information on internationalized
sorting.</p></note>

<p>The sort must be stable: in the sorted list of nodes, any sublist
that has sort keys that all compare equal must be in document
order.</p>

<p>For example, suppose an employee database has the form</p>

<eg><![CDATA[<employees>
  <employee>
    <name>
      <first>James</first>
      <last>Clark</last>
    </name>
    ...
  </employee>
</employees>
]]></eg>
  
<p>Then a list of employees sorted by name could be generated
using:</p>

<eg><![CDATA[<xsl:template match="employees">
  <ul>
    <xsl:apply-templates select="employee">
      <xsl:sort select="name/last"/>
      <xsl:sort select="name/first"/>
    </xsl:apply-templates>
  </ul>
</xsl:template>

<xsl:template match="employee">
  <li>
    <xsl:value-of select="name/first"/>
    <xsl:text> </xsl:text>
    <xsl:value-of select="name/last"/>
  </li>
</xsl:template>]]></eg>

</div1>

<div1 id="variables">
<head>Variables and Parameters</head>

<p>A variable is a name that may be bound to a value.  The value to
which a variable is bound (the <term>value</term> of the variable) can
be an object of any of the types that can be returned by expressions.
There are two elements that can be used to bind variables:
<code>xsl:variable</code> and <code>xsl:param-variable</code>. The
difference is that the value specified on the
<code>xsl:param-variable</code> variable is only a default value for
the binding; when the template or stylesheet within which the
<code>xsl:param-variable</code> element occurs is invoked, parameters
may be passed that are used in place of the default values.</p>

<p>For any use of these variable binding elements, there is a region
of the stylesheet tree within which the binding is visible; within
this region any binding of the variable that was visible on the
variable binding element itself is hidden.  Thus only the innermost
binding of a variable is visible.  The set of variable bindings in
scope for an expression consists of those bindings that are visible at
the point in the stylesheet where the expression occurs.</p>

<p>A variable binding element can specify the value of the variable in
two ways. It can have a <code>expr</code> attribute whose value is
an expression, which is evaluated to give the value of the
variable. If there is no <code>expr</code> attribute, then the
contents of the variable binding element specifies the value. The
contents is a template which is instantiated to give the value. In
this case the value is a result tree fragment.</p>

<p>Both <code>xsl:variable</code> and <code>xsl:param-variable</code>
are allowed at the top-level.  A top-level variable binding element
declares a global variable that is visible everywhere.  A top-level
<code>xsl:param-variable</code> element declares a parameter to the
stylesheet; XSLT does not define the mechanism by which parameters
are passed to the stylesheet.  It is an error if a stylesheet contains
more than one binding of a top-level variable the same name and same
<termref def="dt-important">importance</termref>.  An XSLT processor
may signal the error; if it does not signal the error, it must recover
by choosing from amongst the bindings with highest importance the one
that occurs last in the stylesheet.  At the top-level, the expression
or template specifying the variable value is evaluated with the same
context as that used to process the root node of the source document:
the current node is the root node of the source document and the
current node list is a list containing just the root node of the
source document.  If the template or expression specifying the value
of a global variable <emph>x</emph> references a global variable
<emph>y</emph>, then the value for <emph>y</emph> must be computed
before the value of <emph>x</emph>.  It is an error if it is
impossible to do this for all global variable definitions, in other
words it is an error if the definitions are circular.</p>

<p>This example declares a global variable <code>para-font-size</code>
which it references in an attribute value template.</p>

<eg><![CDATA[<xsl:variable name="para-font-size">12pt</xsl:variable>

<xsl:template match="para">
 <fo:block font-size="{$para-font-size}">
   <xsl:apply-templates/>
 </fo:block>
</xsl:template>
]]></eg>

<p>As well as being allowed at the top-level, both
<code>xsl:variable</code> and <code>xsl:param-variable</code> are also
allowed in templates.  <code>xsl:variable</code> is allowed anywhere
within a template that an instruction is allowed.  In this case the
binding is visible for all following siblings and their descendants.
Note that the binding is not visible for the <code>xsl:variable</code>
element itself.  <code>xsl:param-variable</code> is allowed as a child
at the beginning of an <code>xsl:template</code> element.  In this
context, the binding is visible for all following siblings and their
descendants.  Note that the binding is not visible for the
<code>xsl:param-variable</code> element itself.</p>

<p>Parameters are passed to templates using the <code>xsl:param</code>
element.  The required <code>name</code> attribute specifies the name
of the parameter (the variable the value of whose binding is to be
replaced).  <code>xsl:param</code> is allowed within both
<code>xsl:call-template</code> and <code>xsl:apply-templates</code>.
The value of the parameter is specified in the same way as for
<code>xsl:variable</code> and <code>xsl:param-variable</code>.  The
current node and current node list used for computing the value
specified by <code>xsl:param</code> element is the same as that used
for the <code>xsl:apply-templates</code> or
<code>xsl:call-template</code> element within which it occurs.  It is
not an error to pass a parameter <emph>x</emph> to a template that
does not have a <code>xsl:param-variable</code> element for
<emph>x</emph>; the parameter is simply ignored.</p>

<p>This example defines a named template for a
<code>numbered-block</code> with an argument to control the format of
the number.</p>

<eg><![CDATA[<xsl:template name="numbered-block">
  <xsl:param-variable name="format">1. </xsl:param-variable>
  <xsl:number format="{$format}"/>
  <fo:block><xsl:apply-templates/></fo:block>
</xsl:template>

<xsl:template match="appendix/title">
  <xsl:call-template name="numbered-block">
    <xsl:param name="format">A. </xsl:param>
  </xsl:call-template>
</xsl:template>]]></eg>

</div1>

<div1>
<head>Messages</head>

<p>The <code>xsl:message</code> instruction sends a message in a way
that is dependent on the XSLT processor.  The content of the
<code>xsl:message</code> instruction is a template.  The
<code>xsl:message</code> is instantiated by instantiating the content
to create an XML fragment.  This XML fragment is the content of the
message.</p>

<note><p>An XSLT processor might implement <code>xsl:message</code> by
popping up an alert box or by writing to a log file.</p></note>

</div1>

<div1>
<head>Combining Stylesheets</head>

<p>XSLT provides two mechanisms to combine stylesheets:</p>

<slist>

<sitem>an import mechanism that allows stylesheets to override each
other, and</sitem>

<sitem>an inclusion mechanism that allows stylesheets to be textually
combined.</sitem>

</slist>

<div2 id="import">
<head>Stylesheet Import</head>

<p>An XSLT stylesheet may contain <code>xsl:import</code> elements. All
the <code>xsl:import</code> elements must occur at the beginning of
the stylesheet.  The <code>xsl:import</code> element has an
<code>href</code> attribute whose value is the URI of a 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>

<ednote><edtext>Say what importing a stylesheet means.</edtext></ednote>

<eg><![CDATA[<xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform/1.0">
  <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-important" term="Important">Definitions and
template rules in the importing stylesheet are defined to be more
<term>important</term> than definitions and template rules in any
imported stylesheets.  Also definitions and template rules in one
imported stylesheet are defined to be more <term>important</term> than
definitions and template rules in previous imported
stylesheets.</termdef></p>

<p>In general a more important definition or template rule takes
precedence over a less important definition or template rule.  This is
defined in detail for each kind of definition and for template
rules.</p>

<ednote><edtext>Say something about the case where the same stylesheet
gets imported twice. This should be treated the same as importing a
stylesheet with the same content but different URIs.  What about
import loops?</edtext></ednote>

<p><code>xsl:apply-imports</code> processes the current node using
only template rules that were imported into the stylesheet containing
the current rule; the node is processed in the current rule's
mode.</p>

<ednote><edtext>Expand this.</edtext></ednote>

</div2>

<div2>
<head>Stylesheet Inclusion</head>

<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 the URI of a
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"/>). The <code>xsl:include</code> element can occur as
the child of the <code>xsl:stylesheet</code> element at any point
after all <code>xsl:import</code> elements.</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.  Also 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. Unlike
with <code>xsl:import</code>, the fact that rules or definitions are
included does not affect the way they are processed.</p>

<ednote><edtext>What happens when a stylesheet directly or indirectly
includes itself?</edtext></ednote>

</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>In the second case, the possibility arises of documents with inline
style, that is documents that specify their own style. XSLT does not
define a specific mechanism for this. This is because this can be done
by means of a general purpose mechanism for associating stylesheets
with documents provided that:</p>

<slist>

<sitem>the mechanism allows a part of a resource to be specified as
the stylesheet, for example by using a URI with a fragment identifier,
and</sitem>

<sitem>the mechanism can itself can be embedded in the document, for
example as a processing instruction.</sitem>

</slist>
<p>It is not in the scope of XSLT to define such a mechanism.</p>
<note>
<p>This is because the mechanism should be independent of any one stylesheet
mechanism.</p>
</note>

<p>The <code>xsl:stylesheet</code> element may have an ID attribute
that specifies a unique identifier.</p>

<note><p>In order for such an attribute to be used with the
<code>id</code> XPointer location term, it must actually be declared
in the DTD as being an ID.</p></note>

<p>The following example shows how inline style can be accomplished
using the <code>xml-stylesheet</code> processing instruction mechanism
for associating a stylesheet with an XML document.  The URI uses an
XPointer in a fragment identifier to locate the
<code>xsl:stylesheet</code> element.</p>

<eg><![CDATA[<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="#id(style1)"?>
<!DOCTYPE doc SYSTEM "doc.dtd">
<doc>
<head>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform/1.0" id="style1">
<xsl:import href="doc.xsl"/>
<xsl:template match="id(foo)">
 <fo:block font-weight="bold"><xsl:apply-templates/></fo:block>
</xsl:template>
</xsl:stylesheet>
</head>
<body>
<para id="foo">
...
</para>
</body>
</doc>
]]></eg>

<note><p>The <code>type</code> pseudo-attribute in the
<code>xml-stylesheet</code> processing instruction identifies the
stylesheet language, not the content type of the resource of which the
stylesheet is a part.</p></note>

</div2>
</div1>

</body>

<back>
<div1 id="dtd">
<head>DTD Fragment for XSLT Stylesheets</head>

<p>The following entity can be used to construct a DTD for XSLT
stylesheets that create instances of a particular result DTD.  Before
referencing the entity, the stylesheet DTD must define a
<code>result-elements</code> parameter entity listing the allowed
result element types.  For example:</p>

<eg><![CDATA[<!ENTITY % result-elements "
  | fo:inline-sequence
  | fo:block
">]]></eg>

<p>The use of the <code>xsl:</code> prefix in this DTD does not imply
that XSLT stylesheets are required to use this prefix.  Any of the
elements declared in this DTD may have attributes whose name starts
with <code>xmlns:</code> or is equal to <code>xmlns</code> in addition
to the attributes declared in this DTD.</p>

<eg><![CDATA[<!ENTITY % char-instructions "
  | xsl:apply-templates
  | xsl:call-template
  | xsl:apply-imports
  | xsl:for-each
  | xsl:value-of
  | xsl:copy-of
  | xsl:number
  | xsl:choose
  | xsl:if
  | xsl:text
  | xsl:copy
  | xsl:variable
  | xsl:message
">

<!ENTITY % instructions "
  %char-instructions;
  | xsl:pi
  | xsl:comment
  | xsl:element
  | xsl:attribute
  | xsl:use
">

<!ENTITY % char-template "
 (#PCDATA
  %char-instructions;)*
">

<!ENTITY % template "
 (#PCDATA
  %instructions;
  %result-elements;)*
">

<!-- Used for attribute values that are URIs.-->
<!ENTITY % URI "CDATA">

<!-- Used for attribute values that are patterns.-->
<!ENTITY % pattern "CDATA">

<!-- Used for attribute values that are expressions.-->
<!ENTITY % expr "CDATA">

<!-- Used for an attribute value that consists of a single character.-->
<!ENTITY % char "CDATA">

<!-- Used for attribute values that are a priority. -->
<!ENTITY % priority "NMTOKEN">

<!ENTITY % space-att "xml:space (default|preserve) #IMPLIED">

<!ENTITY % top-level "
 (xsl:import*,
  (xsl:include
  | xsl:strip-space
  | xsl:preserve-space
  | xsl:key
  | xsl:functions
  | xsl:locale
  | xsl:attribute-set
  | xsl:variable
  | xsl:param-variable
  | xsl:template)*)
">

<!ELEMENT xsl:stylesheet %top-level;>
<!ELEMENT xsl:transform %top-level;>

<!ATTLIST xsl:stylesheet
  result-ns NMTOKEN #IMPLIED
  default-space (preserve|strip) "preserve"
  indent-result (yes|no) "no"
  id ID #IMPLIED
  xmlns:xsl CDATA #FIXED "http://www.w3.org/XSL/Transform/1.0"
  %space-att;
>

<!ELEMENT xsl:import EMPTY>
<!ATTLIST xsl:import href %URI; #REQUIRED>

<!ELEMENT xsl:include EMPTY>
<!ATTLIST xsl:include href %URI; #REQUIRED>

<!ELEMENT xsl:strip-space EMPTY>
<!ATTLIST xsl:strip-space elements NMTOKENS #REQUIRED>

<!ELEMENT xsl:preserve-space EMPTY>
<!ATTLIST xsl:preserve-space elements NMTOKENS #REQUIRED>

<!ELEMENT xsl:key EMPTY>
<!ATTLIST xsl:key
  name NMTOKEN #REQUIRED
  match %pattern; #REQUIRED
  use %expr; #REQUIRED
>

<!ELEMENT xsl:functions (#PCDATA)>
<!ATTLIST xsl:functions
  ns NMTOKEN #REQUIRED
  code CDATA #IMPLIED
  archive CDATA #IMPLIED
>

<!ELEMENT xsl:locale EMPTY>
<!ATTLIST xsl:locale
  name NMTOKEN #IMPLIED
  decimal-separator %char; "."
  grouping-separator %char; ","
  infinity CDATA "&#x221E;"
  minus-sign %char; "-"
  NaN CDATA "&#xFFFD;"
  percent %char; "%"
  per-mill %char; "&#x2030;"
  zero-digit %char; "0"
  digit %char; "#"
  pattern-separator %char; ";"
>

<!ELEMENT xsl:template
 (#PCDATA
  %instructions;
  %result-elements;
  | xsl:param-variable)*
>

<!ATTLIST xsl:template
  match %pattern; #IMPLIED
  name NMTOKEN #IMPLIED
  priority %priority; #IMPLIED
  mode NMTOKEN #IMPLIED
  %space-att;
>

<!ELEMENT xsl:value-of EMPTY>
<!ATTLIST xsl:value-of
  select %expr; #REQUIRED
>

<!ELEMENT xsl:copy-of EMPTY>
<!ATTLIST xsl:copy-of select %expr; #REQUIRED>

<!ELEMENT xsl:number EMPTY>
<!ATTLIST xsl:number
   level (single|multi|any) "single"
   count CDATA #IMPLIED
   from CDATA #IMPLIED
   expr %expr; #IMPLIED
   format CDATA '1'
   xml:lang NMTOKEN #IMPLIED
   letter-value (alphabetic|other) #IMPLIED
   digit-group-sep CDATA #IMPLIED
   n-digits-per-group NMTOKEN #IMPLIED
   sequence-src %URI; #IMPLIED
>

<!ELEMENT xsl:apply-templates (xsl:sort|xsl:param)*>
<!ATTLIST xsl:apply-templates
  select %expr; "node()"
  mode NMTOKEN #IMPLIED
>

<!ELEMENT xsl:apply-imports EMPTY>

<!-- xsl:sort cannot occur after any other elements or
any non-whitespace character -->

<!ELEMENT xsl:for-each
 (#PCDATA
  %instructions;
  %result-elements;
  | xsl:sort)*
>

<!ATTLIST xsl:for-each
  select %expr; #REQUIRED
  %space-att;
>

<!ELEMENT xsl:sort EMPTY>
<!ATTLIST xsl:sort
  select %expr; "."
  lang CDATA #IMPLIED
  data-type (text|number) "text"
  order (ascending|descending) "ascending"
  case-order (upper-first|lower-first) #IMPLIED
>

<!ELEMENT xsl:if %template;>
<!ATTLIST xsl:if
  test %expr; #REQUIRED
  %space-att;
>

<!ELEMENT xsl:choose (xsl:when+, xsl:otherwise?)>
<!ATTLIST xsl:choose %space-att;>

<!ELEMENT xsl:when %template;>
<!ATTLIST xsl:when
  test %expr; #REQUIRED
  %space-att;
>

<!ELEMENT xsl:otherwise %template;>
<!ATTLIST xsl:otherwise %space-att;>

<!ELEMENT xsl:attribute-set (xsl:attribute|xsl:use)*>
<!ATTLIST xsl:attribute-set
  name NMTOKEN #REQUIRED
>

<!ELEMENT xsl:call-template (xsl:param)*>
<!ATTLIST xsl:call-template
  name NMTOKEN #REQUIRED
>

<!ELEMENT xsl:param %template;>
<!ATTLIST xsl:param
  name NMTOKEN #REQUIRED
  expr %expr; #IMPLIED
>

<!ELEMENT xsl:variable %template;>
<!ATTLIST xsl:variable 
  name NMTOKEN #REQUIRED
  expr %expr; #IMPLIED
>

<!ELEMENT xsl:param-variable %template;>
<!ATTLIST xsl:param-variable 
  name NMTOKEN #REQUIRED
  expr %expr; #IMPLIED
>

<!ELEMENT xsl:text (#PCDATA)>

<!ELEMENT xsl:pi %char-template;>
<!ATTLIST xsl:pi 
  name CDATA #REQUIRED
  %space-att;
>

<!ELEMENT xsl:element %template;>
<!ATTLIST xsl:element 
  name CDATA #REQUIRED
  namespace CDATA #IMPLIED
  %space-att;
>

<!ELEMENT xsl:attribute %char-template;>
<!ATTLIST xsl:attribute 
  name CDATA #REQUIRED
  namespace CDATA #IMPLIED
  %space-att;
>

<!ELEMENT xsl:use EMPTY>
<!ATTLIST xsl:use attribute-set NMTOKEN #REQUIRED>

<!ELEMENT xsl:comment %char-template;>
<!ATTLIST xsl:comment %space-att;>

<!ELEMENT xsl:copy %template;>
<!ATTLIST xsl:copy %space-att;>

<!ELEMENT xsl:message %template;>
<!ATTLIST xsl:message %space-att;>]]></eg>
</div1>

<div1>
<head>References</head>
<div2>
<head>Normative References</head>
<blist>

<bibl id="XML" key="XML">World Wide Web Consortium. <emph>Extensible
Markup Language (XML) 1.0.</emph> W3C Recommendation. See <loc
href="http://www.w3.org/TR/1998/REC-xml-19980210">http://www.w3.org/TR/1998/REC-xml-19980210</loc></bibl>

<bibl id="XMLNAMES" key="XML Names">World Wide Web
Consortium. <emph>Namespaces in XML.</emph> W3C Recommendation. See
<loc
href="http://www.w3.org/TR/REC-xml-names">http://www.w3.org/TR/REC-xml-names</loc></bibl>

</blist>
</div2>
<div2>
<head>Other References</head>

<blist>

<bibl id="CSS2" key="CSS2">World Wide Web Consortium.  <emph>Cascading
Style Sheets, level 2 (CSS2)</emph>.  W3C Recommendation.  See <loc
href="http://www.w3.org/TR/1998/REC-CSS2-19980512"
>http://www.w3.org/TR/1998/REC-CSS2-19980512</loc></bibl>

<bibl id="DSSSL" key="DSSSL">International Organization
for Standardization, International Electrotechnical Commission.
<emph>ISO/IEC 10179:1996.  Document Style Semantics and Specification
Language (DSSSL)</emph>.  International Standard.</bibl>

<bibl id="UNICODE-TR10" key="UNICODE TR10">Unicode Consortium.  <emph>Draft
Unicode Technical Report #10. Unicode Collation Algorithm</emph>.
Draft Unicode Technical Report.  See <loc
href="http://www.unicode.org/unicode/reports/tr10/index.html"
>http://www.unicode.org/unicode/reports/tr10/index.html</loc>.</bibl>

<bibl id="XHTML" key="XHTML">World Wide Web
Consortium. <emph>XHTML 1.0: The Extensible HyperText Markup
Language.</emph> W3C Working Draft. See <loc
href="http://www.w3.org/TR/WD-html-in-xml"
>http://www.w3.org/TR/WD-html-in-xml</loc></bibl>

<bibl id="XPTR" key="XPointer">World Wide Web
Consortium. <emph>XML Pointer Language (XPointer).</emph> W3C Working
Draft. See <loc href="http://www.w3.org/TR/WD-xptr"
>http://www.w3.org/TR/WD-xptr</loc></bibl>

<bibl id="XMLSTYLE" key="XML Stylesheet">World Wide Web
Consortium. <emph>Associating stylesheets with XML documents.</emph>
W3C Working Draft. See <loc
href="http://www.w3.org/TR/WD-xml-stylesheet">http://www.w3.org/TR/WD-xml-stylesheet</loc></bibl>

<bibl id="XSL" key="XSL">World Wide Web Consortium.  <emph>Extensible
Stylesheet Language (XSL).</emph>  W3C Working Draft.  See <loc
href="http://www.w3.org/TR/&year;/WD-xsl"
     >http://www.w3.org/TR/&year;/WD-xsl</loc></bibl>

</blist>

</div2>
</div1>
<inform-div1>
<head>Examples</head>

<div2>
<head>Formatting Objects Example</head>

<p>The following is a simple but complete stylesheet.</p>

<eg><![CDATA[<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform/1.0"
                xmlns:fo="http://www.w3.org/XSL/Format/1.0"
                result-ns="fo"
                indent-result="yes">
<xsl:template match='/'>
 <fo:basic-page-sequence font-family="serif">
  <fo:simple-page-master page-master-name='scrolling'/>
  <fo:queue queue-name='body'>
   <xsl:apply-templates/>
  </fo:queue>
 </fo:basic-page-sequence>
</xsl:template>

<xsl:template match="title">
 <fo:block font-weight="bold">
  <xsl:apply-templates/>
 </fo:block>
</xsl:template>

<xsl:template match="p">
 <fo:block>
  <xsl:apply-templates/>
 </fo:block>
</xsl:template>

<xsl:template match="emph">
 <fo:inline-sequence font-style="italic">
  <xsl:apply-templates/>
 </fo:inline-sequence>
</xsl:template>

</xsl:stylesheet>]]></eg>

<p>With the following source document</p>

<eg><![CDATA[<doc>
<title>An example</title>
<p>This is a test.</p>
<p>This is <emph>another</emph> test.</p>
</doc>]]></eg>

<p>it would produce the following result</p>

<eg><![CDATA[<fo:basic-page-sequence xmlns:fo="http://www.w3.org/XSL/Format/1.0"
  font-family="serif">
<fo:simple-page-master page-master-name="scrolling"/>
<fo:queue queue-name="body">
<fo:block font-weight="bold">An example</fo:block>
<fo:block>This is a test.</fo:block>
<fo:block>This is <fo:inline-sequence
  font-style="italic">another</fo:inline-sequence> test.</fo:block>
</fo:queue>
</fo:basic-page-sequence>]]></eg>
</div2>

<div2>
<head>XHTML Example</head>

<p>This is an example of using XSLT to create an XHTML document (see
<bibref ref="XHTML"/>).  The following stylesheet:</p>

<eg><![CDATA[<?xml version="1.0"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/XSL/Transform/1.0"
  xmlns="http://www.w3.org/Profiles/xhtml1-transitional"
  default-space="strip"
  indent-result="yes">

        <xsl:template match="/">
<html>
        <head>
                <title>Sales Results By Division</title>
        </head>

        <body>
                <table border="1">
                <tr>
                        <th>Division</th>
                        <th>Revenue</th>
                        <th>Growth</th>
                        <th>Bonus</th>
                </tr>
        <xsl:apply-templates/>
                </table>
        </body>
</html>
        </xsl:template>

        <xsl:template match="sales">
                <xsl:apply-templates match="division">
                        <!-- order the result by revenue -->
                        <xsl:sort select="revenue"
                                  data-type="number"
                                  order="descending"/>
                </xsl:apply-templates>
        </xsl:template>

        <xsl:template match="division">
                        <tr>
                        <td><em><xsl:value-of select="@id"/></em></td>
                <xsl:apply-templates select="revenue"/>
                <xsl:apply-templates select="growth"/>
                <xsl:apply-templates select="bonus"/>
                        </tr>

        </xsl:template>

        <xsl:template match="revenue | growth | bonus">
                        <td><xsl:apply-templates/></td>
        </xsl:template>
</xsl:stylesheet>]]></eg>

<p>with the following input document</p>

<eg><![CDATA[<?xml version="1.0"?>

<sales>

        <division id="North">
                <revenue>10</revenue>
                <growth>9</growth>
                <bonus>7</bonus>
        </division>

        <division id="South">
                <revenue>4</revenue>
                <growth>3</growth>
                <bonus>4</bonus>
        </division>

        <division id="West">
                <revenue>6</revenue>
                <growth>-1.5</growth>
                <bonus>2</bonus>
        </division>

</sales>]]></eg>

<p>would produce the following result</p>

<eg><![CDATA[<?xml version="1.0"?>
<html xmlns="http://www.w3.org/Profiles/xhtml1-transitional">
  <head>
    <title>Sales Results By Division</title>
  </head>
  <body>
    <table border="1">
      <tr>
	<th>Division</th>
	<th>Revenue</th>
	<th>Growth</th>
	<th>Bonus</th>
      </tr>
      <tr>
	<td><em>North</em></td>
	<td>10</td>
	<td>9</td>
	<td>7</td>
      </tr>
      <tr>
	<td><em>West</em></td>
	<td>6</td>
	<td>-1.5</td>
	<td>2</td>
      </tr>
      <tr>
	<td><em>South</em></td>
	<td>4</td>
	<td>3</td>
	<td>4</td>
      </tr>
    </table>
  </body>
</html>]]></eg>

</div2>

</inform-div1>

<inform-div1>
<head>Acknowledgements</head>
<p>The following have contributed to authoring this draft:</p>
<slist>
<sitem>Daniel Lipkin, Oracle</sitem>
<sitem>Jonathan Marsh, Microsoft</sitem>
<sitem>Henry Thompson, University of Edinburgh</sitem>
<sitem>Norman Walsh, ArborText</sitem>
<sitem>Steve Zilles, Adobe</sitem>
</slist>


<p>This specification was developed and approved for publication by the
W3C XSL Working Group (WG). WG approval of this specification does not
necessarily imply that all WG members voted for its approval. The
current members of the XSL WG are:</p>

<orglist>
<member>
<name>Sharon Adler</name>
<affiliation>Inso</affiliation>
<role>Co-Chair</role>
</member>
<member>
<name>Anders Berglund</name>
<affiliation>Inso</affiliation>
</member>
<member>
<name>Scott Boag</name>
<affiliation>Lotus</affiliation> 
</member>
<member>
<name>Jeff Caruso</name>
<affiliation>Bitstream</affiliation>
</member>
<member>
<name>James Clark</name>
<role>XSLT Editor</role>
</member>
<member>
<name>Peter Danielsen</name>
<affiliation>Bell Labs</affiliation>
</member>
<member>
<name>Don Day</name>
<affiliation>IBM</affiliation>
</member>
<member>
<name>Stephen Deach</name>
<affiliation>Adobe</affiliation>
</member>
<member>
<name>Angel Diaz</name>
<affiliation>IBM</affiliation>
</member>
<member>
<name>Dwayne Dicks</name>
<affiliation>SoftQuad</affiliation>
</member>
<member>
<name>Andrew Greene</name>
<affiliation>Bitstream</affiliation>
</member>
<member>
<name>Paul Grosso</name>
<affiliation>ArborText</affiliation>
</member>
<member>
<name>Eduardo Gutentag</name>
<affiliation>Sun</affiliation>
</member>
<member>
<name>Mickey Kimchi</name>
<affiliation>Enigma</affiliation>
</member>
<member>
<name>Chris Lilley</name>
<affiliation>W3C</affiliation>
</member>
<member>
<name>Daniel Lipkin</name>
<affiliation>Oracle</affiliation>
</member>
<member>
<name>Chris Maden</name>
<affiliation>O'Reilly</affiliation>
</member>
<member>
<name>Jonathan Marsh</name>
<affiliation>Microsoft</affiliation>
</member>
<member>
<name>Alex Milowski</name> 
<affiliation>CommerceOne</affiliation>
</member>
<member>
<name>Boris Moore</name>
<affiliation>RivCom</affiliation>
</member>
<member>
<name>Steve Muench</name>
<affiliation>Oracle</affiliation>
</member>
<member>
<name>Carolyn Pampino</name>
<affiliation>Interleaf</affiliation>
</member>
<member>
<name>Scott Parnell</name>
<affiliation>Xerox</affiliation>
</member>
<member>
<name>Vincent Quint</name>
<affiliation>W3C</affiliation>
</member>
<member>
<name>Gregg Reynolds</name>
<affiliation>Datalogics</affiliation>
</member>
<member>
<name>Jonathan Robie</name>
<affiliation>Software AG</affiliation>
</member>
<member>
<name>Henry Thompson</name>
<affiliation>University of Edinburgh</affiliation>
</member>
<member>
<name>Philip Wadler</name>
<affiliation>Bell Labs</affiliation>
</member>
<member>
<name>Randy Waki</name>
<affiliation>Novell</affiliation>
</member>
<member>
<name>Norm Walsh</name>
<affiliation>ArborText</affiliation>
</member>
<member>
<name>Sanjiva Weerawarana</name>
<affiliation>IBM</affiliation>
</member>
<member>
<name>Umit Yalcinalp</name>
<affiliation>Sun</affiliation>
</member>
<member>
<name>Steve Zilles</name>
<affiliation>Adobe</affiliation>
<role>Co-Chair</role>
</member>
</orglist>

</inform-div1>

<inform-div1>
<head>Changes from Previous Public Working Draft</head>

<p>The following is a summary of changes since the previous public
working draft.</p>

<p>Select patterns, string expressions and boolean expressions have
been combined and generalized into an expression language with
multiple data types (see <specref ref="expressions"/>).</p>

<p><code>xsl:strip-space</code> and <code>xsl:preserve-space</code>
have an <code>elements</code> attribute which specifies a list of
element types, rather than a <code>element</code> attribute specifying
a single element type.</p>

<p>The <code>id()</code> function has been split into
<code>id()</code> and <code>idref()</code>.</p>

<p><code>xsl:id</code> has been replaced by the <code>xsl:key</code>
element (see <specref ref="key"/>), and associated <code>key()</code>
and <code>keyref()</code> functions.</p>

<p>The <code>doc()</code> and <code>docref()</code> have been added to
support multiple source documents.</p>

<p>Namespace wildcards (<code>ns:*</code>) have been added.</p>

<p><code>ancestor()</code> and <code>ancestor-or-self()</code> have
been replaced by a more general facility for addressing different
axes.</p>

<p>Positional qualifiers (<code>first-of-type()</code>,
<code>first-of-any()</code>, <code>last-of-type()</code>,
<code>last-of-any()</code>) have been replaced by the
<code>position()</code> and <code>last()</code> functions and numeric
expressions inside <code>[]</code>.</p>

<p>Counters have been removed. An <code>expr</code> attribute has been
added to <code>xsl:number</code> which in conjunction with the
<code>position()</code> allows numbering of sorted node lists.</p>

<p>Multiple adjacent uses of <code>[]</code> are allowed.</p>

<p>Macros and templates have been unified by allowing templates to be
named and have parameters.</p>

<p><code>xsl:constant</code> have been replaced by
<code>xsl:variable</code> which allows variables to be typed and
local.</p>

<p>The default for <code>priority</code> on <code>xsl:template</code>
has changed (see <specref ref="conflict"/>).</p>

<p>An extension mechanism has been added (see <specref
ref="functions"/>).</p>

<p>The namespace URIs have been changed.</p>

<p><code>xsl:copy-of</code> has been added (see <specref
ref="copying"/>).</p>

<p>A error recovery mechanism to allow forwards-compatibility has
been added (see <specref ref="forwards"/>).</p>

<p>A <code>namespace</code> attribute has been added to
<code>xsl:element</code> and <code>xsl:attribute</code>.</p>

</inform-div1>

</back>
</spec>
