<?xml version='1.0' encoding='iso-8859-2'?><!-- -*- coding: iso-latin-2 -*- -->
<!-- $Id: spec-stx.xml,v 1.64 2007/04/27 07:13:11 pcimprich Exp $ -->
<!DOCTYPE spec [ <!-- for validation add: SYSTEM "xmlspec-v21.dtd" -->
<!ENTITY URLFO "http://www.w3.org/TR/xpath-functions/">
<!ENTITY URLDM "http://www.w3.org/TR/xpath-datamodel/">
<!ENTITY URLXML "http://www.w3.org/TR/REC-xml/">
<!ENTITY URLXP2 "http://www.w3.org/TR/xpath20/">
<!ENTITY URLXMLNAMES "http://www.w3.org/TR/REC-xml-names/">
<!ENTITY STXNS "http://stx.sourceforge.net/2002/ns">
<!ENTITY STXFNS "http://stx.sourceforge.net/2003/functions">
<!ENTITY STXDMNS "http://stx.sourceforge.net/2003/data-model">
<!ENTITY XQXPFO "XQuery 1.0 and XPath 2.0 Functions and Operators">
<!ENTITY XQXPDM "XQuery 1.0 and XPath 2.0 Data Model">
]>
<spec w3c-doctype="note-candidate" role="wd">

  <header>

    <title>Streaming Transformations for XML (STX)</title>
    <version>Version 1.0</version>
    <w3c-designation></w3c-designation>
    <w3c-doctype>Working Draft</w3c-doctype>

    <pubdate>
      <day>27</day><month>April</month>
      <year>2007</year>
    </pubdate>

    <publoc>
      <loc href="http://stx.sourceforge.net/documents/spec-stx-20040701.html">http://stx.sourceforge.net/documents/spec-stx-20070427.html</loc>
    </publoc>

    <latestloc>
      <loc href="http://stx.sourceforge.net/documents/">http://stx.sourceforge.net/documents/</loc>
    </latestloc>

    <authlist>
      <author>
	<name>Petr Cimprich</name>
	<email href="mailto:n/a">petr at NO-SPAM.gingerall.cz</email>
      </author>
      <author>
	<name>Oliver Becker</name>
	<email href="mailto:n/a">obecker at NO-SPAM.informatik.hu-berlin.de</email>
      </author>
      <author>
	<name>Christian Nentwich</name>
	<email href="mailto:n/a">c.nentwich at NO-SPAM.cs.ucl.ac.uk</email>
      </author>
      <author>
	<name>Honza Jiroušek</name>
	<email href="mailto:n/a">honza.jirousek at NO-SPAM.ecn.cz</email>
      </author>
      <author>
	<name>Manos Batsis</name>
	<email href="mailto:n/a">mbatsis at NO-SPAM.humanmarkup.org</email>
      </author>
      <author>
	<name>Paul Brown</name>
	<email href="mailto:n/a">prb at NO-SPAM.fivesight.com</email>
      </author>
      <author>
	<name>Michael Kay</name>
	<email href="mailto:n/a">michael.h.kay at NO-SPAM.ntlworld.com</email>
      </author>
    </authlist>

    <copyright>
      <p>Copyright &#xa9; 2002-2005 authors and contributors. All rights 
      reserved.</p>
    </copyright>

    <status>
      <p>This document is a working draft of the STX transformation language 
      specification.</p>
    </status>

    <abstract>
      <p>STX is an XML-based language for transforming XML documents into 
      other XML documents without building a tree in memory. An STX processor 
      transforms one or more source streams of XML events according to rules 
      given in an XML document called STX transformation sheet and generates one 
      or more result XML streams. Each incoming event invokes one or more rules, 
      that can e.g. emit events to a result stream or access a working 
      storage.</p>
    </abstract>

    <langusage>
      <language id="EN">English</language>
      <language id="ebnf">EBNF</language>
    </langusage>

    <revisiondesc><p>See CVS log</p></revisiondesc>

  </header>

  <body>

    <div1 id="introduction">

      <head>Introduction</head>

      <p>This document defines the syntax and semantics of the STX transformation 
	language. Transformation rules in STX are expressed as well-formed XML 
	documents. These documents, called STX transformation sheets (abbreviated 
	as stx-sheets), may include both elements that are defined by STX (STX 
	declarations and instructions) and other elements (literals). STX-defined 
	elements are identified by a specific XML namespace, which is referred to 
	in this specification as the STX namespace. This document uses the 'stx' 
	prefix as a shortcut for referring to elements from the STX 
	namespace.</p>

      <p>An STX transformation describes rules for transforming one or more 
	source XML documents into one or more result XML documents. The STX 
	language is designed in a way which allows transformations to have a 
	streaming character. This means that it does not need to build a tree 
	representing the source documents in memory. Result events are generated 
	as soon as source events appear and are processed. Because of the 
	streaming character of STX transformations, source XML documents are 
	supposed to be provided as streams of XML events. This is why the source 
	documents are also referred to as source streams of XML events. The same 
	is true for result XML documents that may be referred to as result 
	streams of XML events.</p>

      <note>
	<p>The term 'XML events' used within this specification refers to any 
	sequence of events describing a structure of XML document. The STX 
	language does not require any particular set of such events (like SAX2, 
	StAX or Expat events). Implementations are free to support any APIs to 
	communicate with source stream providers and result stream handlers.
	</p>
      </note>

      <p>The transformation is achieved by associating events with templates. 
	A template <nt def="NT-pattern">pattern</nt> is matched against events 
	and their context. The best matching template is then instantiated to 
	create a part of the result stream. A template is always instantiated 
	with respect to the current context, a set of additional information 
	maintained during the transformation. In constructing the result stream, 
	events from the source stream can be filtered and arbitrary events can be 
	added. Events can also be reordered using a working storage.</p>

      <p>On the surface, the syntax of STX is similar to the syntax of 
        <bibref ref="XSLT2"/>. STX also employs a compact expression language 
	embedded in certain attributes. This expression language, called STXPath, 
	is syntactically similar to <bibref ref="XPath2"/>. This should allow XSLT 
	users to easily adapt to the STX syntax.</p>

    </div1>

    <div1 id="concepts">

      <head>Concepts</head>

      <p>The software responsible for running an STX transformation is referred 
	to as an <kw>STX processor</kw>. An STX processor transforms one or more 
	source XML documents according to rules given in an STX transformation 
	sheet and generates one or more result XML documents.</p>

      <p>The source documents are supplied in the form of streams of XML events. 
	These streams are referred to as the <kw>source streams</kw>. The stream 
	whose events are currently processed is referred to as the 
	<kw>current source stream</kw>. The current source stream at the time 
	when the transformation is initiated is referred to as the 
	<kw>principal source stream</kw>.</p>

      <p>A possibly empty set of external values for stx-sheet
        parameters is supplied. These values are available for use within 
	expressions in the stx-sheet.</p>

      <p>No tree representation of the source document is constructed. 
	However, when processing each event, a limited amount of contextual 
	information is available from the system.</p>

      <p>Data arriving with source events form nodes of a data model for source 
	documents (see <specref ref='data-model'/>). The data model is a tree of 
	nodes. However, a processor does not build the complete tree; just a 
	limited set of nodes is available at each time of processing.</p>
      
      <p>The stx-sheet is a well-formed XML document that may be precompiled to 
	some kind of executable representation that can be reused to perform 
	multiple transformations. The stx-sheet can consist of several stx-sheet 
	modules contained in different files. One of these modules is the 
	<kw>principal stx-sheet module</kw>. The complete stx-sheet is assembled 
	by finding the stx-sheet modules referenced directly or indirectly from 
	the principal stx-sheet module using the <el>stx:include</el>
	declaration.</p>
 
      <p>The output of a transformation consists of one or more sequences of XML 
	events. These sequences of events are referred to as 
	<kw>result streams</kw>. The stream where events are currently emitted to 
	is referred to as the <kw>current result stream</kw>. The current result 
	stream at the time when the transformation is initiated is 
	referred to as the <kw>principal result stream</kw>.</p>

      <p>Each incoming event can cause an invocation of one or more rules within 
	the stx-sheet by means of a match <nt def="NT-pattern">pattern</nt>. The 
	actions such a rule may perform include emitting XML events to result 
	streams, saving working data to a working storage, accessing data written 
	to the working storage by previously executed rules, and invoking other 
	rules.</p>

      <note>
	<p>Source and result streams are abstract constructs that function
	  as input or output channels for STX transformations. Each source or 
	  result stream is identified with a URI. This URI must not be confused
	  with a URI of physical document that may be parsed to generate the
	  source stream or a URI of document the result stream may be serialized 
	  to. Stream URIs are passed to a resolver that maps abstract streams to 
	  physical resources.</p>
      </note>

      <div2 id="initiating-a-transformation">
	
	<head>Initiating a Transformation</head>

	<p>This document does not specify interfaces for initiating 
	  an STX transformation. Instead, these interfaces are 
	  implementation defined. This section describes the minimum amount 
          of information that must be supplied to execute a transformation:</p>

	<ulist>

	  <item>
            <p>An identification of the stx-sheet module that is to act as
              the principal stx-sheet module for the transformation.</p>
          </item>

	  <item>
            <p>A possibly empty set of values for stx-sheet parameters 
	      (name-value pairs). External parameter values are matched against 
	      global stx-sheet parameters.</p>
          </item>

	  <item>
            <p>An identification of the stream that is to act as the
              principal source stream.</p>
          </item>

	  <item>
            <p>An identification of the stream that is to act as the
              principal result stream.</p>
          </item>

	</ulist>

      </div2>

      <div2 id="context">
	
	<head>Context</head>

	<p>There is a contextual information available at each point during 
	  processing. It includes the data arriving with the current event and 
	  other data related to the state of processing. The contextual 
	  information at any particular instant during the processing 
	  is called the <kw>current context</kw>. The context information 
	  consists of the following parts:
	</p>

	<ulist>

	  <item><p><emph>current node data</emph> - The node which is the subject 
	      of the current event is called the <kw>current node</kw>. The 
	      information available for the current node depends on the node 
	      kind; see <specref ref='data-model'/> for details.</p></item>

	  <item><p><emph>ancestor stack</emph> - All ancestor nodes of the 
	      current node with all their properties are stored in the ancestor 
	      stack.</p></item>

	  <item><p><emph>position within siblings</emph> - Information about 
	      the position relative to other siblings is kept. The position is 
	      available for the current node and all its ancestors.</p>

	    <p>A position number is available for all node kind tests such as 
	      <code>node()</code>, <code>text()</code>, <code>cdata()</code>, 
	      <code>processing-instruction()</code>, <code>comment()</code>, 
	      and <code>doctype()</code>. For elements, the position is 
	      available for all qualified names or names 
	      containing * shortcut: <code>pre:lname</code>, <code>lname</code>, 
	      <code>pre:*</code>, <code>*:lname</code>, <code>*</code>. For 
	      processing instructions, the position is also available for each 
	      target. The position of attribute nodes is undefined.</p></item>

	</ulist>

      </div2>

      <div2 id="precedence-categories">

	<head>Precedence Categories</head>

	<p>Each incoming event can invoke a template within the stx-sheet by 
	  means of precedence categories and a match pattern (see 
	  <specref ref="match-patterns"/>). The template that is used to process 
	  the current node is called the <kw>current template</kw>. Templates 
	  can be separated into groups (see 
	  <specref ref='grouping-of-templates'/>). Top-level templates are 
	  considered to be members of the default group. The group containing
	  the current template is referred to as the <kw>current group</kw>.</p>

	<p>Templates are classed into the precedence categories according to 
	  their visibility from a <kw>base group</kw>. The base group can be 
	  either the current group or the group explicitly specified in the 
	  <att>group</att> attribute of current process statement. The 
	  visibility is defined using the <att>visibility</att> and 
	  <att>public</att> attributes for each template 
	  (see <specref ref='templates'/>).</p>

	<p>There are three precedence categories (listed with decreasing 
	  precedence):</p>

	<olist>

	  <item><p>templates from the base group and public templates 
	      (<code>public="yes"</code>) from groups that are children of the 
	      base group</p></item>

	  <item><p>group templates (<code>visibility="group"</code>) and global 
	      templates (<code>visibility="global"</code>) from all groups that 
	      are ancestors of the base group</p></item>

	  <item><p>all global templates 
	      (<code>visibility="global")</code></p></item>

	</olist>

	<p>The first precedence category is searched for the best matching 
	  template by means of a match pattern (see 
	  <specref ref='match-patterns'/>). If there is no matching template in 
	  the first precedence category, the second category is searched. If 
	  neither the first nor the second category contain a matching template, 
	  the third category is searched.</p>

      </div2>

      <div2 id="expressions">

	<head>Expressions</head>

	<p>STX uses an expression language called <kw>STXPath</kw> that is 
	  defined later in this specification (see 
	  <specref ref='stxpath-lang'/>).</p>

	<p>Expressions are used in match patterns, to specify conditions for 
	  different ways of processing of the current node, to generate text to 
	  be inserted to an output stream, or to access data from the ancestor 
	  stack.</p>

	<p>An STXPath expression may occur as the value of certain attributes 
	  on STX elements, and also within curly braces in attribute value 
	  templates (see <specref ref='avt'/>). The context within an stx-sheet 
	  where an STXPath expression appears may specify a required data type; 
	  the type of value that the expression is expected to return.</p>

	<p>STXPath expressions can include built-in functions whose expanded
	  names are identified by a specific namespace which is referred to as 
	  the STX function namespace. This document uses the 'sf' prefix as a 
	  shortcut for referring to the built-in STXPath functions</p> 

	<p>It is a static error if the value of an expression attribute does not 
	  match the STXPath production <nt def="NT-expression">expression</nt>. 
	  It is a dynamic error if any STXPath expression is evaluated and 
	  raises an error, or when it raises an error when converting to the 
	  required data type.</p> 

	<p>The attribute <att>stxpath-default-namespace</att> of the
	  <el>stx:transform</el> element (see 
	  <specref ref='transform-element'/>) may be used to define the 
	  namespace that will be used for an unprefixed name used as a 
	  <nt def="prod-NameTest">name test</nt> within a step of an
	  STXPath <nt def="NT-expression">expression</nt> or 
	  <nt def="NT-pattern">pattern</nt>. The value of the attribute is the 
	  namespace URI to be used.</p>

	<p>This default namespace URI applies only to elements; it does not 
	  apply to attributes. In the absence of this attribute, an unqualified 
	  <nt def="prod-NameTest">name test</nt> matches an element whose 
	  namespace URI is null: the default namespace of XML document (as 
	  defined by an <code>xmlns="some-uri"</code> declaration) is not 
	  used.</p>

      </div2>

      <div2 id="match-patterns">

	<head>Match Patterns</head>

	<p>
	  A match <nt def="NT-pattern">pattern</nt> specifies a set of 
	  conditions on the current context. If the current context satisfies 
	  the conditions the current node matches the pattern; if the current 
	  context does not satisfy the conditions the current node does not 
	  match the pattern. The syntax for STX 
	  <nt def="NT-pattern">patterns</nt> is a subset of the syntax for 
	  STXPath <nt def="NT-expression">expressions</nt>. In particular, 
	  patterns are in form of location paths that meet certain 
	  restrictions.</p>

	<p>Here are some examples of patterns:</p>

	<ulist>

	  <item><p><code>item</code> - matches any 'item' element from the 
	      namespace used for unprefixed STXPath path patterns (defined with 
	      'stxpath-default-namespace' attribute, no namespace by 
	      default)</p></item>

	  <item><p><code>list/item</code> - matches any 'item' element with a
	      'list' parent, where both elements are from the namespace used for 
	      unprefixed STXPath path patterns</p></item>

	  <item><p><code>chapter//list/item</code> - matches any 'item' element 
	      with a 'list' parent and a 'chapter' ancestor, where all three 
	      elements are from the namespace used for unprefixed STXPath path 
	      patterns</p></item>

	  <item><p><code>/root/list/*</code> - matches any element with a 'list' 
	      parent and a 'root' grand parent which is the document element, where 
	      both 'root' and 'list' elements are from the namespace used for 
	      unprefixed STXPath path patterns</p></item>

	  <item><p><code>pre:list[@id=5]/pre:item</code> - matches any
	      'item' element with a 'list' parent having an 'id'
	      attribute with a value of 5, where both elements are
	      from the namespace which is bound to the 'pre' prefix in
	      the stx-sheet for this rule</p></item>

	  <item><p><code>*[sf:position()=1]</code> - matches any element that is 
	      the first element child of its parent</p></item>

	  <item><p><code>node()</code> - matches any child node</p></item>

	  <item><p><code>text()</code> - matches any text node (including CDATA 
	      text node)</p></item>

	  <item><p><code>cdata()</code> - matches any CDATA text node</p></item>

	  <item><p><code>processing-instruction()</code> -  matches any 
	      processing instruction</p></item>

	</ulist>

	<p>A match <nt def="NT-pattern">pattern</nt> is a set of location path 
	  patterns separated with <code>|</code>. A location path pattern is a 
	  location path whose steps are separated either by the <code>/</code> 
	  (child axis) operator or by the <code>//</code> operator (descendant 
	  axis). Up to one predicate is allowed in each step. Predicate 
	  expressions are STXPath <nt def="NT-expression">expressions</nt> 
	  (see <specref ref='expressions'/>).</p>

	<p>Predicate expressions are evaluated using the current
	  context. If the result is a number, the result will be converted to 
	  true if the number is equal to the context position and will be 
	  converted to false otherwise. Thus a location path <code>p[3]</code> 
	  is equivalent to <code>p[sf:position()=3]</code>. Otherwise the
	  result will be converted to a boolean using the type
	  conversion rules described in <specref ref="stxpath-lang"/>.
	  If the result of evaluating and converting the predicate
	  expression is false, the pattern does not match the current node.</p>

	<p>If there is no matching template available a <kw>default rule</kw> 
	  is applied. One of three default rules, specified in the
	  <att>pass-through</att> attribute of <el>stx:transform</el>
	  or <el>stx:group</el> element can be used: <attval>none</attval> (to skip 
	  the current node), <attval>all</attval> (to pass through the current 
	  node), and <attval>text</attval> (to pass through the current node 
	  only if it is a text node). All these three default rules process 
	  all children of the current node, thus the processing does not stop
	  when a default rule is applied. The default rule can be set for the
	  stx-sheet (see <specref ref='transform-element'/>) or for a group 
	  (see <specref ref='grouping-of-templates'/>). This feature enables 
	  copying of documents with only a few changes, and to 
	  straightforwardly select just a few items from a document. The 
	  default behavior is to ignore all not matching events (value 
	  <attval>none</attval>) on the stx-sheet level. Groups inherit the
	  pass-through behavior from their parent group when not specified 
	  explicitly.</p>

	<p>It is possible that the current context matches more than one rule 
	  within a precedence category. Each rule has a computed priority 
	  value. The computed priority can be overridden with a 'priority' 
	  attribute value (see <specref ref='templates'/>).</p>

	<olist>
	  <item><p>If the <nt def="NT-pattern">pattern</nt> contains multiple 
	      alternatives separated with <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 <emph>qualified name</emph> 
	      or has the form either of 
	      <emph>processing-instruction(target)</emph> or 
	      <emph>cdata()</emph>, then the priority is 
	      <emph>0</emph>.</p></item>

	  <item><p>If the pattern has the form <emph>pre:*</emph> or 
	      <emph>*:lname</emph>, then the priority is 
	      <emph>-0.25</emph>.</p></item>

	  <item><p>If the pattern consists of just a <emph>node test</emph> 
	      other than cdata(), then the priority is 
	      <emph>-0.5</emph>.</p></item>

	  <item><p>Otherwise, the priority is <emph>0.5</emph>.</p></item>

	</olist>

	<p>
	  The rule with the highest priority is used. If there is more than one 
	  matching template rule with the highest priority, an STX processor  
	  must select the rule that occurs the last in the stx-sheet.</p>

	<note><p>The rules to determine an STX template to be instantiated are, 
	  with the exception of cdata(), the same as in XSLT 1.0 (see 
	  <bibref ref="XSLT"/>, 5.5).</p></note>

      </div2>

      <div2 id="avt">

	<head>Attribute Value Templates</head>

	<p>In an attribute that is designated as an attribute value template an 
	  STXPath expression can be used by surrounding the expression with 
	  curly braces ({}).</p>

	<p>An attribute value template consists of an alternating sequence of 
	  fixed parts and variable parts. A variable part consists of an STXPath 
	  expression enclosed in curly braces ({}). A fixed part may contain any 
	  characters, except that a left curly brace must be written as {{ and 
	  a right curly brace must be written as }}.</p>

	<p>The result of evaluating an attribute value template is obtained by 
	  concatenating the expansions of the fixed and variable parts. The 
	  expansion of a fixed part is obtained by replacing any double curly 
	  braces ({{ or }}) by the corresponding single curly brace. The 
	  expansion of a variable part is obtained by evaluating the enclosed 
	  STXPath expression and converting the resulting value to a string.</p>

	<p>If a left curly brace appears in an attribute value template without 
	  a matching right curly brace, or if a right curly brace occurs in an 
	  attribute value template outside an expression without being followed 
	  by a second right curly brace, a processor must signal a static error.</p>

      </div2>

      <div2 id="stx-errors">

	<head>Errors</head>

	<p>All errors that can occur during an STX transformation belong to one
	  of the following categories:</p>

	<ulist>

	  <item>
            <p><emph>warnings</emph> - The processor may issue a warning; the 
	      transformation must not be stopped.</p>
          </item>

	  <item>
            <p><emph>errors</emph> - The processor
	      must exit the transformation and issue an error message.</p>
          </item>

	</ulist>

	<p>This specification does not define how to issue a warning or an error. 
	  Implementations are free to use either the standard output or standard 
	  error output, or any convenient error handler.</p>

      </div2>

    </div1>

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

      <p>STX operates on a transformation sheet document and one or more source
	and result documents. The stx-sheet document uses the 
	<bibref ref="XQXPDM"/>. The source and result documents use a specific 
	STX Data Model which is derived from the &XQXPDM;. This section describes 
	additions, restrictions and modifications to the &XQXPDM; to derive the 
	STX Data Model. Additions describe information that is required for STX 
	processing but that is not contained in the &XQXPDM;. Restrictions 
	describe features of the &XQXPDM; that are never used in STX. 
	Modifications describe rules about the way in which trees in the STX Data 
	Model are constructed that are different from the rules of the &XQXPDM;. 
	Each item is marked as either addition, restriction or modification in 
	this section.</p>

      <div2 id="xml-schemas-and-types">
	
	<head>XML Schemas and Types</head>
	
	<kw>[Restriction]</kw>

	<p>The aspects of the <bibref ref="XQXPDM"/> that are dependent on W3C 
	  XML Schema are not used in STX.</p>

	<ulist>
	  <item><emph>Validity assessments:</emph> STX does not expect parsers to 
	    validate source documents; it does not work with any PSVI 
	    information.</item>
	  <item><emph>Type assessment:</emph> All elements and attributes are 
	    treated as untyped in STX.</item>
	</ulist>

      </div2>

      <div2 id="accessors">
	
	<head>Accessors</head>
	
	<p>A set of accessors is defined on all nodes in <bibref ref="XQXPDM"/>.
	  These accessors are shown with the prefix 'dm'. Additional, STX 
	  specific, accessors are shown with the prefix 'stxdm'. The prefixes 
	  are not bound to namespaces as both kinds of accessor are abstract, 
	  not directly accessible functions.</p>

	<kw>[Restriction]</kw>

	<p>The following accessors are never used during STX transformations on 
	  any node:</p>

	<ulist>
	  <item>dm:typed-value</item>
	  <item>dm:type</item> 
	  <item>dm:children</item>
	  <item>dm:nilled</item>
	</ulist>

	<kw>[Modification]</kw>

	<p>The dm:string-value accessor defined in <bibref ref="XQXPDM"/> is 
	  replaced with an STX specific accessor stxdm:string-value. The way in 
	  which the string value accessor of different kinds of nodes is computed 
	  is the following: </p>

	<ulist>
	  <item>documents - the empty string.</item>

	  <item>elements - if the very first child of an element happens 
	      to be a text node, the string-value of the element is the 
	      string-value of this text node. Otherwise, the string-value of the
	      element is the empty string.</item>

	  <item>attributes - the same as dm:string-value.</item>

	  <item>text nodes - the same as dm:string-value.</item>

	  <item>processing instructions - the same as dm:string-value.</item>

	  <item>comments - the same as dm:string-value.</item>

	</ulist>

      </div2>

      <div2 id="nodes">
	
	<head>Nodes</head>
	
	<kw>[Addition]</kw>

	<p>In addition to the kinds of nodes defined in 
	  <bibref ref="XQXPDM"/> STX recognizes two more kinds: document type 
	  nodes and cdata nodes.</p>

	<p><emph>Document type (doctype) nodes</emph> represent an information 
	  contained in document type declarations. Doctype has the following 
	  properties:</p>

	<ulist>
	  <item>base-uri, possibly empty</item>
	  <item>node-name</item>
	  <item>parent</item>
	  <item>system-id, possibly empty</item>
	  <item>public-id, possibly empty</item>
	</ulist>

	<p>Doctype nodes must satisfy these constrains:</p>

	<olist>
	  <item>Every doctype node must have a unique identity, distinct from 
	    all other nodes.</item>
	  <item>Parent property must contain the document element.</item>
	</olist>
	
	<p>The properties of doctype nodes are exposed by the following accessors 
	  defined in <bibref ref="XQXPDM"/>.</p>

	<ulist>
	  <item>dm:base-uri - the value of base-uri property</item>
	  <item>dm:node-kind - "doctype"</item>
	  <item>dm:node-name - the name of doctype</item>
	  <item>dm:parent - the document node</item>
	  <item>dm:attributes - the empty sequence</item>
	  <item>dm:namespaces - the empty sequence</item>
	</ulist>

	<p>Moreover, two extra STX accessors are defined for doctype nodes:</p>

	<ulist>
	  <item>stxdm:system-id - doctype system identifier</item>
	  <item>stxdm:public-id - doctype public identifier</item>
	</ulist>

	<p><emph>Cdata nodes</emph> encapsulate XML character content enclosed 
	  within CDATA boundaries. Cdata nodes have the same properties and 
	  accessors as text nodes defined in <bibref ref="XQXPDM"/> with the only 
	  exception of the dm:node-kind accessor which returns "cdata" for cdata 
	  nodes.</p>

	<note><p>The recognition of CDATA boundaries is optional in STX.
	    When these boundaries are ignored the data model of source documents 
	    contains no cdata nodes.</p></note>

	<kw>[Restriction]</kw>

	<p>Namespace nodes are not directly accessible in STX; they are not 
	  exposed as nodes. The information held in namespace nodes is instead 
	  made available using two functions: 
	  <function>sf:get-in-scope-namespaces</function> and
	  <function>sf:get-namespace-uri-for-prefix</function>. Properties of 
	  namespace nodes that are not exposed by these two functions can not 
	  be accessed by STX processors.</p>

	<note><p>The same restriction is applied in XPath 2.0.</p></note>

      </div2>

      <div2 id="atomic-values">
	
	<head>Atomic Values</head>
	
	<kw>[Modification]</kw>

	<p>In <bibref ref="XQXPDM"/>, atomic values belong to the value space of 
	  W3C XML Schema atomic types; this is, primitive simple types or types
	  derived by restriction from a primitive simple type. The STX data 
	  model recognizes only three atomic types:</p>

	<ulist>
	  <item><p>string</p></item>
	  <item><p>number</p></item>
	  <item><p>boolean</p></item>
	</ulist>

      </div2>

      <div2 id="whitespace-stripping">

	<head>Whitespace Stripping</head>

	<kw>[Restriction]</kw>

	<p>Source documents and an stx-sheet may contain whitespace 
	  nodes (text nodes consisting solely of whitespace characters: #x20, 
	  #x9, #xD or #xA). Such whitespace nodes may be removed according to 
	  the following rules. This process is referred to as whitespace
          stripping.</p>

	<p>Whitespace nodes are stripped from source documents if the 
	  <att>strip-space</att> attribute of <el>stx:transform</el> (see 
	  <specref ref='transform-element'/>) or <el>stx:group</el> 
	  (see <specref ref='grouping-of-templates'/>) is set to 
	  <attval>yes</attval>. Otherwise they are preserved and treated as 
	  any other text nodes.</p>

	<p>For stx-sheets, whitespace text nodes are preserved only if an 
	  ancestor element of this text node has the <att>xml:space</att>
          attribute set to <attval>preserve</attval>, and no closer ancestor 
	  element has <att>xml:space</att> set to <attval>default</attval>. All 
	  other whitespace nodes are removed from the stx-sheet.</p>

        <p>The STX elements <el>stx:text</el> and <el>stx:cdata</el> have the 
	  default <att>xml:space</att> attribute set to <attval>preserve</attval> 
	  which may be overridden in the stx-sheet. <att>xml:space</att> 
	  attributes on literal result elements will not be stripped from these 
	  elements.</p>
          
      </div2>

    </div1>

    <div1 id="sheet-structure">

      <head>STX Transformation Sheet Structure</head>

      <div2 id="stx-namespace">

	<head>STX Namespace</head>

	<p>The STX namespace has the URI 
	  <code>&STXNS;</code>.</p>

	<p>The STX functions namespace has the URI 
	  <code>&STXFNS;</code>.</p>

	<p>These two namespaces are recognized as reserved namespaces in STX 
	  transformation sheets, and may be used only for purposes specified in 
	  this document.</p>

      </div2>

      <div2 id="transform-element">

	<head>Transform Element</head>
	
	<p><kw>stx:transform</kw></p>

<eg id="stx-transform"><![CDATA[
<!-- Category: root -->
<stx:transform
  version = number
  pass-through = "none"|"all"|"text"
  recognize-cdata = "yes"|"no"
  stxpath-default-namespace = uri-reference
  strip-space = "yes"|"no"
  output-method = "xml"|"text"|qname-but-not-ncname
  output-encoding = string
  exclude-result-prefixes = tokens
  <!-- Content: top-level-elements -->
</stx:transform>
]]></eg>

	<p>STX transformation sheets are required to use the root element  
	  <el>stx:transform</el>.</p>

	<p>The <att>version</att> attribute contains a version number to 
	  distinguish language versions; this attribute is mandatory and its 
	  value must be <attval>1.0</attval> for this version of STX.</p>

	<p>The other attributes make it possible to set global properties of 
	  the transformation. Some of these properties (pass-through, 
	  recognize-cdata, strip-space) can also be set on the group level.</p>

	<ulist>
	  <item><p><att>pass-through</att> - This optional attribute specifies a 
	      default rule how to treat events no matching template is found for. 
	      These events are either ignored (<attval>none</attval>, default) or 
	      passed to the output without modification (<attval>all</attval>). 
	      For the <attval>text</attval> value, only text nodes are passed 
	      through to the output.</p></item>

	  <item><p><att>recognize-cdata</att> - This optional attribute 
	      specifies whether CDATA boundaries are recognized during the 
	      transformation. If so (<code>recognize-cdata=&quot;yes&quot;</code>), 
	      every CDATA section forms a single cdata node (see 
	      <specref ref='nodes'/>). Otherwise 
	      (<code>recognize-cdata=&quot;no&quot;</code>), CDATA boundaries 
	      are ignored and all consequent character data form a single text 
	      node. The default value is <attval>yes</attval>.</p></item>

	  <item><p><att>stxpath-default-namespace</att> - This optional attribute
	      specifies a namespace used for unprefixed name tests in STXPath 
	      expressions and patterns. See <specref ref='expressions'/> for 
	      details. No namespace is used by default.</p></item>

	  <item><p><att>strip-space</att> - This optional attribute specifies 
	      whether whitespace text nodes are stripped from source streams. 
	      See <specref ref='whitespace-stripping'/> for details. The default 
	      value is <attval>no</attval>.</p></item>

          <item><p><att>output-method</att> - This optional attribute
              specifies the preferred serialization method into a byte
              stream. The value <attval>xml</attval> (which is the default
              value) indicates to serialize the result as a well-formed XML
              fragment. The value <attval>text</attval> indicates to
              serialize only text and cdata nodes without any escaping
              and without any markup, especially without an XML declaration.
              An implementation may provide additionally serialization
              methods that have to be indicated by a valid QName in a
              non-empty namespace.</p></item>
          
	  <item><p><att>output-encoding</att> - This optional attribute specifies 
	      the preferred output encoding of the resulting byte stream. The 
	      value of this attribute should be treated case-insensitively; the 
	      value must contain only printable ASCII characters (#x21 - #x7E); 
	      the value must be a charset registered with the Internet 
	      Assigned Numbers Authority 
	      (see <bibref ref='IANA-CS'/>).</p>

	    <p>If the attribute is not present, the output encoding is UTF-8.
	      A compliant STX processor is not required to support any particular 
	      encoding other than UTF-8.</p></item>

          <item>
            <p><att>exclude-result-prefixes</att> - This optional
              attribute contains a whitespace-separated list of tokens,
              each of which is either a namespace prefix or the value
              <attval>#default</attval>. The namespace bound to each of the
              prefixes is designated as an excluded namespace,
              <attval>#default</attval> indicates the default namespace.
              It is a static error if there is no namespace
              declared for a specified prefix (or the default namespace
              for <attval>#default</attval>).</p>
            <p>The special value <attval>#all</attval> indicates that all
              namespaces declared for this <el>stx:transform</el> element
              are designated as excluded namespaces. It is a static
              error if <attval>#all</attval> is used together with other 
              tokens.</p>
            <p>A result stream element that was copied from the stx-sheet as 
	      a literal result element must be preceded by all necessary 
	      namespace declarations that are in scope for this element in the 
	      stx-sheet, unless the namespace is designated as an excluded 
	      namespace. By default the STX namespace (<code>&STXNS;</code>) is 
	      designated as an excluded namespace.</p>
            <p>However, a namespace that is in use for a result element name
              or an attribute name must be declared in any case, regardless
              of its designation as an excluded namespace.</p>
          </item>
	</ulist>

	<p>An <el>stx:transform</el> element can contain the following children 
	  from the STX namespace. These elements are called top-level 
	  elements:</p>

	<ulist>
	  <item><p>stx:include</p></item>
	  <item><p>stx:variable</p></item>
	  <item><p>stx:param</p></item>
	  <item><p>stx:buffer</p></item>
	  <item><p>stx:namespace-alias</p></item>
	  <item><p>stx:group</p></item>
	  <item><p>stx:template</p></item>
	  <item><p>stx:procedure</p></item>
	</ulist>

	<p>All top-level elements may occur multiple times. 
	  <el>stx:namespace-alias</el> element is allowed as top-level 
	  element only.</p>

      </div2>

      <div2 id="grouping-of-templates">

	<head>Grouping of Templates</head>

	<p>Templates can be organized into groups using the <el>stx:group</el> 
	  element. Groups of templates play a role in template matching 
	  (precedence categories are defined for groups) and determine 
	  the scoping of variables.</p>

	<p>Each stx-sheet has a virtual <kw>default group</kw> (represented 
	  by <el>stx:transform</el>) that is considered to be the parent 
	  of top-level groups. Explicit groups represented by <el>stx:group</el>
	  are not mandatory; many transformations can be done without grouping 
	  templates. On the other hand, templates separated to groups make it 
	  possible to define more precise transformation rules and to run safer 
	  complex transformations, especially on a well-known, regular input 
	  data.</p>

	<p><kw>stx:group</kw></p>

<eg><![CDATA[
<!-- Category: top-level or group -->
<stx:group
  name = qname
  pass-through = "none"|"all"|"text"|"inherit"
  recognize-cdata = "yes"|"no"|"inherit"
  strip-space = "yes"|"no"|"inherit">
  <!-- Content: group-elements -->
</stx:group>
]]></eg>

	<p>This element must be a child of either the <el>stx:transform</el> 
	  or the <el>stx:group</el> element. The optional <att>name</att> 
	  attribute contains a qualified name that must be unique in the 
	  stx-sheet. The name can be referenced by the <att>group</att> 
	  attribute of any of <el>stx:process-children</el>, 
	  <el>stx:process-attributes</el>, 
	  <el>stx:process-self</el>, <el>stx:process-siblings</el>, 
	  <el>stx:process-document</el> or <el>stx:process-buffer</el> 
	  instructions. In this event, the referenced group is used instead of 
	  the current group for matching. It is not possible to
	  reference the default group.</p>

	<p>It is a static error if an stx-sheet contains more than one group of 
	  the same name.</p>

	<p>The attributes <att>pass-through</att>, <att>recognize-cdata</att>,
          and <att>strip-space</att> are optional, and they set transformation 
	  properties specific for this group. Their meaning is exactly the 
	  same as the meaning of global properties of the same name
          specified on the <el>stx:transform</el> element (see 
	  <specref ref="transform-element"/>). The only difference is that the
          group attributes have the additional value <attval>inherit</attval>. 
	  This is the default value; it specifies that the value of the same 
	  property of the nearest ancestor group should be used. In other words,
          the value of the property stems from the nearest ancestor group
          that has the corresponding attribute set to a value distinct from
          <attval>inherit</attval>, or from the default value of the
          <el>stx:transform</el> element, if no such attribute was specified.
	  At each point of the processing, properties of the current base group
          apply.</p>

        <note>
          <p>The last sentence means, that for a visible template from a
            different group, the properties of this different group do not take
            effect untill this template is instantiated. Thus, the different
            properties do not apply during the matching process.</p>
        </note>

      </div2>

      <div2 id="sheet-inclusion">

	<head>STX Transformation Sheet Inclusion</head>

	<p>An stx-sheet may include another stx-sheet using the 
	  <el>stx:include</el> element.</p>

	<p><kw>stx:include</kw></p>

<eg><![CDATA[
<!-- Category: top-level or group -->
<stx:include
  href = uri-reference/>
]]></eg>

	<p>This declaration is used to insert additional stx-sheet modules into
	  the principal stx-sheet module. A circular inclusion is prohibited.</p>

	<p>This element must be top-level or a child of the <el>stx:group</el> 
	  element. <el>stx:include</el> is replaced with the 
	  <el>stx:transform</el> element of the included stx-sheet whereupon the 
	  included <el>stx:transform</el> then becomes an <el>stx:group</el> 
	  element. There is one exception: an <el>stx:namespace-alias</el> 
	  instruction from the included stx-sheet is always inserted as 
	  a top-level element. The resulting stx-sheet must meet the criteria for 
	  being a valid STX transformation sheet (for example concerning unique 
	  group and procedure names).</p>

        <p>The rules for attributes of the imported <el>stx:transform</el>
          element are as follows:</p>
        <ulist>
          <item>
            <p>The <att>version</att>, <att>output-method</att>, and
              <att>output-encoding</att> attributes will not affect the
              including stx-sheet. However, the included stx-sheet must
              be valid, that means its <att>version</att> attribute must
              be <attval>1.0</attval>.</p>
          </item>
          <item>
            <p>The <att>stxpath-default-namespace</att> and
              <att>exclude-result-prefixes</att> attributes will be used for the 
	      included stx-sheet. The <att>stxpath-default-namespace</att> and
              <att>exclude-result-prefixes</att> attributes of the
              <el>stx:transform</el> element of the principal stx-sheet
              module never affects included stx-sheet modules.</p>
          </item>
          <item>
            <p>The <att>pass-through</att>, <att>recognize-cdata</att>, and
              <att>strip-space</att> attributes become attributes of the
              new <el>stx:group</el> element. If one of these attributes is 
	      missing the <el>stx:group</el> element has an attribute of the 
	      same name with the default value defined for the 
	      <el>stx:transform</el> element. It never can have the value 
	      <attval>inherit</attval>.</p>
          </item>
        </ulist>

        <p>There is no difference between templates from the principal
          stx-sheet module and included templates in terms of matching
          precedence.</p>

      </div2>

    </div1>

    <div1 id="generating-output">

      <head>Generating Output</head>

      <p>STX templates are called sequentially for each incoming node rather 
	than from other templates. Pair events for the document and elements 
	match only one template, which is broken into two parts; the first part 
	is executed when the start event appears and the second one at the end 
	event. The two parts can be separated by the <el>stx:process-children</el> 
	element.</p>

      <div2 id="namespace-aliasing">

	<head>Namespace Aliasing</head>

	<p><kw>stx:namespace-alias</kw></p>

<eg><![CDATA[
<!-- Category: top-level -->
<stx:namespace-alias
  sheet-prefix = ncname|"#default"
  result-prefix = ncname|"#default"/>
]]></eg>

	<p>Namespaces of literal elements and attributes in stx-sheets can be 
	  mapped to different namespaces in result streams using the 
	  <el>stx:namespace-alias</el> element. Both attributes are mandatory 
	  and can contain either a prefix bound to a namespace or the 
	  <attval>#default</attval> keyword for the default namespace.</p>

      </div2>

      <div2 id="templates">

	<head>Templates</head>

	<p><kw>stx:template</kw></p>

<eg><phrase><![CDATA[
<!-- Category: top-level or group -->
<stx:template
  match = ]]><nt def="NT-pattern">pattern</nt><![CDATA[
  priority = number
  visibility = "local"|"group"|"global"
  public = "yes"|"no"
  new-scope = "yes"|"no">
  <!-- Content: template -->
</stx:template>
]]></phrase></eg>

	<p>Rules to process input events are written in templates. The
	  <el>stx:template</el> element must be a child of either the
	  <el>stx:transform</el> or the <el>stx:group</el> element.
          Templates match to the events by means of precedence categories and 
	  a <nt def="NT-pattern">pattern</nt> in the mandatory <att>match</att>
          attribute. The optional <att>priority</att> attribute can contain 
	  an explicit priority value used for matching (see 
	  <specref ref='match-patterns'/>).</p>

	<p>Two optional attributes; <att>visibility</att> and <att>public</att>; 
	  control whether the template is visible from other groups (and thus can 
	  match to the next event) or not. See 
	  <specref ref="precedence-categories"/> for meaning of the two 
	  attributes. The default value of the <att>visibility</att> attribute is
	  <attval>local</attval>. The default value of the <att>public</att>
	  attribute for top-level templates is <attval>yes</attval>, for
          group templates it is <attval>no</attval>. Whether a top-level
          template is public or not is important only when the stx-sheet
          is included into another stx-sheet, because every top-level
          template then becomes a group template, see <specref
          ref="sheet-inclusion" />.</p>

	<p>The optional <att>new-scope</att> attribute specifies whether the 
	  template creates new instances of group variables. The default value 
	  is <attval>no</attval>. A new set of group variables is created for 
	  each instantiated template with <code>new-scope=&quot;yes&quot;</code>. 
	  These variables shadow their former values and exist as long as the 
	  template is being processed.</p>

	<p>The content of templates may include both STX instructions and 
	  declarations, and literal elements. Literal elements are simply copied 
	  to the output.</p>

	<p>A <kw>text template</kw> is defined as the content of some elements 
	  (<el>stx:attribute</el>, <el>stx:variable</el>, 
	  <el>stx:param</el>, <el>stx:assign</el>, <el>stx:with-param</el>,
	  <el>stx:cdata</el>, 
	  <el>stx:processing-instruction</el>, <el>stx:comment</el>, 
	  <el>stx:message</el>). This is a part of template that is supposed to
	  generate nothing but character events to the current output stream.
	  If an event of another type is emitted, an STX processor is required 
	  either (1) to issue a run-time error or (2) to serialize the event or 
	  (3) to ignore the event according to the value of <att>markup</att>
	  attribute. See <specref ref="outputting-strings"/> for details.</p>

      </div2>

      <div2 id="procedures">

	<head>Procedures</head>

	<p><kw>stx:procedure</kw></p>

<eg><![CDATA[
<!-- Category: top-level or group -->
<stx:procedure
  visibility = "local"|"group"|"global"
  public = "yes"|"no"
  new-scope = "yes"|"no"
  name = qname>
  <!-- Content: template -->
</stx:procedure>
]]></eg>

	<p>Procedures are sub-templates that can be called by names (with the
	  <el>stx:call-procedure</el> instruction). The optional 
	  <att>visibility</att>, <att>public</att>, and <att>new-scope</att> 
	  attributes have the same meaning and default values as for
          templates. Only visible procedures can be called by name, the 
	  <att>new-scope</att> must be set to <attval>yes</attval> to create 
	  new copies of group variables. It is a static error if an stx-sheet 
	  contains more than one visible procedure with the same name within the 
	  same precedence category.</p>

	<p>The content of procedures may be the same as the content of 
	  templates.</p>

	<p><kw>stx:call-procedure</kw></p>

<eg><![CDATA[
<!-- Category: template -->
<stx:call-procedure
  name = qname
  group = qname>
  <!-- Content: stx:with-param* -->
<stx:call-procedure>
]]></eg>

	<p>The <el>stx:call-procedure</el> element makes it possible to invoke 
	  a procedure by its name. The <att>name</att> attribute is mandatory. 
	  The optional <att>group</att> attribute allows to use the specified 
	  group instead of the current group as a base group for calling 
	  the procedure.</p>

        <p>The target procedure will be determined according to the
          precedence categories described in
          <specref ref="precedence-categories"/>. If the first category
          does not contain a procedure with the requested name, then the
          second category will be searched. If neither the first nor the second
	  category contain such procedure, the third category is searched. 
	  It is a static error if none of the three precedence categories contain 
	  the requested procedure.</p>

      </div2>

      <div2 id="Parameters">

	<head>Parameters</head>

	<p>Values can be passed to stx-sheets or to their templates and 
	  procedures as parameters. Parameters are variables (see 
	  <specref ref="variables"/>) with the additional property that their 
	  value can be set by the caller of the stx-sheet, the template, or 
	  the procedure. Stx-sheet parameters behave in the same way
	  as group variables, they may be shadowed by variables or parameters
          of the same name in descendant groups. Note that a parameter that
          is only visible in a certain group will be initialized with the
          value passed to the stx-sheet anyway, regardless whether there
          is a shadowed parameter of the same name or not.
          Template/procedure parameters behave 
	  in the same way as local variables; thus they are only visible within 
	  the template or procedure they are passed to. There are two elements 
	  available to work with parameters:</p>

	<p><kw>stx:with-param</kw></p>

<eg><phrase><![CDATA[
<!-- Category: process-xxx, call-procedure -->
<stx:with-param
  name = qname
  select = ]]><nt def="NT-expression">expression</nt><![CDATA[
  <!-- Content: text template -->
</stx:with-param>
]]></phrase></eg>

	<p>Parameters are passed to templates or procedures using the 
	  <el>stx:with-param</el> element. The required <att>name</att> 
	  attribute specifies the name of the parameter. The value of the 
	  parameter is either the result returned by the 
	  <nt def="NT-expression">expression</nt> located in the optional
	  <att>select</att> attribute or the content of this element if the
	  <att>select</att> attribute is missing. If neither the 
	  <att>select</att> attribute nor the content is present the parameter 
	  value is the empty string.</p>

	<p>The <el>stx:with-param</el> instruction is allowed as a child of 
          the elements
	  <el>stx:process-children</el>, <el>stx:process-attributes</el>, 
	  <el>stx:process-self</el>, <el>stx:process-siblings</el>, 
	  <el>stx:process-document</el>, <el>stx:process-buffer</el>, or
	  <el>stx:call-procedure</el>, and must not have any of
          these elements in its content.</p>

	<p><kw>stx:param</kw></p>

<eg><phrase><![CDATA[
<!-- Category: top-level or group or template -->
<stx:param
  name = qname
  select = ]]><nt def="NT-expression">expression</nt><![CDATA[
  required = "yes" | "no">
  <!-- Content: text template -->
</stx:param>
]]></phrase></eg>

	<p>The <el>stx:param</el> element is allowed as a top-level or group
          element (indicating an stx-sheet parameter) and in templates or
          procedures (as a child of <el>stx:template</el> or 
	  <el>stx:procedure</el>). The required <att>name</att> attribute 
	  specifies the name of the parameter. The optional <att>select</att> 
	  attribute or the content of this element specifies a default value, 
	  which is both evaluated and used only when there is no value
          specified using the 
	  <att>select</att> attribute or the content of the appropriate 
	  <el>stx:with-param</el> element. Should both the <att>select</att>
	  attribute and the content be missing, the parameter defaults to the 
	  empty string.</p> 

        <p>Stx-sheet parameters are statically initialized while parsing 
	  the stx-sheet; only the <emph>static context</emph> information is 
	  available during the initialization. Template/procedure parameters  
	  are initialized at run-time. Since there is no current
	  source stream available during the static initialization,
          it is an error if an stx-sheet parameter has an 
	  <el>stx:process-children</el>,
          <el>stx:process-attributes</el>, <el>stx:process-self</el>, or 
	  <el>stx:process-siblings</el> instruction in its content.</p>
        
        <p>The optional <att>required</att> attribute may be used to indicate
          that a parameter is mandatory. The default value is 
	  <attval>no</attval>, indicating that the parameter is optional. If the 
	  value of the <att>required</att> attribute is <attval>yes</attval>,
          the <el>stx:param</el> element must be empty, and must have no 
	  <att>select</att> attribute. It is a dynamic error if the caller 
	  does not supply a value with <el>stx:with-param</el> for a required 
	  parameter.</p>
        
      </div2>

      <div2 id="copying-the-current-node">

	<head>Copying the Current Node</head>
	
	<p><kw>stx:copy</kw></p>

<eg><phrase><![CDATA[
<!-- Category: template -->
<stx:copy
  attributes = ]]><nt def="NT-pattern">pattern</nt><![CDATA[>
  <!-- Content: template -->
</stx:copy>
]]></phrase></eg>

	<p>The <el>stx:copy</el> element is used to copy the current node to the 
	  output. The optional <att>attributes</att> attribute contains a 
	  <nt def="NT-pattern">pattern</nt>. These attributes of the current node 
	  that match the pattern are copied to the output. If the 
	  <att>attributes</att> attribute is not present no attributes are copied 
	  with the current node.</p>

	<p>Thus, <code>attributes=&quot;@*&quot;</code> copies all attributes, 
	  <code>attributes=&quot;@foo|@bar&quot;</code> copies the <att>foo</att> 
	  and <att>bar</att> attributes only, 
	  <code>attributes=&quot;@*[not(name()='foo')]&quot;</code> copies all 
	  but the <att>foo</att> attribute, and 
	  <code>attributes=&quot;@*[false()]&quot;</code> does not copy any 
	  attributes as if the <att>attributes</att> attribute is missing at 
	  all.</p>

	<p>If the <el>stx:copy</el> instruction applies to a node other than 
	  element the <att>attributes</att> attribute is ignored.</p>

      </div2>

      <div2 id="processing-nested-events">

	<head>Processing Nested Events</head>

	<p><kw>stx:process-children</kw></p>

<eg><![CDATA[
<!-- Category: template -->
<stx:process-children
  group = qname
  filter-method = {uri-reference}
  filter-src = uri-specification | buffer-specification>
  <!-- Content: stx:with-param* -->
</stx:process-children>
]]></eg>

	<p>The instruction <el>stx:process-children</el> suspends the
          processing of the current template by processing the children
          of the current node.
	</p>
xxx
	<note>
	  <p>STX processors are not required to use any particular API. Any 
	    event-base API providing information about all events needed for 
	    an STX processor, such as <bibref ref='SAX2'/> or <bibref ref='StAX'/>, 
	    can be used. However, sometimes it makes sense to illustrate the 
	    functionality of an STX processor in terms 
	    of a specific well-known API.</p> 
	  <p>Using SAX2 terms: the <el>stx:process-children</el> instruction splits 
	    a template into two parts such that a SAX2 <code>startElement</code>
	    event causes the execution of the first part and the
	    corresponding SAX2 <code>endElement</code> event causes the
	    execution of the second part.</p>
	</note>

        <p>There must be always at most one <el>stx:process-children</el>
          instruction executed during the processing of a template. Moreover, it 
	  is an error if <el>stx:process-children</el> is encountered after an 
	  <el>stx:process-self</el> instruction or an 
	  <el>stx:process-siblings</el> instruction.</p>
        
	<note>
          <p>If a template does not contain any <el>stx:process-children</el> 
	    instruction, the children of this element will be skipped. The 
	    default rule applies only to nodes that are processed and no matching 
	    template is found.</p>
        </note>
	
        <note>
          <p>If the current node is neither an element node nor the document root 
	    then the <el>stx:process-children</el> instruction simply does
	    nothing.</p>
        </note>

	<p>The optional <att>group</att> attribute makes it possible to use the
	  specified group instead of the current group as the base for 
	  matching (see <specref ref="precedence-categories"/>). It is a static 
	  error if the group of the specified name is not available.</p>

        <p>The optional <att>filter-method</att> and <att>filter-src</att>
          attributes can be used to direct the processing of children
          to an external filter, see <specref ref="external-filters" />.</p>

      </div2>

      <div2 id="processing-attributes">

	<head>Processing Attributes</head>

	<p><kw>stx:process-attributes</kw></p>

<eg><![CDATA[
<!-- Category: template -->
<stx:process-attributes
  group = qname>
  <!-- Content: stx:with-param* -->
</stx:process-attributes>
]]></eg>

	<p>This instruction is used to apply templates to attributes of 
	  an element node.</p>

	<p>The optional <att>group</att> attribute makes it possible to use the
	  specified group instead of the current group as the base for 
	  matching (see <specref ref="precedence-categories"/>). It is a static 
	  error if the group of the specified name is not available.</p>

      </div2>

      <div2 id="processing-siblings">

	<head>Processing Siblings</head>

	<p><kw>stx:process-siblings</kw></p>

<eg><phrase><![CDATA[
<!-- Category: template -->
<stx:process-siblings
  while = ]]><nt def="NT-pattern">pattern</nt>
  until = <nt def="NT-pattern">pattern</nt><![CDATA[
  group = qname
  filter-method = {uri-reference}
  filter-src = uri-specification | buffer-specification>
  <!-- Content: stx:with-param* -->
</stx:process-siblings>
]]></phrase></eg>

	<p>The <el>stx:process-siblings</el> instruction suspends the
          processing of the current template and processes the following
          siblings of the current node. The processing can be terminated
          by one of <att>while</att> or <att>until</att> conditions,
          or because of the end of the parent element or the current
          buffer (see <el>stx:process-buffer</el>).</p>

        <note>
          <p>If the current node is an attribute node or the document root
          node the <el>stx:process-siblings</el> instruction does nothing.</p>
        </note>

        <p>The optional <att>while</att> attribute contains a
          <nt def="NT-pattern">pattern</nt>. The next siblings are processed 
	  as long as they match the specified pattern. The first non-matching 
	  node stops the processing; this node is not processed by this 
	  <el>stx:process-siblings</el> instruction.. The <att>while</att> 
	  attribute defaults to <code>node()</code>.</p>

        <p>The optional <att>until</att> attribute contains a
          <nt def="NT-pattern">pattern</nt>. The next siblings are processed
	  until a node matching the pattern is encountered; this node is not
          processed by this <el>stx:process-siblings</el> instruction.
          The <att>until</att> attribute defaults to
          <code>node()[false()]</code>.</p>

        <p>If both <att>while</att> and <att>until</att> attributes have been
          specified then both conditions have to be met. For example
          <code>&lt;stx:process-siblings while="foo" until="foo"/&gt;</code>
          does not process any siblings. Variable bindings used within the
          patterns will be interpreted with regard to the current context.
          That means changed group variables affect the evaluation, whereas
          new instances of group variables or local variables are not
          visible.</p>

        <note>
          <p>Whitespace text nodes not stripped from the document must be
          considered in the patterns, particularly when using the
          <att>while</att> attribute. A typical attribute specification
          would be <code>while="foo | text()"</code> which processes
          all following <code>foo</code> elements and potential text
          nodes between these <code>foo</code> elements.</p>
        </note>

	<p>The optional <att>group</att> attribute makes it possible to use the
	  specified group instead of the current group as the base for 
	  matching (see <specref ref="precedence-categories"/>). It is a static 
	  error if the group of the specified name is not available.</p>

        <p>The optional <att>filter-method</att> and <att>filter-src</att>
          attributes can be used to direct the processing of siblings
          to an external filter, see <specref ref="external-filters" />.</p>
        
        <p>An <el>stx:process-siblings</el> instruction encountered during
          the processing of siblings does not affect the <att>while</att> and 
	  <att>until</att> conditions of the previous 
	  <el>stx:process-siblings</el>. In other words: nested
          <el>stx:process-siblings</el> instructions process at most the
          siblings chosen in the preceding <el>stx:process-siblings</el>.
          That means
          <el>stx:process-siblings</el> also returns if there are no more
          siblings in the input available or a preceding
          <el>stx:process-siblings</el> terminates.</p>

        <p>Though multiple <el>stx:process-siblings</el> instructions may
          appear within the same template it is an error if an 
	  <el>stx:process-children</el> or <el>stx:process-self</el>
          instruction will be encountered after <el>stx:process-siblings</el>.</p>
        
      </div2>

      <div2 id="running-overridden-templates">

	<head>Running Overridden Templates</head>

	<p><kw>stx:process-self</kw></p>

<eg><![CDATA[
<!-- Category: template -->
<stx:process-self
  group = qname
  filter-method = {uri-reference}
  filter-src = uri-specification | buffer-specification>
  <!-- Content: stx:with-param* -->
</stx:process-self>
]]></eg>

	<p>This instruction is used to process the current node using the 
	  template that would have been chosen if the current template was 
	  not present in the stx-sheet. The current template will not be
          instantiated again for this node, even in a chain of calls to
          <el>stx:process-self</el>. There must be always at most one 
	  <el>stx:process-self</el> instruction executed during the
          processing of a template.
          Moreover it is an error if an <el>stx:process-self</el> 
	  instruction is encountered after an <el>stx:process-children</el> or 
	  an <el>stx:process-siblings</el> instruction in a template.</p>

	<p>The optional <att>group</att> attribute makes it possible to use the
	  specified group instead of the current group as the base for 
	  matching (see <specref ref="precedence-categories"/>). It is a static 
	  error if the group of the specified name is not available.</p>

        <note>
          <p>If no <att>group</att> attribute has been specified then
            the current group will be used for choosing the next best
            matching template. This is also true if the current group
            has been automatically entered via a public template.</p>
        </note>
        
        <p>The optional <att>filter-method</att> and <att>filter-src</att>
          attributes can be used to direct the processing of the context
          node to an external filter, see
          <specref ref="external-filters" />.</p>
        
      </div2>

      <div2 id="outputting-strings">

	<head>Outputting Strings</head>

	<p><kw>stx:value-of</kw></p>

<eg><phrase><![CDATA[
<!-- Category: template -->
<stx:value-of
  select = ]]><nt def="NT-expression">expression</nt><![CDATA[
  separator = { string } />
]]></phrase></eg>

	<p>This instructions emits characters to the result stream. The
          mandatory <att>select</att> attribute contains an STXPath
          <nt def="NT-expression">expression</nt> whose value may be any
          sequence of items. The optional <att>separator</att> attribute
          defaults to a single space character.
          This element is always empty.</p>
        <p>The result of this instruction is the concatenation of the
          string values of the items in the sequence from the
          <att>select</att> attribute, with each of these string values
          except the last being followed by the string that is the effective
          value of the <att>separator</att> attribute. If the effective
          value of the <att>separator</att> attribute is a zero-length
          string, then all items in the sequence are processed and the
          results are concatenated with no separator.</p>

	<p><kw>stx:text</kw></p>

<eg><![CDATA[
<!-- Category: template -->
<stx:text
  markup = "error"|"ignore"|"serialize">
  <!-- Content: template -->
</stx:text>
]]></eg>

	<p>This instruction emits literal character data to the result
          stream.</p>

        <p>The optional <att>markup</att> attribute determines how non-text 
	  nodes in the content of <el>stx:text</el> should be handled: 
	  <attval>error</attval> causes the processor to raise a run-time
          error for such nodes, <attval>ignore</attval> ignores any markup by 
	  emitting only the string value of the contents to the result stream, 
	  <attval>serialize</attval> emits any markup serialized as text. The 
	  default value is <attval>error</attval>.</p>
        
        <note>
          <p>The string created by <code>markup="serialize"</code> may
          vary in different STX implementations, because some of the
          lexical representation is not relevant for the information coded
          in XML. For example every STX implementation may choose its own
          order for serializing attributes.</p>
        </note>

        <p>The <el>stx:text</el> element has an implicit <att>xml:space</att>
          attribute with the default value <attval>preserve</attval>. Thus
          the content is normally neither normalized nor stripped should it
          contain whitespace characters only.</p>

	<p><kw>stx:cdata</kw></p>

<eg><![CDATA[
<!-- Category: template -->
<stx:cdata>
  <!-- Content: text template -->
</stx:cdata>
]]></eg>

	<p>This instructions emits literal data as a CDATA section to the
          result stream.</p>
        <p>The <el>stx:cdata</el> element has an implicit <att>xml:space</att>
          attribute with the default value <attval>preserve</attval>. Thus
          the content is normally neither normalized nor stripped should it
          contain whitespace characters only.</p>

      </div2>

      <div2 id="outputting-elements-and-attributes">

	<head>Outputting Elements and Attributes</head>

	<p><kw>stx:element</kw></p>

<eg><![CDATA[
<!-- Category: template -->
<stx:element
  name = {qname}
  namespace = {uri-reference}>
  <!-- Content: template -->
</stx:element>
]]></eg>

	<p>This instruction is used to generate an element. It has the same 
	  meaning as in <bibref ref='XSLT'/>.</p>

	<p><kw>stx:start-element</kw></p>

<eg><![CDATA[
<!-- Category: template -->
<stx:start-element
  name = {qname}
  namespace = {uri-reference}/>
]]></eg>

	<p><kw>stx:end-element</kw></p>

<eg><![CDATA[
<!-- Category: template -->
<stx:end-element
  name = {qname}
  namespace = {uri-reference}/>
]]></eg>

	<p>There are separate instructions available to output an element start 
	  tag and end tag. The <att>name</att> attribute is required 
	  for both instructions. The both elements must be empty.</p>

	<p>A compliant STX processor is required to produce a well-formed XML 
	  output. An attempt to create an end-tag without a matching start-tag 
	  must be reported as an error by the STX processor.</p>

	<p><kw>stx:attribute</kw></p>

<eg><phrase><![CDATA[
<!-- Category: template -->
<stx:attribute
  name = {qname}
  namespace = {uri-reference}
  select = ]]><nt def="NT-expression">expression</nt><![CDATA[>
  <!-- Content: text template -->
</stx:attribute>
]]></phrase></eg>

	<p>This instruction is used to generate an attribute. It has the same 
	  meaning as in <bibref ref='XSLT'/>. Alternatively, the value of the
          generated attribute may be specified in the optional
          <att>select</att> attribute. It is a static error if this
          instruction has a <att>select</att> attribute and is not empty.</p>

        <p><el>stx:attribute</el> must follow an element-starting instruction
          (<el>stx:element</el>, <el>stx:start-element</el>,
          <el>stx:copy</el>, or a literal element) and no other
          output-generating instructions are allowed between the 
          element-starting instruction and <el>stx:attribute</el>.
	  Otherwise, an STX processor is required to issues an error.</p>

      </div2>

      <div2 id="outputting-other-nodes">

	<head>Outputting Other Nodes</head>

	<p><kw>stx:processing-instruction</kw></p>

<eg><![CDATA[
<!-- Category: template -->
<stx:processing-instruction
  name = {ncname}
  select = ]]><nt def="NT-expression">expression</nt><![CDATA[>
  <!-- Content: text template -->
</stx:processing-instruction>
]]></eg>

	<p>This instruction is used to generate a processing
          instruction. It has the same meaning as in <bibref ref='XSLT'/>.
	  Alternatively, the value of the generated processing instruction may 
	  be specified in the optional <att>select</att> attribute. It is 
	  a static error if this instruction has a <att>select</att> attribute 
	  and is not empty.</p>

	<p><kw>stx:comment</kw></p>

<eg><![CDATA[
<!-- Category: template -->
<stx:comment
  select = ]]><nt def="NT-expression">expression</nt><![CDATA[>
  <!-- Content: text template -->
</stx:comment>
]]></eg>

	<p>This instruction is used to generate a comment. It has the same 
	  meaning as in <bibref ref='XSLT'/>. Alternatively, the value of the 
	  generated comment may be specified in the optional <att>select</att> 
	  attribute. It is a static error if this instruction has 
	  a <att>select</att> attribute and is not empty.</p>
	
	<p><kw>stx:doctype</kw></p>

<eg><![CDATA[
<!-- Category: template -->
<stx:doctype
  system-id = {system-literal}
  public-id = {pubid-literal}>
  <!-- Content: text template -->
</stx:doctype>
]]></eg>

	<p>This instruction is used to generate a document type declaration that
	  contains or points to markup declarations. The <att>system-id</att> 
	  attribute contains a system identifier. The <att>public-id</att> 
	  attribute contains a public identifier. Both attributes are optional.
	  This element must be either empty or it must contain a valid internal 
	  subset of markup declarations (see <bibref ref='XML1'/>).</p>

      </div2>

      <div2 id="conditions">

	<head>Conditions</head>

	<p><kw>stx:if</kw></p>

<eg><phrase><![CDATA[
<!-- Category: template -->
<stx:if
  test = ]]><nt def="NT-expression">expression</nt><![CDATA[>
  <!-- Content: template -->
</stx:if>
]]></phrase></eg>

	<p>The mandatory <att>test</att> attribute contains an STXPath
          <nt def="NT-expression">expression</nt> evaluating to boolean.
          The content template is instantiated if and only 
	  if the <att>test</att> attribute has evaluated to true.</p>

	<p><kw>stx:else</kw></p>

<eg><![CDATA[
<!-- Category: template -->
<stx:else>
  <!-- Content: template -->
</stx:else>
]]></eg>

	<p>This instruction must follow immediately after <el>stx:if</el>; 
	  a static error must be reported otherwise. The content template is 
	  instantiated if and only if the <att>test</att> attribute of the 
	  preceding <el>stx:if</el> instruction has evaluated to false.</p>

	<p><kw>stx:choose</kw></p>

<eg><phrase><![CDATA[
<!-- Category: template -->
<stx:choose>
  <stx:when
    test = ]]><nt def="NT-expression">expression</nt><![CDATA[>
    <!-- Content: template -->
  </stx:when>+
  <stx:otherwise>
    <!-- Content: template -->
  </stx:otherwise>?
</stx:choose>
]]></phrase></eg>

	<p>The same meaning as in <bibref ref='XSLT'/>.</p>

      </div2>

      <div2 id="loops">

	<head>Loops</head>

	<p><kw>stx:for-each-item</kw></p>

<eg><phrase><![CDATA[
<!-- Category: template -->
<stx:for-each-item
  name = qname
  select = ]]><nt def="NT-expression">expression</nt><![CDATA[>
  <!-- Content: template -->
</stx:for-each-item>
]]></phrase></eg>

	<p>The <el>stx:for-each-item</el> instruction contains a template that 
	  is instantiated for each item of the sequence specified by the 
	  mandatory <att>select</att> attribute.</p>

	<p>The mandatory <att>name</att> attribute specifies a name of local 
	  variable that is declared automatically for each item, and that 
	  contains the current item.</p>

	<p>Neither the current node (accessed with <code>.</code>) nor 
	  sf:position() change inside <el>stx:for-each-item</el>.</p>

        <p><kw>stx:while</kw></p>

<eg><phrase><![CDATA[
<!-- Category: template -->
<stx:while
  test = ]]><nt def="NT-expression">expression</nt><![CDATA[>
  <!-- Content: template -->
</stx:while>
]]></phrase></eg>

        <p>The mandatory <att>test</att> attribute contains an STXPath
          <nt def="NT-expression">expression</nt> evaluating to boolean.
          The contents of the <el>stx:while</el> element is instantiated
          repeatedly as long as the <att>test</att> attribute evaluates to
          true.</p>
        
      </div2>

      <div2 id="multiple-input-documents">

	<head>Multiple Input Documents</head>

	<p><kw>stx:process-document</kw></p>

<eg><phrase><![CDATA[
<!-- Category: template -->
<stx:process-document
  href = ]]><nt def="NT-expression">expression</nt><![CDATA[
  base = {uri-reference}|"#input"|"#sheet"
  group = qname
  filter-method = {uri-reference}
  filter-src = uri-specification | buffer-specification>
  <!-- Content: stx:with-param* -->
</stx:process-document>
]]></phrase></eg>

	<p>An stx-sheet can process further source streams in addition to this 
	  supplied when the transformation is invoked (the principal source 
	  stream). The current source stream can be changed with the 
	  <el>stx:process-document</el> instruction. When this instruction is
          instantiated the <nt def="NT-expression">expression</nt> in the
          mandatory <att>href</att> attribute will be evaluated, each
          item in the resulting sequence will be converted sequentially to
          a string (a URI), and its value will be used to identify and to
          process a new source stream. When new source streams for all items are
	  processed, the execution of the template containing the 
	  <el>stx:process-document</el> instruction continues with the original 
	  source stream.</p>

        <p>If a URI is a relative URI then the base URI will be derived
          from the type of item in the sequence that represents this
          URI. In case this item is a node then its base URI will be used,
          otherwise the base URI of the stx-sheet will be used.
          Alternatively, the optional <att>base</att> attribute can be used
          to specify explicitly which base URI should be used. Its value
          must be either an absolute URI, the string
          <attval>#input</attval> in which case the base URI of the
          current input stream will be used, or the string
          <attval>#stylesheet</attval> in which case the base URI of the
          principal stx-sheet will be used.</p>
                
	<p>The optional <att>group</att> attribute makes it possible to use the
	  specified group instead of the current group as the base for matching 
	  (see <specref ref="precedence-categories"/>). It is a static error if 
	  the group of the specified name is not available.</p>

	<note>
	  <p>When processing a new document, the ancestor stack of the original 
	    document is not available for matching and navigation. Each
	    new document has an ancestor stack of its own.</p>
	</note>

        <p>The optional <att>filter-method</att> and <att>filter-src</att>
          attributes can be used to direct the processing of document
          to an external filter, see <specref ref="external-filters" />.</p>
        
      </div2>

      <div2 id="multiple-output-documents">

	<head>Multiple Output Documents</head>

	<p><kw>stx:result-document</kw></p>

<eg><phrase><![CDATA[
<!-- Category: template -->
<stx:result-document
  href = {uri-reference}
  output-method = "xml"|"text"|qname-but-not-ncname
  output-encoding = string>
  <!-- Content: template -->
</stx:result-document>
]]></phrase></eg>

	<p>An stx-sheet can produce further result streams in addition to the 
          principal result stream. The current result stream can be changed
          with the <el>stx:result-document</el> instruction. Events generated
          as the result of executing instructions contained within the 
	  <el>stx:result-document</el> element are emitted to a new current 
	  result stream identified with the URI which is specified by an 
	  attribute value template in the required <att>href</att> attribute.
          Then, the execution of instructions behind the end of the
          <el>stx:result-document</el> element continues to emit events into
	  the original result stream.</p>

	<p>If the <att>href</att> attribute contains a relative URI, the 
	  <kw>base output URI</kw> is used to resolve the URI. The base output
	  URI is implementation defined; it can be provided through an 
	  interface or otherwise specified by an STX processor.</p>

        <p>The optional attributes <att>output-method</att> and
          <att>output-encoding</att> can be used to specify a preferred
          output method and an output encoding for the new result stream.
          Their semantics is the same as for <el>stx:transform</el>,
          see <specref ref="transform-element" />.
          If one of these attributes is not present then the values will
          be used that are in effect for the principal result stream.</p>
      </div2>

      <div2 id="buffers">

	<head>Buffers</head>

	<p>A sequence of events can be stored into an object called a buffer. The 
	  stored events can be emitted and processed later, in the same way as 
	  events emitted from a source stream. The events are emitted from 
	  a buffer in the same order as they were stored in. In other words, the
	  buffers are temporary storages of the 'first in first out' type. The
	  events stored in a buffer must represent a well-formed external general 
	  parsed entity (the restriction on a single root node is relaxed).</p>

	<p>There are two types of buffers:</p>

	<ulist>

	  <item><p><emph>group buffers</emph> - <el>stx:buffer</el> is child 
	      of either <el>stx:transform</el> or <el>stx:group</el>. Top-level 
	      buffers are considered members of the top-most default group 
	      that exists for each stx-sheet.</p></item>

	  <item><p><emph>local buffers</emph> - Declared within 
	      templates.</p></item>

	</ulist>

	<p>A buffer must be declared before it can be used. The same rules as for 
	  variables (see <specref ref="variables"/>) apply for the the visibility 
	  of buffers, their shadowing, and the creating of new instances for 
	  new-scope templates (see <specref ref="templates"/>).</p>

	<p><kw>stx:buffer</kw></p>

<eg><![CDATA[
<!-- Category: top-level, group or template-->
<stx:buffer
  name = qname>
  <!-- Content: template -->
</stx:buffer>
]]></eg>

	<p>The <el>stx:buffer</el> element declares a buffer. The mandatory 
	  <att>name</att> attribute contains a qualified name identifying the 
	  declared buffer. The buffer is initialized with events generated as
	  a result of evaluation of the content of the <el>stx:buffer</el> 
	  declaration. If the content is empty (<el>stx:buffer</el> element has 
	  no children) the buffer is empty.</p>

	<p>For group buffers, the content of <el>stx:buffer</el> element is 
	  evaluated statically. It is a static error if the element
	  <el>stx:buffer</el> declaring a <emph>group buffer</emph> contains 
	  an <el>stx:process-children</el>, <el>stx:process-self</el>,
	  <el>stx:process-siblings</el>, <el>stx:process-attributes</el>, 
	  <el>stx:process-document</el>, <el>stx:process-buffer</el>, or 
	  <el>call-procedure</el> instruction in its content.</p>

	<p><kw>stx:result-buffer</kw></p>

<eg><![CDATA[
<!-- Category: template -->
<stx:result-buffer
  name = qname
  clear = "yes"|"no">
  <!-- Content: template -->
</stx:result-buffer>
]]></eg>

	<p>The <el>stx:result-buffer</el> instruction directs events emitted by
	  its content into the buffer specified with the mandatory <att>name</att> 
	  attribute rather than to the current result stream. The buffer must be
	  declared with <el>stx:buffer</el> before it can be employed in 
	  <el>stx:result-buffer</el>.</p>

	<p>If the buffer specified with the <att>name</att> attribute already 
	  contains a sequence of events, the new sequence of events is appended
	  behind the last event in the previously stored sequence normally. If 
	  the <el>stx:result-buffer</el> element has the optional 
	  <att>clear</att> attribute with the value of <attval>yes</attval>, the 
	  previously stored events are removed from the buffer before the new 
	  sequence of events is stored in. The <att>clear</att> attribute 
	  defaults to <attval>no</attval>.</p>

	<note>
	  <p>To clear a buffer without storing a new sequence of events, use the
	    <el>stx:result-buffer</el> instruction with no content:<code>
	    <![CDATA[<stx:result-buffer name="my-buffer" clear="yes"/>]]></code></p>
	</note>

        <p>The events stored in a buffer will not be available for a following
          <el>stx:process-buffer</el> before the <el>stx:result-buffer</el> 
	  instruction has terminated. Until then the previous contents is accessible. 
	  Thus for processing a buffer and storing the result in the same buffer 
	  again use <code><![CDATA[<stx:result-buffer name="b" clear="yes">
          <stx:process-buffer name="b"/> </stx:result-buffer>]]></code></p>

        <p>It is an error if this instruction is executed for a buffer that acts 
	  already as (current or suspended) result buffer.</p>
        
	<p><kw>stx:process-buffer</kw></p>

<eg><![CDATA[
<!-- Category: template -->
<stx:process-buffer
  name = qname
  group = qname
  filter-method = {uri-reference}
  filter-src = uri-specification | buffer-specification>
  <!-- Content: stx:with-param* -->
</stx:process-buffer>
]]></eg>
	
	<p>The <el>stx:process-buffer</el> instruction emits the events
          currently stored in the buffer specified by the mandatory
          <att>name</att> attribute to the STX processor. The events are 
	  processed in the same way as events supplied by source streams. When 
	  the very last event from the buffer is processed, the processing in the 
	  current template continues with an instruction, declaration or literal
	  next to the <el>stx:process-buffer</el> instruction.</p>

        <note>
          <p>Changes to the contents of a buffer that is currently processed
            will not affect this processing. The <el>stx:process-buffer</el>
            instruction creates an internal copy of the contained events
            and emits them afterwards.</p>
        </note>

	<p>The optional <att>group</att> attribute makes it possible to use the
	  specified group instead of the current group as the base for matching 
	  (see <specref ref="precedence-categories"/>). It is a static error if 
	  the group of the specified name is not available.</p>

        <p>The optional <att>filter-method</att> and <att>filter-src</att>
          attributes can be used to direct the processing of buffer's
          contents to an external filter, see
          <specref ref="external-filters" />.</p>
        
	<p>The processing of events from a buffer does not mean the emptying of 
	  this buffer. Once a sequence of events is stored in the buffer, it can
	  be processed repeatedly.</p>

	<note>
	  <p>A buffer is not treated as a new document, but rather as
	    if events emitted from the buffer originate from the
	    current source stream. The ancestor stack of the current
	    source stream remains available for matching and 
	    navigation when processing nodes from the buffer.</p>
	</note>

      </div2>

      <div2 id="external-filters">

        <head>Using external filters</head>

        <p>The main task of an STX processor is to transform a stream of
          source XML events into a stream of result XML events (see
          <specref ref="concepts" />). Using this paradigm, an STX processor
          can be seen as a typical representative of a filter of XML events.</p>
        
        <p>Moreover, STX can as well direct any XML event stream to an
          external filter process and incorporate the result of
          this processing into the current result stream. Thus an
          STX process may split a large XML document into smaller fragments,
          pass each of these fragments to an external filter (for example
          an XSLT processor), and combine the results into a large XML result
          document.</p>

        <note>
          <p>The term <emph>external</emph> refers to a processing outside of
            the STX scope. An actual filter might be as well a built-in part
            of an STX processor implementation.</p>
        </note>
        
        <p>Each event stream directed to an external filter represents
          a well-formed XML fragment.</p>

	<note><p>In terms of SAX2, that means the stream starts with 
	    a <code>startDocument</code> event, followed by a sequence of
	    <code>startPrefixMapping</code> events, one for each namespace
	    in scope, followed by the SAX2 representation of the piece of
	    XML to be processed. The stream will be completed by the
	    matching <code>endPrefixMapping</code> and <code>endDocument</code>
	    events. Before incorporating the SAX2 stream resulting from the
	    filter into the current result stream, its enclosing
	    <code>startDocument</code> and <code>endDocument</code> events
	    will be discarded.</p>
	</note>
        
        <p>STX provides two attributes for identifying the external filter:
          <att>filter-method</att> and <att>filter-src</att>. Both attributes
          can be used on <el>stx:process-children</el>
          (<specref ref="processing-nested-events" />),
          <el>stx:process-siblings</el>
          (<specref ref="processing-siblings" />),
          <el>stx:process-self</el> (<specref
          ref="running-overridden-templates" />),
          <el>stx:process-document</el>
          (<specref ref="multiple-input-documents" />), and
          <el>stx:process-buffer</el> (<specref ref="buffers" />).</p>

        <p>The optional <att>filter-method</att> attribute contains a URI that
          identifies a filter method to be used. This specification cannot
          provide a complete list of filters and their URIs. However,
          if a filter method is described using an XML vocabulary in a
          well-known namespace, its namespace URI is the recommended value
          for the <att>filter-method</att> attribute, see
          <specref ref="filter-uris" /> for a list of currently recommended
          URIs. A conformant STX processor does not have to support any
          external filters. The function
          <function>sf:filter-available</function> may be used to check,
          whether the current STX processor supports the requested filter.</p>
        
        <p>It is a static error to specify both <att>filter-method</att> and 
	  <att>group</att> attributes. It is a run-time error if the requested 
	  filter is not supported by the processor.</p>

        <p>The optional <att>filter-src</att> attribute provides additional
          information about the source to initialize the desired filter.
          There are two kinds of values allowed for this attribute:</p>

        <ulist>
          <item>
            <p>A &lt;uri-specification&gt;, as defined in
              <bibref ref="XSLFO"/>, repeated here for convenience:</p>

            <p>A sequence of characters that is "url(", followed by
              optional white space, followed by an optional single quote
              (') or double quote (") character, followed by a URI
              reference as defined in <bibref ref="RFC2396" />, followed
              by an optional single quote (') or double quote (")
              character, followed by optional white space, followed by ")".
              The two quote characters must be the same and must both be
              present or absent. If the URI reference contains a single
              quote, the two quote characters must be present and be double
              quotes.</p>

            <p>The document identified by that URI provides the source
              for the external filter.</p>
          </item>
          <item>
            <p>A &lt;buffer-specification&gt;: A sequence of characters that
              is "buffer(" followed by optional white space, followed by a
              QName as defined in <bibref ref="XMLNAMES" />, followed by
              optional white space, followed by ")".</p>

            <p>The contents of the specified buffer provides the source for the 
	      external filter. This implies, that this source can be supplied as 
	      an XML event stream to the filter. It is an error, if there's no such 
	      buffer declared at this scope.</p>
          </item>
        </ulist>
        
        <p>It is a static error, if the <att>filter-src</att>
          attribute is present without a <att>filter-method</att> attribute.
          However, the specification of a <att>filter-method</att> attribute
          does not require necessarily a <att>filter-src</att> attribute.</p>

        <note>
          <p>A typical use case would be an embedded XSLT transformation,
            that could be invoked like this:<eg>
<![CDATA[<stx:variable name="xslt" select="'http://www.w3.org/1999/XSL/Transform'" />
<stx:if test="filter-available($xslt)">
  <stx:process-self filter-method="{$xslt}" filter-src="buffer(xslt-code)" />
</stx:if>
<stx:else>
  <stx:message>Cannot invoke an XSLT transformation</stx:message>
</stx:else>]]></eg></p>
          <p>This instruction passes the current node and all its children
            to an XSLT processor, using an XSLT stylesheet that is the
            contents of the buffer named <code>xslt-code</code>.</p>
        </note>

      </div2>
      
      <div2 id="messages">

	<head>Messages</head>

	<p><kw>stx:message</kw></p>

<eg><![CDATA[
<!-- Category: template -->
<stx:message
  select = ]]><nt def="NT-expression">expression</nt><![CDATA[>
  <!-- Content: template -->
</stx:message>
]]></eg>

	<p>The <el>stx:message</el> instruction generates a separate result 
	  stream whose handling is implementation dependent. It can be 
	  directed to a log, or to a special message resolver, etc. However,
	  all instructions of the content of the <el>stx:message</el> element
	  must processed even if the message stream is ignored.
	  Alternatively, the value of the generated message may 
	  be specified in the optional <att>select</att> attribute. It is 
	  a static error if this instruction has a <att>select</att> attribute 
	  and is not empty.</p>

      </div2>

    </div1>

    <div1 id="stxpath-lang">

      <head>STXPath</head>

      <p>STXPath is an expression language for STX; STXPath expressions are 
	embeded in specific attributes of STX instructions and declarations.
	STXPath is similar to <bibref ref='XPath2'/> on the first sight. 
	Syntactically, STXPath is a subset of <bibref ref='XPath2'/>.
	However, as STX has a different notion of context, the meaning of some 
	expressions may be different in STXPath and in XPath 2.0. Consider the 
	following example:</p>

      <p>In XPath 2.0, the expression <code>/node1/node2</code> returns a sequence
	containing all <el>node2</el> elements, whose parent <el>node1</el> is 
	the document element. In STXPath, on contrary, the same expression 
	returns a sequence containing a single node from this node-set; the one 
	which is an ancestor of the current node.</p>

      <p>Each expression has its <kw>static context</kw> - the information that 
	is available during static analysis of the expression, prior to its 
	evaluation. The static context includes in-scope namespaces, default 
	namespace for element names, default function namespace, and in-scope 
	variables. The information that is available at the time when the 
	expression is evaluated is the <kw>current context</kw> as defined in 
	<specref ref='context'/>.</p>

      <p>Each STXPath expressions evaluate always to a sequence (see 
	<xspecref href="&URLXP2;#id-sequence-expressions">XPath 2.0 definition 
	  of a sequence</xspecref>).</p>

      <p>Certain operators, functions, and syntactic constructs expect a value 
	of a particular type to be supplied: this type is referred to as a 
	<emph>required type</emph>. In such an event, a general sequence is 
	converted to the required type according to the <emph>conversion 
	  rules</emph>.</p>

      <p>The empty sequence is converted to required types as defined in the
	following table:</p>

      <table border="1">
	<thead>
	  <tr>
	    <th>required type</th>
	    <th>result</th>
	    </tr>
	</thead>
	<tbody>
	  <tr>
	    <td>boolean</td>
	    <td><code>false</code></td>
	  </tr>
	  <tr>
	    <td>string</td>
	    <td>empty string</td>
	  </tr>
	  <tr>
	    <td>number</td>
	    <td><code>NaN</code></td>
	  </tr>
	  <tr>
	    <td>node</td>
	    <td>ERROR</td>
	  </tr>
	</tbody>
      </table>

      <p>A singleton sequence is converted to a required type according to the 
	type of the only item in the sequence. An attempt to convert boolean,
	string, or number to node causes an error.</p>
      
      <table border="1">
	<thead>
	  <tr>
	    <th>item type</th>
	    <th>boolean required</th>
	    <th>string required</th>
	    <th>number required</th>
	  </tr>
	</thead>
	<tbody>
	  <tr>
	    <td>boolean</td>
	    <td></td>
	    <td><code>false</code> is converted to 'false', <code>true</code> 
	      is converted to 'true'</td>
	    <td><code>false</code> is converted to 0, <code>true</code> is 
	      converted to 1</td>
	  </tr>
	  <tr>
	    <td>string</td>
	    <td>the empty string is converted to 
	      <code>false</code>, other strings are converted to 
	      <code>true</code></td>
	    <td></td>
	    <td>a string that consists of optional whitespace followed by an 
	      optional minus sign followed by a numeric literal (see 
	      <specref ref='literals'/>) followed by whitespace is converted to 
	      the number that is nearest to the mathematical value represented 
	      by the string; any other string is converted to 
	      <code>NaN</code>.</td>
	  </tr>
	  <tr>
	    <td>number</td>
	    <td>0, +0, -0, <code>NaN</code> are converted to 
	      <code>false</code>, other numbers are converted to 
	      <code>true</code></td>
	    <td><code>NaN</code> is converted to 'NaN', +0 and  -0 are 
	      converted to '0', positive infinity is converted to 'Infinity', 
	      negative infinity is converted to '-Infinity'. Other numbers are 
	      represented in decimal form as numeric literal (see 
	      <specref ref='literals'/>) with no leading zeros (apart possibly 
	      from the one required digit immediately before the decimal 
	      point), preceded by a minus sign (-) if the number is 
	      negative.</td>
	    <td></td>
	  </tr>
	  <tr>
	    <td>node</td>
	    <td>a node is converted to <code>true</code></td>
	    <td>a node is converted to its string value (see 
	      <specref ref='expressions'/>)</td>
	    <td>a node is converted to its string value (see 
	      <specref ref='expressions'/>); then the rules to convert strings 
	      to numbers are applied to convert the string value to a 
	      number</td>
	  </tr>
	</tbody>
      </table>

      <p>A sequence containing more than one item is converted according to its 
	very first item; all other items are ignored. The same conversion rules 
	as for singleton sequences are applied (see the table above).</p>

      <p>The following sections discuss basic kinds of expression. Each kind of
	expression has a name such as expression 
	<nt def="prod-PathExpr">PathExpr</nt>, which is introduced on the left 
	side of the grammar production that defines the expression. Each kind of 
	expression is defined in terms of other expressions whose operators have 
	a higher precedence. In this way, the precedence of operators is 
	represented explicitly in the grammar. For the complete grammar, see 
	<specref ref='stxpath'/>.</p>

      <div2 id="literals">

	<head>Literals</head>

	<p>A literal is a direct syntactic representation of an atomic value. 
	  STXPath supports two kinds of literals: <emph>string literals</emph> 
	  and <emph>numeric literals</emph>.</p>

	<p>The value of a string literal is a singleton sequence containing an 
	  item whose atomic type is string and whose value is the string denoted 
	  by the characters between the delimiting quotation marks.</p>

<eg>
  <scrap lang="ebnf">
     <prodrecap ref="prod-StringLiteral"/>
  </scrap>
</eg>

	<p>The value of a numeric literal is a singleton sequence containing an 
	  item whose type is number and whose value is obtained by parsing the 
	  numeric literal according to the rules for string to numbers 
	  conversion (see <specref ref='stxpath-lang'/>).</p>

<eg>
  <scrap lang="ebnf">
     <prodrecap ref="prod-NumericLiteral"/>
     <prodrecap ref="prod-IntegerLiteral"/>
     <prodrecap ref="prod-DecimalLiteral"/>
     <prodrecap ref="prod-DoubleLiteral"/>
  </scrap>
</eg>

      </div2>

      <div2 id="variables">

	<head>Variables</head>

	<p>STX has full-featured re-assignable variables. The STX elements 
	  <el>stx:variable</el> and <el>stx:assign</el> are used to declare 
	  and initialize variables, and to assign new values to them. Visible 
	  variables can be referenced in STXPath expressions.</p>

<eg>
  <scrap lang="ebnf">
     <prodrecap ref="prod-VarRef"/>
  </scrap>
</eg>

	<p>STX variables are scoped statically according to the literal structure 
	  of stx-sheets. The grouping of templates is used to make the sharing 
	  of other than global variables possible.</p>

	<p>There are two types of variables:</p>

	<ulist>

	  <item><p><emph>group variables</emph> - <el>stx:variable</el> is child 
	      of either <el>stx:transform</el> or <el>stx:group</el>. Top-level 
	      variables are considered to be members of the top-most default 
	      group that exists for each stx-sheet.</p></item>

	  <item><p><emph>local variables</emph> - Declared within 
	      templates.</p></item>

	</ulist>

	<p>A group variable is visible for the group where the variable is 
	  declared, for all descendant groups and for all templates belonging to 
	  these groups. A local variable is visible for all following siblings of 
	  the variable declaration and their descendants. Group variables may be 
	  shadowed (another variable with the same name is visible) by descendant 
	  group variables and by local variables. It is a static error to 
	  redeclare a variable with the same name in the same group or 
	  template.</p>

	<p>Variables always contain a sequence. STX instructions 
	  <el>stx:variable</el> and <el>stx:assign</el> are used to evaluate an 
	  expression and store its value into a variable.</p>

	<p>Since variables are re-assignable, each variable must be declared
	  using the <el>stx:variable</el> element before it is used (assigned, 
	  referenced). Group variables are statically initialized while parsing 
	  an stx-sheet. Only the <emph>static context</emph> information is 
	  available during the initialization of group variables. Local variables 
	  are initialized at run-time. A variable declared with no value is 
	  initialized with a singleton sequence containing the empty 
	  string.</p>

	<p><kw>stx:variable</kw></p>

<eg><phrase><![CDATA[
<!-- Category: top-level or group or template -->
<stx:variable
  name = qname
  select = ]]><nt def="NT-expression">expression</nt><![CDATA[
  keep-value = "yes"|"no">
  <!-- Content: text template -->
</stx:variable>
]]></phrase></eg>

	<p>This instruction is used to declare and initialize a variable. The 
	  mandatory <att>name</att> attribute contains the name of
	  the variable. An <nt def="NT-expression">expression</nt> in
	  the <att>select</att> attribute is evaluated and the 
	  variable is initialized with its result. The <att>select</att> 
	  attribute is optional; a variable is initialized with the string 
	  resulting from the content of the <el>stx:variable</el> element if 
	  the <att>select</att> attribute is missing. If the content is empty 
	  (<el>stx:variable</el> element has no children) the variable is 
	  initialized with the empty string.</p>

	<note>
          <p>Thus, <![CDATA[<stx:variable name="var"/>]]> is equal to
	  <![CDATA[<stx:variable name="var" select="''"/>]]>.</p>
        </note>

	<p>It is a static error if the element <el>stx:variable</el> declaring 
	  a <emph>group variable</emph> contains an 
	  <el>stx:process-children</el>, <el>stx:process-self</el>,
	  <el>stx:process-siblings</el>, <el>stx:process-attributes</el>,
	  <el>stx:process-document</el>, <el>stx:process-buffer</el>, or
	  <el>call-procedure</el> instruction in its content.</p>

	<p>The optional <att>keep-value</att> attribute specifies whether a
          new instance of the variable created by instantiating a template
	  having its <att>new-scope</att> attribute set to
          <attval>yes</attval> is initialized with the value of 
	  the shadowed variable (<code>yes</code>) or not
	  (<code>no</code>). This attribute is allowed only for group
	  variables. The default value is <code>no</code>. If there is no 
	  shadowed variable (a variable with the same name declared in 
	  an ancestor group) yet, the <att>keep-value</att> attribute is 
	  ignored.</p>

	<p><kw>stx:assign</kw></p>

<eg><phrase><![CDATA[
<!-- Category: top-level or group or template -->
<stx:assign
  name = qname
  select = ]]><nt def="NT-expression">expression</nt><![CDATA[>
  <!-- Content: text template -->
</stx:assign>
]]></phrase></eg>

	<p>This instruction is used to assign a new value to a previously 
	  declared variable. The mandatory <att>name</att> attribute contains the 
	  name of the variable. The <nt def="NT-expression">expression</nt> in 
	  the optional <att>select</att> attribute is evaluated and its result is 
	  assigned to the variable. The string resulting from the content of the 
	  <el>stx:assign</el> element is assigned to the variable if the 
	  <att>select</att> attribute is missing. If the content is empty, the empty 
	  string is assigned to the variable.</p>

	<note>
          <p>Thus, <![CDATA[<stx:assign name="var"/>]]> is equal to
	  <![CDATA[<stx:assign name="var" select="''"/>]]>.</p>
        </note>

      </div2>

      <div2 id="parenthesized-expressions">

	<head>Parenthesized Expressions</head>

	<p>Parentheses may be used to enforce a particular evaluation order in 
	  expressions that contain multiple operators.</p>

	<p>Parentheses are also used as delimiters in constructing a sequence, 
	  as described in <specref ref='sequence-expressions'/>.</p>

<eg>
  <scrap lang="ebnf">
     <prodrecap ref="prod-ParenthesizedExpr"/>
  </scrap>
</eg>
	<p>Parenthesized Expressions in STXPath are equivalent to
	  <xspecref href="&URLXP2;#id-paren-expressions">Parenthesized 
	    Expressions</xspecref> in <bibref ref="XPath2"/>.</p>

      </div2>

      <div2 id="context-item-expression">

	<head>Context Item Expression</head>

	<p>The context item expression evaluates to the current node.</p>

<eg>
  <scrap lang="ebnf">
     <prodrecap ref="prod-ContextItemExpr"/>
  </scrap>
</eg>

      </div2>

      <div2 id="functions">

	<head>Functions</head>

	<p>A <nt def="prod-FunctionCall">function call</nt> consists of a function 
	  <xnt href="&URLXMLNAMES;#NT-QName">qualified name</xnt> followed by 
	  a parenthesized list 
	  of zero or more <nt def="NT-expression">expressions</nt>. The 
	  expressions inside the parentheses provide arguments of the 
	  function call. The number of arguments must be equal to the number of 
	  function parameters; otherwise a static error is raised.</p>

<eg>
  <scrap lang="ebnf">
     <prodrecap ref="prod-FunctionCall"/>
  </scrap>
</eg>

	<p>A function call is evaluated as follows:</p>

	<olist>

	  <item><p>Each argument expression is evaluated, producing an argument 
	      value (sequence).</p></item>

	  <item><p>If the corresponding function parameter has a required 
	      type, the argument value is converted to this type.</p></item>

	  <item><p>The function is executed using the converted argument values. 
	      The result is a value of the function's declared return 
	      type.</p></item>

	</olist>

	<p>STXPath function names are contained in the reserved namespace
	  <code>&STXFNS;</code>. The 
	  <code>sf:</code> prefix is used to refer to this namespace in this
	  document. The default function namespace is assigned to this 
	  reserved namespace in STX. Thus, the functions namespace does not 
	  need to be declared in stx-sheets and STXPath functions can be 
	  invoked without any namespace prefix.</p>

	<p>Some STXPath functions have the same definitions as their 
	  counterparts (functions with the same local name) in XPath 2.0. These
	  functions are not re-defined in this section. Instead, original 
	  definitions in <bibref ref="XQXPFO"/> are 
	  referenced. Other STXPath functions are either different from their 
	  XPath 2.0 counterparts or have no such counterparts; these functions
	  are defined in this section.</p>

	<div3 id="sequence-functions">

 	  <head>Sequence Functions</head>

	  <!-- ***** empty ***** -->
	  <proto name="sf:empty" return-type="boolean">
	    <arg type="sequence" occur="req"/>
	  </proto>

	  <p>Indicates whether or not the provided sequence is empty.</p>

	  <p>See the <xspecref href="&URLFO;#func-empty">definition</xspecref> 
	    in <bibref ref="XQXPFO"/>.</p>

	  <!-- ***** exists ***** -->
	  <proto name="sf:exists" return-type="boolean">
	    <arg type="sequence" occur="req"/>
	  </proto>

	  <p>Indicates whether or not the provided sequence is not empty.</p>

	  <p>See the <xspecref href="&URLFO;#func-exists">definition</xspecref> 
	    in <bibref ref="XQXPFO"/>.</p>

	  <!-- ***** item-at ***** -->
	  <proto name="sf:item-at" return-type="item">
	    <arg type="sequence" occur="req"/>
	    <arg type="number" occur="req"/>
	  </proto>

	  <p>Returns the item at the given index. The first position is 1.</p>
          <!--
	  <p>See the <xspecref href="&URLFO;#func-item-at">definition</xspecref> 
	    in <bibref ref="XQXPFO"/>.</p>
          -->

	  <!-- ***** index-of ***** -->
	  <proto name="sf:index-of" return-type="number">
	    <arg type="sequence" occur="req"/>
	    <arg type="item" occur="req"/>
	  </proto>

	  <p>Returns a sequence of integer numbers, each of which is the index 
	    of a member of the specified sequence that is equal to the item that 
	    is the value of the second argument.</p>

	  <p>See the <xspecref href="&URLFO;#func-index-of">definition</xspecref> 
	    in <bibref ref="XQXPFO"/>.</p>

	  <!-- ***** subsequence ***** -->
	  <proto name="sf:subsequence" return-type="sequence">
	    <arg type="sequence" occur="req"/>
	    <arg type="number" occur="req"/>
	    <arg type="number" occur="opt"/>
	  </proto>

	  <p>Returns the subsequence of a given sequence identified by 
	    location.</p>

	  <p>See the <xspecref href="&URLFO;#func-subsequence">definition</xspecref> 
	    in <bibref ref="XQXPFO"/>.</p>

	  <!-- ***** insert-before ***** -->
	  <proto name="sf:insert-before" return-type="sequence">
	    <arg type="sequence" occur="req"/>
	    <arg type="number" occur="req"/>
	    <arg type="sequence" occur="req"/>
	  </proto>

	  <p>Inserts an item or sequence of items into a specified position 
	    of a sequence.</p>

	  <p>See the <xspecref href="&URLFO;#func-insert-before">definition</xspecref> 
	    in <bibref ref="XQXPFO"/>.</p>

	  <!-- ***** remove ***** -->
	  <proto name="sf:remove" return-type="sequence">
	    <arg type="sequence" occur="req"/>
	    <arg type="number" occur="req"/>
	  </proto>

	  <p>Removes an item from a specified position of a sequence.</p>

	  <p>See the <xspecref href="&URLFO;#func-remove">definition</xspecref> 
	    in <bibref ref="XQXPFO"/>.</p>

	</div3>

	<div3 id="node-functions">

	  <head>Node Functions</head>

	  <!-- ***** name ***** -->
	  <proto name="sf:name" return-type="string">
	    <arg type="node" occur="opt"/>
	  </proto>

	  <p>Returns the name of the current node or the specified node.</p>

	  <p>See the <xspecref href="&URLFO;#func-xpath-name">definition</xspecref> 
	    in <bibref ref="XQXPFO"/>.</p>

	  <!-- ***** namespace-uri ***** -->
	  <proto name="sf:namespace-uri" return-type="string">
	    <arg type="node" occur="opt"/>
	  </proto>

	  <p>Returns the namespace URI for the QName of the argument 
	    node or the current node if the argument is omitted.</p>

	  <p>See the <xspecref href="&URLFO;#func-namespace-uri">definition</xspecref>
	    in <bibref ref="XQXPFO"/>.</p>

	  <!-- ***** local-name ***** -->
	  <proto name="sf:local-name" return-type="string">
	    <arg type="node" occur="opt"/>
	  </proto>

	  <p>Returns the local name of the current node or the specified node.</p>
	  
	  <p>See the <xspecref href="&URLFO;#func-local-name">definition</xspecref> 
	    in <bibref ref="XQXPFO"/>.</p>

	  <!-- ***** position ***** -->
	  <proto name="sf:position" return-type="number">
	  </proto>

	  <p>The <function>sf:position</function> function returns a number equal 
	    to the position of the current node relative to other siblings. 
	    See <specref ref='context'/> for details of 
	    <function>sf:position()</function> semantics.</p>

	  <!-- ***** has-child-nodes ***** -->
	  <proto name="sf:has-child-nodes" return-type="boolean">
	  </proto>

	  <p>The <function>sf:has-child-nodes</function> function returns true 
	    if and only if the current node is the document node or an element 
	    node and has child nodes (it is not empty). It returns false 
	    otherwise.</p>

	  <!-- ***** node-kind ***** -->
	  <proto name="sf:node-kind" return-type="string">
	    <arg type="node" occur="req"/>
	  </proto>

	  <p>The <function>sf:node-kind</function> function returns a string 
	    value representing the node's kind: either "document", "element", 
	    "attribute", "text", "cdata", "processing-instruction", or 
	    "comment".</p>

	  <!-- ***** get-in-scope-prefixes ***** -->
	  <proto name="sf:get-in-scope-prefixes" return-type="sequence">
	    <arg type="node" occur="req"/>
	  </proto>

	  <p>Returns the prefixes of the in-scope namespaces for the given 
	    element.</p>

	  <p>See the <xspecref href="&URLFO;#func-get-in-scope-prefixes">definition</xspecref>
	    in <bibref ref="XQXPFO"/>.</p>

	  <!-- ***** get-namespace-uri-for-prefix ***** -->
	  <proto name="sf:get-namespace-uri-for-prefix" return-type="string">
	    <arg type="string" occur="req"/>
	    <arg type="node" occur="req"/>
	  </proto>

	  <p>Returns a namespace URI of one of the in-scope namespaces for 
	    the given element, identified by its namespace prefix.</p>

	  <p>See the <xspecref href="&URLFO;#func-get-namespace-uri-for-prefix">definition</xspecref>
	    in <bibref ref="XQXPFO"/>.</p>

	  <!-- ***** lang ***** -->
	  <proto name="sf:lang" return-type="boolean">
	    <arg type="string" occur="req"/>
	  </proto>

	  <p>Returns true or false depending on whether the language of the 
	    current node, as defined using the xml:lang attribute, is the same 
	    as, or a sub-language of, the language specified by the argument.</p>

	  <p>See the <xspecref href="&URLFO;#func-lang">definition</xspecref>
	    in <bibref ref="XQXPFO"/>.</p>

	</div3>

	<div3 id="boolean-functions">

	  <head>Boolean Functions</head>

	  <!-- ***** true ***** -->
	  <proto name="sf:true" return-type="boolean">
	  </proto>

	  <p>Returns the boolean value TRUE.</p>

	  <p>See the <xspecref href="&URLFO;#func-true">definition</xspecref> 
	    in <bibref ref="XQXPFO"/>.</p>

	  <!-- ***** false ***** -->
	  <proto name="sf:false" return-type="boolean">
	  </proto>

	  <p>Returns the boolean value FALSE.</p>

	  <p>See the <xspecref href="&URLFO;#func-false">definition</xspecref> 
	    in <bibref ref="XQXPFO"/>.</p>

	  <!-- ***** not ***** -->
	  <proto name="sf:not" return-type="boolean">
	    <arg type="sequence" occur="req"/>
	  </proto>

	  <p>Inverts the boolean value of the argument.</p>

	  <p>See the <xspecref href="&URLFO;#func-not">definition</xspecref> 
	    in <bibref ref="XQXPFO"/>.</p>

	</div3>

	<div3 id="string-functions">

	  <head>String Functions</head>

	  <!-- ***** concat ***** -->
	  <proto name="sf:concat" return-type="string">
	    <arg type="string" occur="req"/>
	    <arg type="string" occur="req"/>, ...
	  </proto>

	  <p>Concatenates two or more character strings.</p>

	  <p>See the <xspecref href="&URLFO;#func-concat">definition</xspecref>
	    in <bibref ref="XQXPFO"/>.</p>

	  <!-- ***** string-join ***** -->
	  <proto name="sf:string-join" return-type="string">
	    <arg type="sequence" occur="req"/>
	    <arg type="string" occur="req"/>
	  </proto>

	  <p>Accepts a sequence of strings and returns the strings concatenated 
	    together with an optional separator.</p>

	  <p>See the <xspecref href="&URLFO;#func-string-join">definition</xspecref>
	    in <bibref ref="XQXPFO"/>.</p>

	  <!-- ***** starts-with ***** -->
	  <proto name="sf:starts-with" return-type="boolean">
	    <arg type="string" occur="req"/>
	    <arg type="string" occur="req"/>
	  </proto>
	  
	  <p>Indicates whether the value of one string begins with characters 
	    of the value of another string.</p>

	  <p>See the <xspecref href="&URLFO;#func-starts-with">definition</xspecref> 
	    in <bibref ref="XQXPFO"/>.</p>

	  <!-- ***** ends-with ***** -->
	  <proto name="sf:ends-with" return-type="boolean">
	    <arg type="string" occur="req"/>
	    <arg type="string" occur="req"/>
	  </proto>
	  
	  <p>Indicates whether the value of one string ends with characters 
	    of the value of another string.</p>

	  <p>See the <xspecref href="&URLFO;#func-ends-with">definition</xspecref> 
	    in <bibref ref="XQXPFO"/>.</p>

	  <!-- ***** contains ***** -->
	  <proto name="sf:contains" return-type="boolean">
	    <arg type="string" occur="req"/>
	    <arg type="string" occur="req"/>
	  </proto>
	  
	  <p>Indicates whether the value of one string contains characters 
	    of the value of another string.</p>

	  <p>See the <xspecref href="&URLFO;#func-contains">definition</xspecref> 
	    in <bibref ref="XQXPFO"/>.</p>

	  <!-- ***** substring ***** -->
	  <proto name="sf:substring" return-type="string">
	    <arg type="string" occur="req"/>
	    <arg type="number" occur="req"/>
	    <arg type="number" occur="opt"/>
	  </proto>

	  <p>Returns a string located at a specified place in the value of 
	    a string.</p>
	  
	  <p>See the <xspecref href="&URLFO;#func-substring">definition</xspecref> 
	    in <bibref ref="XQXPFO"/>.</p>

	  <!-- ***** substring-before ***** -->
	  <proto name="sf:substring-before" return-type="string">
	    <arg type="string" occur="req"/>
	    <arg type="string" occur="req"/>
	  </proto>

	  <p>Returns characters of one string that precede in that string 
	    the characters in the value of another string.</p>

	  <p>See the <xspecref href="&URLFO;#func-substring-before">definition</xspecref> 
	    in <bibref ref="XQXPFO"/>.</p>

	  <!-- ***** substring-after ***** -->
	  <proto name="sf:substring-after" return-type="string">
	    <arg type="string" occur="req"/>
	    <arg type="string" occur="req"/>
	  </proto>

	  <p>Returns characters of one string that succeed in that string 
	    the characters in the value of another string.</p>

	  <p>See the <xspecref href="&URLFO;#func-substring-after">definition</xspecref> 
	    in <bibref ref="XQXPFO"/>.</p>

	  <!-- ***** string-length ***** -->
	  <proto name="sf:string-length" return-type="number">
	    <arg type="string" occur="req"/>
	  </proto>

	  <p>Returns the length of the argument.</p>

	  <p>See the <xspecref href="&URLFO;#func-string-length">definition</xspecref>
	    in <bibref ref="XQXPFO"/>.</p>

	  <!-- ***** normalize-space ***** -->
	  <proto name="sf:normalize-space" return-type="string">
	    <arg type="string" occur="req"/>
	  </proto>

	  <p>Returns the whitespace-normalized value of the argument.</p>

	  <p>See the <xspecref href="&URLFO;#func-normalize-space">definition</xspecref>
	    in <bibref ref="XQXPFO"/>.</p>

	  <!-- ***** normalize-unicode ***** -->
	  <proto name="sf:normalize-unicode" return-type="string">
	    <arg type="string" occur="req"/>
	    <arg type="string" occur="opt"/>
	  </proto>

	  <p>Returns the normalized value of the first argument in the 
	    normalization form specified by the second argument.</p>

	  <p>See the <xspecref href="&URLFO;#func-normalize-unicode">definition</xspecref>
	    in <bibref ref="XQXPFO"/>.</p>

	  <!-- ***** upper-case ***** -->
	  <proto name="sf:upper-case" return-type="string">
	    <arg type="string" occur="req"/>
	  </proto>

	  <p>Returns the upper-cased value of the argument.</p>

	  <p>See the <xspecref href="&URLFO;#func-upper-case">definition</xspecref>
	    in <bibref ref="XQXPFO"/>.</p>

	  <!-- ***** lower-case ***** -->
	  <proto name="sf:lower-case" return-type="string">
	    <arg type="string" occur="req"/>
	  </proto>

	  <p>Returns the lower-cased value of the argument.</p>

	  <p>See the <xspecref href="&URLFO;#func-lower-case">definition</xspecref>
	    in <bibref ref="XQXPFO"/>.</p>

	  <!-- ***** translate ***** -->
	  <proto name="sf:translate" return-type="string">
	    <arg type="string" occur="req"/>
	    <arg type="string" occur="req"/>
	    <arg type="string" occur="req"/>
	  </proto>

	  <p>Returns the first argument string with occurrences of characters 
	    in the second argument replaced by the character at the corresponding 
	    position in the third string.</p>

	  <p>See the <xspecref href="&URLFO;#func-translate">definition</xspecref>
	    in <bibref ref="XQXPFO"/>.</p>

	  <!-- ***** string-pad ***** -->
	  <proto name="sf:string-pad" return-type="string">
	    <arg type="string" occur="req"/>
	    <arg type="number" occur="req"/>
	  </proto>

	  <p>Returns a string composed of as many copies of its first argument 
	    as specified in its second argument.</p>
<!--
	  <p>See the <xspecref href="&URLFO;#func-string-pad">definition</xspecref>
	    in <bibref ref="XQXPFO"/>.</p>
-->
          
	  <!-- ***** matches ***** -->
	  <proto name="sf:matches" return-type="boolean">
	    <arg type="string" occur="req"/>
	    <arg type="string" occur="req"/>
	    <arg type="string" occur="opt"/>
	  </proto>

	  <p>Returns a boolean value that indicates whether the value of the 
	    first argument is matched by the regular expression that is the value 
	    of the second argument, using the flags in the optional third
            argument.</p>

	  <p>See the <xspecref href="&URLFO;#func-matches">definition</xspecref>
	    in <bibref ref="XQXPFO"/>.</p>

	  <!-- ***** replace ***** -->
	  <proto name="sf:replace" return-type="string">
	    <arg type="string" occur="req"/>
	    <arg type="string" occur="req"/>
	    <arg type="string" occur="req"/>
	    <arg type="string" occur="opt"/>
	  </proto>

	  <p>Returns the value of the first argument with every substring matched 
	    by the regular expression that is the value of the second argument 
	    replaced by the replacement string that is the value of the third 
	    argument, using the flags in the optional fourth argument.</p>

	  <p>See the <xspecref href="&URLFO;#func-replace">definition</xspecref>
	    in <bibref ref="XQXPFO"/>.</p>
          
	  <!-- ***** tokenize ***** -->
	  <proto name="sf:tokenize" return-type="sequence">
	    <arg type="string" occur="req"/>
	    <arg type="string" occur="req"/>
	    <arg type="string" occur="opt"/>
	  </proto>

	  <p>Returns a sequence of zero or more strings whose values are 
	    substrings of the value of the first argument separated by substrings 
	    that match the regular expression that is the value of the second 
	    argument, using the flags in the optional third argument.</p>

	  <p>See the <xspecref href="&URLFO;#func-tokenize">definition</xspecref>
	    in <bibref ref="XQXPFO"/>.</p>

	  <!-- ***** escape-uri ***** -->
	  <proto name="sf:escape-uri" return-type="string">
	    <arg type="string" occur="req"/>
	    <arg type="boolean" occur="req"/>
	  </proto>

	  <p>Returns the string representing a URI value with certain characters 
	    escaped.</p>

	  <p>See the <xspecref href="&URLFO;#func-escape-uri">definition</xspecref>
	    in <bibref ref="XQXPFO"/>.</p>

	</div3>

	<div3 id="numerical-functions">

	  <head>Numerical Functions</head>

	  <!-- ***** floor ***** -->
	  <proto name="sf:floor" return-type="number">
	    <arg type="number" occur="req"/>
	  </proto>

	  <p>Returns the largest integer less than or equal to the argument.</p>
	  
	  <p>See the <xspecref href="&URLFO;#func-floor">definition</xspecref>
	    in <bibref ref="XQXPFO"/>.</p>

	  <!-- ***** ceiling ***** -->
	  <proto name="sf:ceiling" return-type="number">
	    <arg type="number" occur="req"/>
	  </proto>

	  <p>Returns the smallest integer greater than or equal to the argument.</p>

	  <p>See the <xspecref href="&URLFO;#func-ceiling">definition</xspecref>
	    in <bibref ref="XQXPFO"/>.</p>
	  
	  <!-- ***** round ***** -->
	  <proto name="sf:round" return-type="number">
	    <arg type="number" occur="req"/>
	  </proto>

	  <p>Rounds to the nearest integer.</p>

	  <p>See the <xspecref href="&URLFO;#func-round">definition</xspecref>
	    in <bibref ref="XQXPFO"/>.</p>

	</div3>

	<div3 id="aggregate-functions">

	  <head>Aggregate Functions</head>

	  <!-- ***** count ***** -->
	  <proto name="sf:count" return-type="number">
	    <arg type="sequence" occur="req"/>
	  </proto>

	  <p>Returns the number of items in the sequence.</p>

	  <p>See the <xspecref href="&URLFO;#func-count">definition</xspecref> 
	    in <bibref ref="XQXPFO"/>.</p>

	  <!-- ***** sum ***** -->
	  <proto name="sf:sum" return-type="number">
	    <arg type="sequence" occur="req"/>
	  </proto>

	  <p>The <function>sf:sum</function> function returns the sum, for each 
	    item in the argument sequence, of the result of converting the item 
	    to a number. If the argument is the empty sequence, 
	    the function returns zero. </p>

	  <!-- ***** avg ***** -->
	  <proto name="sf:avg" return-type="number">
	    <arg type="sequence" occur="req"/>
	  </proto>

	  <p>The <function>sf:avg</function> function returns the average of all 
	    items in the argument sequence converted to numbers. If the argument
	    is the empty sequence, the empty sequence is returned. </p>

	  <!-- ***** max ***** -->
	  <proto name="sf:max" return-type="number">
	    <arg type="sequence" occur="req"/>
	  </proto>

	  <p>The <function>sf:max</function> function converts all items of the 
	    argument sequence to numbers and returns the item whose value is greater 
	    than or equal to the value of every other item in the argument sequence. 
            If the argument contains the value NaN, the value NaN is returned.
            If the argument is the empty sequence, the empty sequence is
            returned.</p>

	  <!-- ***** min ***** -->
	  <proto name="sf:min" return-type="number">
	    <arg type="sequence" occur="req"/>
	  </proto>

	  <p>The <function>sf:min</function> function converts all items of the 
	    argument sequence to numbers and returns the item whose value is less 
	    than or equal to the value of every other item in the argument sequence.
            If the argument contains the value NaN, the value NaN is returned.
            If the argument is the empty sequence, the empty sequence is
            returned.</p>

	</div3>

	<div3 id="conversion-functions">

	  <head>Conversion Functions</head>

	  <!-- ***** string ***** -->
	  <proto name="sf:string" return-type="string">
	    <arg type="sequence" occur="req"/>
	  </proto>

	  <p>The <function>sf:string</function> function returns the result of 
	    converting the argument to a string. See 
	    <specref ref="stxpath-lang"/> for details.</p>

	  <!-- ***** number ***** -->
	  <proto name="sf:number" return-type="number">
	    <arg type="sequence" occur="req"/>
	  </proto>

	  <p>The <function>sf:number</function> function returns the result of 
	    converting the argument to a number. See 
	    <specref ref="stxpath-lang"/> for details.</p>

	  <!-- ***** boolean ***** -->
	  <proto name="sf:boolean" return-type="boolean">
	    <arg type="sequence" occur="req"/>
	  </proto>

	  <p>The <function>sf:boolean</function> function returns the result of 
	    converting the argument to a boolean. See 
	    <specref ref="stxpath-lang"/> for details.</p>

	</div3>

        <div3 id="other-functions">
          
          <head>Other Functions</head>
	  <!-- ***** filter-available ***** -->
	  <proto name="sf:filter-available" return-type="boolean">
	    <arg type="string" occur="req"/>
	  </proto>

	  <p>The <function>sf:filter-available</function> function returns
            <code>true</code> if the STX processor supports the external
            filter identified by its argument, and <code>false</code>
            otherwise (see <specref ref="external-filters" />).</p>
          
        </div3>
      </div2>

      <div2 id="path-expressions">

	<head>Path Expressions</head>

	<p>A <nt def="prod-PathExpr">path expression</nt> can be used to locate 
	  nodes within the ancestor stack. It evaluates to a sequence of nodes 
	  from the stack. A path expression consists of a series of one or more 
	  <nt def="prod-AxisStep">axis steps</nt>, separated by <code>/</code> or 
	  <code>//</code>, and optionally beginning with <code>/</code> or 
	  <code>//</code>.</p>

	<p>This sequence of axis steps is evaluated from left to right. Each 
	  operation <code>E1/E2</code> is evaluated as follows: Expression E1 is 
	  evaluated into a sequence of nodes. Each node resulting from the 
	  evaluation of E1 then serves to provide an inner context for
	  evaluation of E2. Each evaluation of E2 results in a (possibly empty) 
	  sequence of nodes. The sequences of nodes resulting from all the 
	  evaluations of E2 are combined, eliminating duplicate nodes based on 
	  node identity and sorting the result in document order.</p>

	<p>A <code>/</code> at the beginning of a path expression is an 
	  abbreviation for the initial step. The effect of this initial step is 
	  to begin the path at the root node of the current ancestor stack.</p>

	<p>A <code>//</code> at the beginning of a path expression is an 
	  abbreviation for the initial steps. The effect of these initial steps 
	  is to establish an initial node sequence that contains the root of the 
	  current ancestor stack, plus all nodes descended from this root. This 
	  node sequence is used as an input to subsequent steps in the path 
	  expression.</p>

	<p>An axis step generates a sequence of nodes from the ancestor stack and 
	  then filters the sequence using up to one predicate. The value of the 
	  step consists of those nodes that satisfy the predicate. Predicates are 
	  described in <specref ref="predicate" />. Nodes are always returned 
	  in the document order. An axis step may be either an 
	  <nt def="prod-AbbrevForwardStep">abbreviated forward step</nt> or an 
	  <nt def="prod-AbbrevReverseStep">abbreviated reverse step</nt>, 
	  followed by no or one predicate. An axis step might be thought of as 
	  beginning at the context node and navigating to those nodes that are 
	  reachable from the context node via a specified axis. STXPath has 
	  abbreviated syntax only. The result of an abbreviated forward step 
	  consists of the nodes reachable from the context node via the specified 
	  axis (either child or attribute) that have the 
	  <nt def="prod-KindTest">node kind</nt> or 
	  <nt def="prod-NameTest">name</nt> specified by 
	  a <nt def="prod-NodeTest">node test</nt>.</p>

	<p>In addition to common names (<nt def="prod-QName">QNames</nt> and 
	  <nt def="prod-Wildcard">wildcards</nt>) and kind tests known from
	  <bibref ref="XPath2"/>, STXPath has two more kind tests:</p>

	<ulis