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