<?xml version="1.0" encoding="UTF-8"?>
<!--
<?publication-root http://www.w3.org/XML/XProc/docs/?>
<?latest-version http://www.w3.org/XML/XProc/docs/langspec.html?>
-->
<?xml-stylesheet type="text/css" href="specification.css"?>
<?oxygen RNGSchema="../schema/dbspec.rnc" type="compact"?>
<?xml-stylesheet type="text/xsl" href="../style/dbspec.xsl"?>
<!DOCTYPE specification [
<!ENTITY qn "<glossterm>QName</glossterm>">
<!ENTITY must "<rfc2119>must</rfc2119>">
<!ENTITY xml "<glossterm>XML</glossterm>">
]>
<specification xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:cs="http://www.w3.org/XML/XProc/2006/04/components#" xmlns:e="http://www.w3.org/1999/XSL/Spec/ElementSyntax" class="wd" version="5.0-extension w3c-xproc">
<info>
<title>XProc: An XML Pipeline Language</title>
<w3c-shortname>xproc</w3c-shortname>
<pubdate>2008-05-01</pubdate><!-- Viva la revolucion! -->

<bibliorelation type="isformatof" xlink:href="langspec.xml">XML</bibliorelation>
<bibliorelation type="isformatof" xlink:href="diff.html">Revision markup</bibliorelation>

<bibliorelation type="replaces" xlink:href="http://www.w3.org/TR/2007/WD-xproc-20071129/"/>
<bibliorelation type="replaces" xlink:href="http://www.w3.org/TR/2007/WD-xproc-20070920/"/>
<bibliorelation type="replaces" xlink:href="http://www.w3.org/TR/2007/WD-xproc-20070706/"/>
<bibliorelation type="replaces" xlink:href="http://www.w3.org/TR/2007/WD-xproc-20070405/"/>
<!--
<bibliorelation type="replaces"
		xlink:href="http://www.w3.org/TR/2006/WD-xproc-20061117/"/>
-->

<authorgroup>
  <author>
    <personname>Norman Walsh</personname>
    <affiliation>
      <orgname>Sun Microsystems, Inc.</orgname>
    </affiliation>
    <email>Norman.Walsh@Sun.COM</email>
  </author>
  <author>
    <personname>Alex Milowski</personname>
    <affiliation>
      <orgname>Invited expert</orgname>
    </affiliation>
    <email>alex@milowski.org</email>
  </author>
  <author>
    <personname>Henry S. Thompson</personname>
    <affiliation>
      <orgname>University of Edinburgh</orgname>
    </affiliation>
    <email>ht@inf.ed.ac.uk</email>
  </author>
</authorgroup>

<abstract>
<para>This specification describes the syntax and semantics of
<citetitle>XProc: An XML Pipeline Language</citetitle>, a language
for describing operations to be performed on XML documents.
</para>

<para>An XML Pipeline specifies a sequence of operations to be
performed on zero or more XML documents.
Pipelines generally accept zero or more XML documents as input and
produce zero or more XML documents as output.  Pipelines are made up
of simple steps which perform atomic operations on XML documents
and constructs similar to conditionals, iteration, and exception
handlers which control which steps are executed.</para>
</abstract>

<legalnotice role="status">

<para><emphasis>This section describes the status of this document at
the time of its publication. Other documents may supersede this
document. A list of current W3C publications and the latest revision
of this technical report can be found in the <link xlink:href="http://www.w3.org/TR/">W3C technical reports index</link>
at http://www.w3.org/TR/.</emphasis></para>

<para>This document was produced by the
<link xlink:href="http://www.w3.org/XML/Processing/">XML Processing Model Working Group</link>
which is part of the
<link xlink:href="http://www.w3.org/XML/Activity">XML Activity</link>.
Publication as a Working Draft does not imply endorsement
by the W3C Membership. This is a draft document and may be updated,
replaced or obsoleted by other documents at any time. It is
inappropriate to cite this document as other than work in progress.
</para>

<!--
<para>This is a Last Call Working Draft. The Working Group considers
this specification complete and finished. The review period for this document
ends on 24 October 2007.</para>
-->

<para>Since the last public working draft, the Working Group has considered
<link xlink:href="http://www.w3.org/XML/XProc/2007/09/lastcall/">several hundred
comments</link> in nearly 150 threads. We've responded to many of these
by changing the specification. Some of the significant changes in this
draft are:</para>

<orderedlist>
<listitem>
<para>Removed implicit pipeline inputs and outputs. See
<xref linkend="primary-input-output"/>.
</para>
</listitem>
<listitem>
<para>Support either XPath 1.0 or XPath 2.0 as the pipeline expression language.
</para>
</listitem>
<listitem>
<para>Support both XSLT 1.0 and XSLT 2.0 on a single <tag>p:xslt</tag> step.
</para>
</listitem>
<listitem>
<para>Added <tag>p:hash</tag>, <tag>p:uuid</tag>,
<tag>p:www-form-urldecode</tag>, and <tag>p:www-form-urlencode</tag>.
</para>
</listitem>
<listitem>
<para>Added <xref linkend="security-considerations"/>.
</para>
</listitem>
<listitem>
<para>Added a <literal>p:language</literal> system property.
</para>
</listitem>
<listitem>
<para>Added <option>fixup-xml-base</option> and <option>fixup-xml-lang</option>
options to <tag>p:xinclude</tag>.
</para>
</listitem>
<listitem>
<para>Renamed <literal>c:http-request</literal> and
<literal>c:http-response</literal> to simply
<tag>c:request</tag> and <tag>c:response</tag>.
</para>
</listitem>
<listitem>
<para>Added <tag class="attribute">psvi-required</tag> attribute to
pipelines.
</para>
</listitem>
<listitem>
<para>Fairly substantial syntax changes. A <tag>p:pipeline</tag> is now
just syntactic sugar for a particular <tag>p:declare-step</tag>.
</para>
</listitem>
<listitem>
<para>Changed definition of <tag>p:error</tag> to better address localization
issues.
</para>
</listitem>
<listitem>
<para>Significantly reworked the syntax and semantics of variables, options,
and parameters. Added <tag>p:variable</tag>. Imposed a syntactic distinction
between declaration (<tag>p:option</tag>) and use
(<tag>p:with-option</tag>/<tag>p:with-param</tag>) of options and parameters.
</para>
</listitem>
<listitem>
<para>Clarified the scope of variables and options.
</para>
</listitem>
<listitem>
<para>Removed <tag class="attribute">value</tag> attribute from 
<tag>p:variable</tag>, <tag>p:option</tag>,
<tag>p:with-option</tag>, and <tag>p:with-param</tag>.
</para>
</listitem>
<listitem>
<para>Removed automatic declaration of parameter input ports.
</para>
</listitem>
<listitem>
<para>Added <function>p:base-uri</function> and <function>p:resolve-uri</function>
functions to support (XPath 1.0) pipelines that need access to the base URI
of documents.
</para>
</listitem>
<listitem>
<para>Removed ignored namespaces, added <tag>p:pipeinfo</tag>.
</para>
</listitem>
<listitem>
<para>Redefined the <tag>p:label-elements</tag> step to use a step-local
variable in the XPath context.
</para>
</listitem>
</orderedlist>

<para>Please send comments about this document to
<link xlink:href="mailto:public-xml-processing-model-comments@w3.org">public-xml-processing-model-comments@w3.org</link> (public
<link xlink:href="http://lists.w3.org/Archives/Public/public-xml-processing-model-comments/">archives</link> are available).</para>

<para>This document was produced by a group operating under the
<link xlink:href="http://www.w3.org/Consortium/Patent-Policy-20040205/">5 February 2004 W3C Patent Policy</link>.
W3C maintains a
<link xlink:href="http://www.w3.org/2004/01/pp-impl/38398/status">public list of any patent disclosures</link>
made in connection with the deliverables of the group; that page also
includes instructions for disclosing a patent. An individual who has
actual knowledge of a patent which the individual believes contains
<link xlink:href="http://www.w3.org/Consortium/Patent-Policy-20040205/#def-essential">Essential Claim(s)</link>
must disclose the information in accordance with
<link xlink:href="http://www.w3.org/Consortium/Patent-Policy-20040205/#sec-Disclosure">section 6 of the W3C Patent Policy</link>.
</para>

</legalnotice>
</info>

<section xml:id="introduction">
<title>Introduction</title>

<para>An XML Pipeline specifies a sequence of operations to be
performed on a collection of XML input documents. Pipelines take zero
or more XML documents as their input and produce zero or more XML
documents as their output.</para>

<para>A <glossterm>pipeline</glossterm> consists of steps. Like pipelines, steps take zero or
more XML documents as their inputs and produce zero or more XML
documents as their outputs. The inputs of a step come from the web,
from the pipeline document, from the inputs to the pipeline itself, or
from the outputs of other steps in the pipeline. The outputs from a
step are consumed by other steps, are outputs of the pipeline as a
whole, or are discarded.</para>

<para>There are two kinds of steps: atomic steps and compound
steps. Atomic steps carry out single operations and have no
substructure as far as the pipeline is concerned, whereas compound steps
control the execution of other steps, which they include in the form
of one or more subpipelines.</para>

<para>This specification defines a standard library,
<xref linkend="std-components"/>, of steps.
Pipeline implementations <rfc2119>may</rfc2119> support additional types of
steps as well.
</para>

<para><xref linkend="fig-xival"/> is a graphical representation of a
simple pipeline that performs XInclude processing and validation on a
document.</para>

<figure xml:id="fig-xival">
<title>A simple, linear XInclude/Validate pipeline</title>
<mediaobject>
<alt>A simple, linear XInclude/Validate pipeline</alt>
<imageobject>
<imagedata fileref="graphics/sch-xinclude-validate-pipeline.png"/>
</imageobject>
</mediaobject>
</figure>

<para>This is a pipeline that consists of two atomic steps, XInclude
and Validate with XML Schema. The pipeline itself has two inputs, “source” (a source
document) and “schemas” (a sequence of W3C XML Schemas). The XInclude step
reads the pipeline input “source” and produces a result document. The
Validate with XML Schema step reads the pipeline input “schemas” and the result of
the XInclude step and produces its own result document. The result of the
validation, “result”, is the result of the pipeline. (For consistency across
the step vocabulary, the standard input is usually named “source” and
and the standard output is usually named “result”.)
</para>

<para>The pipeline document determines how the steps are connected
together inside the pipeline. <impl>How inputs are connected to XML
documents outside the pipeline is
<glossterm>implementation-defined</glossterm>.</impl> <impl>How
pipeline outputs are connected to XML documents outside the pipeline
is <glossterm>implementation-defined</glossterm>.</impl></para>

<para>The pipeline document for this pipeline is shown in
<xref linkend="ex1"/>.</para>

<example xml:id="ex1">
<title>A simple, linear XInclude/Validate pipeline</title>
<programlisting>&lt;p:declare-step xmlns:p="http://www.w3.org/ns/xproc"
		name="xinclude-and-validate"&gt;
  &lt;p:input port="source" primary="true"/&gt;
  &lt;p:input port="schemas" sequence="true"/&gt;
  &lt;p:output port="result"&gt;
    &lt;p:pipe step="validated" port="result"/&gt;
  &lt;/p:output&gt;

  &lt;p:xinclude name="included"&gt;
    &lt;p:input port="source"&gt;
      &lt;p:pipe step="xinclude-and-validate" port="source"/&gt;
    &lt;/p:input&gt;
  &lt;/p:xinclude&gt;

  &lt;p:validate-with-xml-schema name="validated"&gt;
    &lt;p:input port="source"&gt;
      &lt;p:pipe step="included" port="result"/&gt;
    &lt;/p:input&gt;
    &lt;p:input port="schema"&gt;
      &lt;p:pipe step="xinclude-and-validate" port="schemas"/&gt;
    &lt;/p:input&gt;
  &lt;/p:validate-with-xml-schema&gt;
&lt;/p:declare-step&gt;

</programlisting>
</example>

<para>The example in <xref linkend="ex1"/> is very verbose. It makes
all of the connections seen in the figure explicit. In practice,
pipelines do not have to be this verbose. XProc supports defaults for
many common cases:</para>

<itemizedlist>
<listitem>
<para>If you use <tag>p:pipeline</tag> instead of <tag>p:declare-step</tag>,
the “<port>source</port>” input port and “<port>result</port>” output port
are implicitly declared for you.</para>
</listitem>
<listitem>
<para>Where inputs and outputs are connected between sequential sibling
steps, they do not have to be made explicit.</para>
</listitem>
</itemizedlist>

<para>The same pipeline, using XProc defaults, is shown in
<xref linkend="ex1-abbr"/>.</para>

<example xml:id="ex1-abbr">
<title>A simple, linear XInclude/Validate pipeline (simplified)</title>
<programlisting>&lt;p:pipeline xmlns:p="http://www.w3.org/ns/xproc"
	    name="xinclude-and-validate"&gt;
  &lt;p:input port="schemas" sequence="true"/&gt;

  &lt;p:xinclude/&gt;

  &lt;p:validate-with-xml-schema&gt;
    &lt;p:input port="schema"&gt;
      &lt;p:pipe step="xinclude-and-validate" port="schemas"/&gt;
    &lt;/p:input&gt;
  &lt;/p:validate-with-xml-schema&gt;
&lt;/p:pipeline&gt;
</programlisting>
</example>

<para><xref linkend="fig-style-proc"/> is a more complex example: it
performs schema validation with an appropriate schema and then styles
the validated document.</para>

<figure xml:id="fig-style-proc">
<title>A validate and transform pipeline</title>
<mediaobject>
<alt>A validate and transform pipeline</alt>
<imageobject>
<imagedata fileref="graphics/sch-transform.png"/>
</imageobject>
</mediaobject>
</figure>

<para>The heart of this example is the conditional. The “choose”
step evaluates an XPath expression over a test document. Based on the
result of that expression, one or another branch is run. In this example,
each branch consists of a single validate step.</para>

<example xml:id="ex2">
<title>A validate and transform pipeline</title>
<programlisting>&lt;p:pipeline xmlns:p="http://www.w3.org/ns/xproc"&gt;

  &lt;p:choose&gt;
    &lt;p:when test="/*[@version &amp;lt; 2.0]"&gt;
      &lt;p:validate-with-xml-schema&gt;
	&lt;p:input port="schema"&gt;
	  &lt;p:document href="v1schema.xsd"/&gt;
	&lt;/p:input&gt;
      &lt;/p:validate-with-xml-schema&gt;
    &lt;/p:when&gt;

    &lt;p:otherwise&gt;
      &lt;p:validate-with-xml-schema&gt;
	&lt;p:input port="schema"&gt;
	  &lt;p:document href="v2schema.xsd"/&gt;
	&lt;/p:input&gt;
      &lt;/p:validate-with-xml-schema&gt;
    &lt;/p:otherwise&gt;
  &lt;/p:choose&gt;

  &lt;p:xslt&gt;
    &lt;p:input port="stylesheet"&gt;
      &lt;p:document href="stylesheet.xsl"/&gt;
    &lt;/p:input&gt;
  &lt;/p:xslt&gt;
&lt;/p:pipeline&gt;
</programlisting>
</example>

<para>This example, like the preceding, relies on XProc defaults for simplicity.
It is always valid to write the fully explicit form if you prefer.</para>

<para>The media type for pipeline documents is <literal>application/xml</literal>.
Often, pipeline documents are identified by the extension
<filename class="extension">.xpl</filename>.</para>

</section>

<section xml:id="pipeline-concepts">
<title>Pipeline Concepts</title>

<para><termdef xml:id="dt-pipeline">A <firstterm>pipeline</firstterm>
is a set of connected steps, with outputs of one step flowing into
inputs of another.</termdef> A pipeline is itself a
<glossterm>step</glossterm> and must satisfy the constraints on steps.
Connections between steps occur where the input of one step is bound
to the output of another.
</para>

<para>The result of evaluating a pipeline (or <glossterm>subpipeline</glossterm>)
is the result of evaluating
the steps that it contains, in an order consistent with the connections
between them. A pipeline must behave as if it
evaluated each step each time it occurs. Unless otherwise
indicated, implementations <rfc2119>must not</rfc2119> assume that
steps are functional (that is, that their outputs depend only on
their
<link linkend="input-output">inputs</link>,
<glossterm baseform="option">options</glossterm>, and
<glossterm baseform="parameter">parameters</glossterm>)
or side-effect free.</para>

<para>The pattern of connections between steps will not always
completely determine their order of evaluation. <impl>The evaluation
order of steps not connected to one another is
<glossterm>implementation-dependent</glossterm></impl>.
</para>

<section xml:id="step-concept">
<title>Steps</title>

<para><termdef xml:id="dt-step">A <firstterm>step</firstterm> is the
basic computational unit of a pipeline.</termdef> A typical step has
zero or more inputs, from which it receives XML documents to process,
zero or more outputs, to which it sends XML document results, and
can have options and/or parameters.</para>

<para>There are three kinds of steps:
<glossterm baseform="atomic step">atomic</glossterm>,
<glossterm baseform="compound step">compound</glossterm>, and
<glossterm baseform="multi-container step">multi-container</glossterm>.</para>

<para><termdef xml:id="dt-atomic-step">An <firstterm>atomic step</firstterm> is a
step that performs a unit of XML processing, such as XInclude or
transformation, and has no internal <glossterm>subpipeline</glossterm>.
</termdef> Atomic
steps carry out fundamental XML operations and can perform arbitrary
amounts of computation, but they are indivisible. An XSLT step, for
example, performs XSLT processing; a Validate with XML Schema step
validates one input with respect to some set of XML Schemas,
etc.</para>

<para>There are many <emphasis>types</emphasis> of atomic steps. The
standard library of atomic steps is described in
<xref linkend="std-components"/>, but implementations
<rfc2119>may</rfc2119> provide others as
well. <impl>What additional step types, if any, are provided is
<glossterm>implementation-defined</glossterm>.</impl>
Each use, or instance, of an atomic step invokes the processing
defined by that type of step. A pipeline may contain instances of many
types of steps and many instances of the same type of step.</para>

<para>Compound steps, on the other hand, control and organize the flow of
documents through a pipeline, reconstructing familiar programming
language functionality such as conditionals, iterators and exception
handling. They contain other steps, whose evaluation
they control.</para>

<para><termdef xml:id="dt-compound-step">A <firstterm>compound
step</firstterm> is a step that contains a
<glossterm>subpipeline</glossterm>.</termdef> That is, a compound step
differs from an atomic step in that its semantics are at least
partially determined by the steps that it contains.</para>

<para>Finally, there are two “multi-container steps”:
<tag>p:choose</tag> and <tag>p:try</tag>.
<termdef xml:id="dt-multi-container-step">A <firstterm>multi-container
step</firstterm> is a step that contains several alternate
<glossterm baseform="subpipeline">subpipelines</glossterm>.
</termdef> Each subpipeline is identified by a non-step wrapper element:
<tag>p:when</tag> and <tag>p:otherwise</tag> in the case of <tag>p:choose</tag>,
<tag>p:group</tag> and <tag>p:catch</tag> in the case of <tag>p:try</tag>.
</para>

<para>The runtime semantics of a multi-container step are that it
behaves as if it evaluated exactly one of its subpiplines. In this
sense, they function like <glossterm baseform="compound step">compound
steps</glossterm>.
</para>

<para><termdef xml:id="dt-container">A compound
step or multi-container step is a <firstterm>container</firstterm> for the
steps directly within it or within non-step wrappers directly within it.</termdef>
<termdef xml:id="dt-contained-steps">The steps that occur directly within,
or within non-step wrappers directly within, a step are called that step's
<firstterm>contained steps</firstterm>. In other words, “container”
and “contained steps” are inverse relationships.</termdef>
<termdef xml:id="dt-ancestors">The
<firstterm>ancestors</firstterm> of a step are
its <glossterm>container</glossterm>, the container of its container,
and all other containers above it.</termdef></para>

<para><termdef xml:id="dt-subpipeline">Sibling steps (and the
connections between them) form a <firstterm>subpipeline</firstterm>.</termdef>
<termdef xml:id="dt-last-step">The <firstterm>last step</firstterm> in
a subpipeline is its last step in document order.</termdef></para>

<para xml:id="p.subpipeline" role="element-syntax element-syntax-language-construct hanging-indent">
<code>subpipeline</code> = <tag>p:variable</tag>*, (<tag>p:for-each</tag>|<tag>p:viewport</tag>|<tag>p:choose</tag>|<tag>p:group</tag>|<tag>p:try</tag>|<code xlink:href="#p.atomic">p:<replaceable>standard-step</replaceable></code>|<code xlink:href="#p.atomic"><replaceable>pfx:user-pipeline</replaceable></code>)+
</para>

<para>Note that user-defined pipelines,
<code><replaceable>pfx:user-pipeline</replaceable></code>,
are atomic; although a pipeline <emphasis>declaration</emphasis>,
contains a subpipeline, a step which invokes a user-defined pipeline
does not.</para>

<para>Steps have “ports” into which inputs and outputs are connected
or “<glossterm baseform="binding">bound</glossterm>”. Each step has a
number of input ports and a number of output ports; a step can have
zero input ports and/or zero output ports. (All steps have an implicit
output port for reporting errors that <rfc2119>must not</rfc2119> be
declared.) The names of all ports on each step must be unique on that
step (you can't have two input ports named “source”, nor can you have
an input port named “schema” and an output port named “schema”).
</para>

<para>Steps may have any number of
<link linkend="options">options</link>, all with unique names.
A step can have zero options.</para>

<para>Steps may have parameter input ports, on which <link linkend="parameters">parameters</link> can be passed. The parameters
passed on a particular <glossterm>parameter input port</glossterm>
must be uniquely named. If multiple parameters with the same name are
used, only one of the values will actually be available to the step. A
step can have zero, one, or many parameter input ports, and each
parameter port can have zero or more parameters passed on it.
</para>

<para>All of the different instances of steps (atomic or compound) in
a pipeline can
be distinguished from one another by name. If the pipeline author does
not provide a name for a step, a default name is <link linkend="step-names">manufactured automatically</link>.</para>

<section xml:id="step-names">
<title>Step names</title>

<para>The <tag class="attribute">name</tag> attribute on any step can be
used to give it a name. The name must be unique within its scope,
see <xref linkend="scoping"/>.</para>

<para>If the pipeline author does not provide an explicit name, the processor
manufactures a default name. All default names are of the form
“<literal>!1</literal><replaceable>.m</replaceable><replaceable>.n</replaceable>…”
where
“<replaceable>m</replaceable>” 
is the
position of the step's highest <glossterm baseform="ancestors">ancestor</glossterm>
within the pipeline document
or library which contains it, “<replaceable>n</replaceable>”
is the position of the
next-highest ancestor, and so on, including both steps and non-step
wrappers.
For example, consider the pipeline in
<xref linkend="ex2"/>. The <tag>p:pipeline</tag> step has no name,
so it gets the default name “<literal>!1</literal>”; the
<tag>p:choose</tag> gets the name “<literal>!1.2</literal>”; the first
<tag>p:when</tag> gets the name “<literal>!1.2.1</literal>”, etc.
If the <tag>p:choose</tag> had had a name, it would not have received a default
name, but it would still have been counted and its first
<tag>p:when</tag> would still have been “<literal>!1.2.1</literal>”.</para>

<para>Providing every step in the pipeline with an interoperable name has
several benefits:</para>

<orderedlist>
<listitem><para>It allows implementors to refer to all steps in an interoperable
fashion, for example, in error messages.</para></listitem>
<listitem><para>Pragmatically, we say that <glossterm>readable ports</glossterm> are
identified by a step name/port name pair. By manufacturing names for otherwise
anonymous steps, we include implicit bindings without changing our
model.</para></listitem>
</orderedlist>

<para>In a valid pipeline that runs successfully to completion, the manufactured
names aren't visible (except perhaps in debugging or logging output).</para>

<note xml:id="def-name-ncname">
<para>The format for defaulted names does not conform to the
requirements of an
<link xlink:href="http://www.w3.org/TR/xml-names/#NT-NCName">NCName</link>.
This is an explicit design decision; it prevents pipelines from using
the defaulted names on <tag>p:pipe</tag> elements. If an explicit connection
is required, the pipeline author must provide an explicit name for the
step.</para>
</note>
</section>
</section>

<section xml:id="input-output">
<title>Inputs and Outputs</title>

<para>Although some steps can read and write non-XML resources, what
flows <emphasis>between</emphasis> steps through input ports and
output ports are exclusively XML documents or sequences of XML
documents.</para>

<para>For the purposes of this specification, an XML document is an
<biblioref linkend="xml-infoset-rec"/>. Implementations are free to
transmit infosets as sequences of characters, sequences of events,
object models, or any other representation that preserves the
necessary infoset properties (see <xref linkend="infoset-conformance"/>).</para>

<para>Most steps in this specification manipulate XML documents, or
portions of XML documents. In these cases, we speak of changing
elements, attributes, or nodes without prejudice to the actual
representation used by an implementation.</para>

<para>An implementation <rfc2119>may</rfc2119> make it possible for a
step to produce non-XML output (through channels other than a named
output port)—for example, writing a PDF document to a URI—but that
output cannot flow through the pipeline. Similarly, one can imagine a
step that takes no pipeline inputs, reads a non-XML file from a URI,
and produces an XML output. But the non-XML data cannot arrive on an
input port to a step.</para>

<para><error code="D0001">It is a <glossterm>dynamic error</glossterm>
if a non-XML resource is produced on a step output or arrives on a
step input.</error></para>

<para>The common case is that each step has one or more inputs and
one or more outputs. <xref linkend="fig-atomic-step"/> illustrates symbolically
an <glossterm>atomic step</glossterm> with two inputs and one output.</para>

<figure xml:id="fig-atomic-step">
<title>An atomic step</title>
<mediaobject>
<alt>An atomic step with two inputs and one output</alt>
<imageobject>
<imagedata fileref="graphics/atomic-step.png"/>
</imageobject>
</mediaobject>
</figure>

<para>All atomic steps are defined by a <tag>p:declare-step</tag>. The
declaration of an atomic step type defines the input ports, output ports,
and options of all steps of that type. For example, every
<tag>p:validate-with-xml-schema</tag> step has two inputs, named
“<literal>source</literal>” and “<literal>schema</literal>”,
one output named “<literal>result</literal>”, and the same set of options.
</para>

<para>Like atomic steps, top level, user-defined pipelines also have
declarations. The situation is slightly more complicated for the other
compound steps because they don't have separate declarations; each
instance of the compound step serves as its own declaration. On these
compound steps, the number and names of the outputs can be different
on each instance of the step.</para>

<para><xref linkend="fig-compound-step"/> illustrates symbolically
a compound step with one subpipeline and one output. As you can see from the
diagram, the output from the compound step comes from one of the outputs
of the subpipeline within the step.</para>

<figure xml:id="fig-compound-step">
<title>A compound step</title>
<mediaobject>
<alt>A compound step with two inputs and one output</alt>
<imageobject>
<imagedata fileref="graphics/compound-step.png"/>
</imageobject>
</mediaobject>
</figure>

<para><termdef xml:id="dt-declared-inputs">The input ports declared on
a step are its <firstterm>declared inputs</firstterm>.</termdef>
<termdef xml:id="dt-declared-outputs">The output ports declared on a
step are its <firstterm>declared outputs</firstterm>.</termdef>
When a step is used in a pipeline, it is connected to other steps through
its inputs and outputs.
</para>

<para>When a step is used, all of the <glossterm>declared
inputs</glossterm> of the step <rfc2119>must</rfc2119> be connected. Each input can be
connected to:</para>

<itemizedlist>
<listitem>
<para>The output port of some other step.</para>
</listitem>
<listitem>
<para>A fixed, inline document or sequence of documents.</para>
</listitem>
<listitem>
<para>A document read from a URI.</para>
</listitem>
<listitem>
<para>One of the inputs declared on one of its <glossterm>ancestors</glossterm>.</para>
</listitem>
<listitem>
<para>A special port provided by an ancestor compound step, for example,
“<literal>current</literal>” in a <tag>p:for-each</tag> or <tag>p:viewport</tag>.
</para>
</listitem>
</itemizedlist>

<para>When an input accepts a sequence of documents, the documents can
come from any combination of these locations.</para>

<para>The <glossterm>declared outputs</glossterm> of a step may be
connected to:</para>

<itemizedlist>
<listitem>
<para>The input port of some other step.</para>
</listitem>
<listitem>
<para>One of the outputs declared on its container.
</para>
</listitem>
</itemizedlist>

<para>The <glossterm>primary output port</glossterm> of a step
<rfc2119>must</rfc2119> be connected, but other outputs can remain
unconnected. Any documents produced on an unconnected output port are
discarded. </para>

<para>Output ports on compound steps have a dual nature: from the
perspective of the compound step's siblings, its outputs are just
ordinary outputs and must be connected as described above. From the
perspective of the subpipeline inside the compound step, they are
inputs into which something must be connected.</para>

<para>Within a compound step, the <glossterm>declared outputs</glossterm>
of the step can be connected to:</para>

<itemizedlist>
<listitem>
<para>The output port of some
<glossterm baseform="contained steps">contained step</glossterm>.</para>
</listitem>
<listitem>
<para>A fixed, inline document or sequence of documents.</para>
</listitem>
<listitem>
<para>A document read from a URI.</para>
</listitem>
</itemizedlist>

<para>Each input and output is declared to accept or produce either a
single document or a sequence of documents. It <emphasis>is
not</emphasis> an error to connect a port that is declared to
produce a sequence of documents to a port that is declared to accept
only a single document. It is, however, an error if the former
step actually produces more than one document at run
time.</para>

<para>It is also not an error to connect a port that is declared to
produce a single document to a port that is declared to accept a
sequence. A single document is the same as a sequence of one
document.</para>

<para>An output port may be
connected to more than one input port.  At runtime this will
result in distinct copies of the output.</para>

<para><termdef xml:id="dt-signature">The
<firstterm>signature</firstterm> of a step is the set of inputs,
outputs, and options that it is declared to accept.</termdef>
The declaration for a step provides a fixed signature which all its
instances share.</para>

<para><termdef xml:id="dt-matches">A step
<firstterm>matches</firstterm> its signature if and only if it specifies
an input for each declared input, it specifies no inputs that are not
declared, it specifies
an option for each option that is declared to be required, and it
specifies no options that are not declared.</termdef>
In other words, every input and required option <rfc2119>must</rfc2119> be specified
and only inputs and options that are declared <rfc2119>may</rfc2119> be
specified. Options that aren't required do not have to be
specified.</para>

<para>Steps <rfc2119>may</rfc2119> also produce error, warning, and
informative messages. These messages are captured and provided on the
<port>error</port> port inside of a <tag>p:catch</tag>. <impl>Outside
of a <link linkend="p.try">try/catch</link>, the disposition of error
messages is <glossterm>implementation-dependent</glossterm></impl>.
</para>

<section xml:id="external-docs">
<title>External Documents</title>

<para>It's common for some of the documents used in processing a pipeline
to be read from URIs. Sometimes this occurs directly, for example with a
<tag>p:document</tag> element. Sometimes it occurs indirectly, for
example if an implementation allows the URI of a pipeline input to be
specified on the command line or if an <tag>p:xslt</tag> step
encounters an <tag>xsl:import</tag> in the stylesheet that it is processing.
It's also common for some of the documents produced in processing a
pipeline to be written to locations which have, or at least could have, a URI.
</para>

<para>The process of dereferencing a URI to retrieve a document is
often more interesting than it seems at first. On the web, it may
involve caches, proxies, and various forms of indirection.
<impl>Resolving a URI locally
may involve resolvers of various sorts and possibly appeal to
<glossterm>implementation-dependent</glossterm> mechanisms such as
catalog files.</impl></para>

<para>In XProc, the situation is made even more interesting by the
fact that many intermediate results produced by steps in the pipeline have
base URIs. <impl>Whether or not (and when and how) the
intermediate results that pass between steps are ever written to a filesystem is
<glossterm>implementation-dependent</glossterm>.</impl></para>

<para><impl>In Version 1.0 of XProc, how (or if) implementers provide local
resolution mechanisms and how (or if) they provide access to intermediate
results by URI is <glossterm>implementation-defined</glossterm>.</impl>
</para>

<para>Version 1.0 of XProc does not require implementations to guarantee
that multiple attempts to dereference the same URI always produce
consistent results.</para>

<note>
<para>On the one hand, this is a somewhat unsatisfying state of
affairs because it leaves room for interoperability problems. On the other,
it is not expected to cause such problems very often in practice.
</para>

<para>If these problems arise in practice, implementers are encouraged
to use the existing extension mechanisms to give users the control
needed to circumvent them. Should such mechanisms become widespread, a
standard mechanism could be added in some future version of the
language.</para>
</note>
</section>
</section>

<section xml:id="primary-input-output">
<title>Primary Inputs and Outputs</title>

<para>As a convenience for pipeline authors, each step may have one
input port designated as the primary input port and one output port
designated as the primary output port.</para>

<para><termdef xml:id="dt-primary-input-port">If a step has a document input
port which is explicitly marked “<code>primary='true'</code>”, or if it has
exactly one document input port and that port is <emphasis>not</emphasis>
explicitly marked “<code>primary='false'</code>”, then that input port
is the <firstterm>primary input port</firstterm> of the step.</termdef>
If a step has a single input port and that port is explicitly marked
“<code>primary='false'</code>”, or if a step has more than one input
port and none is explicitly marked as the primary, then the primary
input port of that step is undefined. A step can have at most one primary
input port.</para>

<para><termdef xml:id="dt-primary-output-port">If a step has a document output
port which is explicitly marked “<code>primary='true'</code>”, or if it has
exactly one document output port and that port is <emphasis>not</emphasis>
explicitly marked “<code>primary='false'</code>”, then that output port
is the <firstterm>primary output port</firstterm> of the step.</termdef>
If a step has a single output port and that port is explicitly marked
“<code>primary='false'</code>”, or if a step has more than one output
port and none is explicitly marked as the primary, then the primary
output port of that step is undefined. A step can have at most one
primary output port.</para>

<para>The special significance of primary input and output ports is
that they are connected automatically by the processor if no
explicit binding is given. Generally speaking, if two steps appear
sequentially in a subpipeline, then the primary output of the first
step will automatically be connected to the primary input of the
second.</para>

<para>Additionally, if a compound step has no declared outputs and the
<glossterm>last step</glossterm> in its subpipeline has an unbound
primary output, then an implicit primary output port will be added to
the compound step (and consequently the last step's primary output
will be bound to it). This implicit output port has no name. It inherits the
<tag class="attribute">sequence</tag> property of the port bound to it.
</para>
</section>

<section xml:id="connections">
<title>Connections</title>

<para>Steps are connected together by their input ports and output
ports. <error code="S0001">It is a <glossterm>static error</glossterm>
if there are any loops in the connections between steps: no step can
be connected to itself nor can there be any sequence of connections
through other steps that leads back to itself.</error></para>

<section xml:id="namespace-fixup">
<title>Namespace Fixup on Outputs</title>

<para>XProc processors are expected, and sometimes required, to
perform <glossterm>namespace fixup</glossterm>. Unless the semantics
of a step explicitly says otherwise:</para>

<itemizedlist>
<listitem>
<para>The in-scope namespaces associated with a node (even those that
are inherited from namespace bindings that appear among its ancestors
in the document in which it appears initially) are assumed to travel
with it.</para>
</listitem>
<listitem>
<para>Changes to one part of a tree (wrapping or unwrapping a node or
renaming an element, for example) do not change the in-scope
namespaces associated with the descendants of the node so changed.</para>
</listitem>
</itemizedlist>

<para>As a result, some steps can produce XML documents which have no
direct serialization (because they include nodes with conflicting or
missing namespace declarations, for example).
<termdef xml:id="dt-namespace-fixup">To produce a serializable
<glossterm>XML</glossterm> document, the XProc processor must
sometimes add additional namespace nodes, perhaps even renaming
prefixes, to satisfy the constraints of <glossterm>Namespaces in
XML</glossterm>. This process is referred to as <firstterm>namespace
fixup</firstterm>.</termdef>
</para>

<para>Implementors are encouraged to perform <glossterm>namespace
fixup</glossterm> before passing documents between steps, but they are
not required to do so. Conversely, an implementation which
<emphasis>does</emphasis> serialize between steps and therefore must
perform such fixups, or reject documents that cannot be serialized, is
also conformant.</para>

<para>Except where the semantics of a step explicitly require changes,
processors are required to preserve the information in the
documents and fragments they manipulate. In
particular, the information corresponding to the
<biblioref linkend="xml-infoset-rec"/>
properties
<literal role="infoset-property">attributes</literal>,
<literal role="infoset-property">base URI</literal>,
<literal role="infoset-property">children</literal>,
<literal role="infoset-property">local name</literal>,
<literal role="infoset-property">namespace name</literal>,
<literal role="infoset-property">normalized value</literal>,
<literal role="infoset-property">owner</literal>, and
<literal role="infoset-property">parent</literal>
<rfc2119>must</rfc2119> be preserved.</para>

<para>The information corresponding to
<literal role="infoset-property">prefix</literal>,
<literal role="infoset-property">in-scope namespaces</literal>,
<literal role="infoset-property">namespace attributes</literal>, and
<literal role="infoset-property">attribute type</literal>
<rfc2119>should</rfc2119> be preserved, with changes to the first three only as
required for <glossterm>namespace fixup</glossterm>. In particular,
processors are encouraged to take account of prefix information in
creating new namespace bindings, to minimize negative impact on
prefixed names in content.</para>

<para><impl>Except for cases which are specifically called out in <xref linkend="std-components"/>, the extent to which namespace fixup, and
other checks for outputs which cannot be serialized,
are performed on intermediate outputs is
<glossterm>implementation-defined</glossterm>.</impl></para>

<para>Whenever an implementation serializes pipeline contents, for
example for pipeline outputs, logging, or as part of steps such as
<tag>p:store</tag> or <tag>p:http-request</tag>,
it is a
<link xlink:href="#err.D0001">dynamic error</link> if that
serialization could not be done so as to produce a document which is
both well-formed and namespace-well-formed, as specified in
<glossterm>XML</glossterm> and
<glossterm>Namespaces in XML</glossterm>,
regardless of what serialization method, if any, is called for.</para>
</section>
</section>

<section xml:id="environment">
<title>Environment</title>

<para><termdef xml:id="dt-environment">The
<firstterm>environment</firstterm> is a context-dependent collection
of information available withing sub-pipelines.</termdef>
Most of the information in the environment is static and can be computed
for each subpipeline before evaluation of the pipeline as a whole begins. The  in-scope
bindings have to be calculated as the pipeline is being evaluated.</para>

<para>The environment consists of:</para>

<orderedlist>
<listitem>
<para>A set of readable ports.
<termdef xml:id="dt-readable-ports">The
<firstterm>readable ports</firstterm> are a set of step name/port name
pairs.</termdef> Inputs and outputs can
only be connected to readable ports.</para>
</listitem>
<listitem>
<para>A default readable port.
<termdef xml:id="dt-default-readable-port">The <firstterm>default
readable port</firstterm>, which may be undefined, is a specific step
name/port name pair from the set of readable ports.</termdef></para>
</listitem>
 <listitem>
  <para>A set of in-scope bindings.
<termdef xml:id="dt-in-scope-bindings">The <firstterm>in-scope
bindings</firstterm> are a set of name-value pairs, based on
<glossterm>option</glossterm> and
<glossterm>variable</glossterm> bindings.</termdef></para>
 </listitem>
</orderedlist>

<para><termdef xml:id="dt-empty-environment">The
<firstterm>empty environment</firstterm> contains no readable ports, an
undefined default readable port and no in-scope bindings.</termdef>
</para>

<para>Unless otherwise specified, the environment of a
<glossterm baseform="contained steps">contained step</glossterm> is its
<glossterm>inherited environment</glossterm>.
<termdef xml:id="dt-inherited-environment">The <firstterm>inherited
environment</firstterm> of a
<glossterm baseform="contained steps">contained step</glossterm> is an environment that is the same
as the environment of its <glossterm>container</glossterm> with the
<link linkend="dt-standard-modifications">standard modifications</link>.
</termdef></para>

<para>The
<phrase xml:id="dt-standard-modifications">standard modifications</phrase>
made to an inherited environment are:</para>

<itemizedlist>
<listitem>
<para>The declared inputs of the container are added to the
<glossterm>readable ports</glossterm>.</para>
<para>In other words, contained steps can see the inputs to their container.</para>
</listitem>
<listitem>
<para>The union of all the declared outputs of all of the containers's
<glossterm>contained steps</glossterm> are added to the
<glossterm>readable ports</glossterm>.</para>
<para>In other words, sibling steps can see each other's outputs
in addition to the outputs visible to their container.</para>
</listitem>
<listitem>
<para>If there is a preceding sibling step element:</para>
<itemizedlist>
<listitem>
<para>If that preceding sibling has a <glossterm>primary output port</glossterm>,
then that output port becomes
the <glossterm>default readable port</glossterm>.</para>
</listitem>
<listitem>
<para>Otherwise, the <glossterm>default readable port</glossterm> is undefined.</para>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<para>If there <emphasis>is not</emphasis> a preceding sibling step element,
the <glossterm>default readable port</glossterm> is the
<glossterm>primary input port</glossterm> of the container, if it has one, otherwise
the default readable port is unchanged.</para>
</listitem>
 <listitem>
  <para>The names and values from each <tag>p:variable</tag> present at the
beginning of the container are added, in document order, to the
<glossterm>in-scope bindings</glossterm>.  A new binding replaces an old
binding with the same name.  See <xref linkend="p.variable"/> for the
specification of variable evaluation.</para>
 </listitem>
</itemizedlist>

<para>A step with no parent inherits the <glossterm>empty environment</glossterm>.
</para>
</section>

<section xml:id="xpath-context">
<title>XPaths in XProc</title>

<para>XProc uses XPath as an expression language.
XPath expressions are evaluated by the XProc processor
in several places: on compound steps, 
to compute the default values of options and the values of variables;
on atomic steps, to compute the actual values of options and the
values of parameters.</para>

<para>XPath expressions are also passed to some steps. These expressions
are evaluated by the implementations of the individual steps.</para>

<para>This distinction can be seen in the following example:</para>

<programlisting>&lt;p:variable name="home" select="'http://example.com/docs'"/&gt;

&lt;p:load name="read-from-home"&gt;
  &lt;p:with-option name="href" select="concat($home,'/document.xml')"/&gt;
&lt;/p:load&gt;

&lt;p:split-sequence name="select-chapters" test="@role='chapter'"&gt;
  &lt;p:input port="source" select="//section"/&gt;
&lt;/p:split-sequence&gt;
</programlisting>

<para>The <option>href</option> option of the <tag>p:load</tag> step
is evaluated by the XProc processor. The actual <literal>href</literal> option
received by the step is
simply the string literal “<uri>http://example.com/docs/document.xml</uri>”.
(The selection on the <literal>source</literal> input of the
<literal>select-chapters</literal> step is also evaluated by the XProc processor.)
</para>

<para>The XPath expression “<literal>@role='chapter'</literal>” is passed
literally to the <literal>test</literal> option on the
<tag>p:split-sequence</tag> step. That's because the nature of the
<tag>p:split-sequence</tag> is that <emphasis>it evaluates</emphasis> the expression.
Only some options on some steps expect XPath expressions.
</para>

<para>The XProc processor evaluates all of the XPath expressions in
<tag class="attribute">select</tag> attributes on variables, options,
parameters, and inputs, in
<tag class="attribute">match</tag> attributes on <tag>p:viewport</tag>,
and in <tag class="attribute">test</tag>
attributes on <tag>p:when</tag> steps.</para>

<para>An XProc implementation can use <emphasis>either</emphasis>
<biblioref linkend="xpath"/> or <biblioref linkend="xpath2"/> to
evaluate these expressions. This is a compromise driven entirely by
the timing of XProc development. During the development of this
specification, the community indicated that it was too early to
mandate that all implementations use XPath 2.0 and too late to mandate
that all implementations use XPath 1.0.</para>

<para>Many, many expressions that are likely to be used in XProc pipelines
are the same in both versions (simple element tests, ancestor and descendant
tests, string-based attribute tests, etc.).</para>

<para>As an aid to interoperability, pipeline authors may indicate the
version of XPath that they are using. The attribute
<tag class="attribute">xpath-version</tag> may be used on <tag>p:pipeline</tag>,
<tag>p:declare-step</tag>,
(or <tag>p:library</tag>) to identify the XPath version that
<rfc2119>should</rfc2119> be used to evaluate XPath expressions on
the pipeline(s). The attribute is lexically scoped, but see below.</para>

<para>If an <tag class="attribute">xpath-version</tag> is specified on
a <tag>p:pipeline</tag> or <tag>p:declare-step</tag>, then that is the
version of XPath that the step uses. If it does not specify a version,
but a version is specified on one of its ancestors, the nearest
ancestor version specified is the version that it uses. <impl>If no version
is specified on the step or among its ancestors, then its XPath
version is <glossterm>implementation-defined</glossterm>.</impl>
</para>

<note><para>The decision about which XPath version applies can be made
dynamically. For example,
if a pipeline explicitly labeled with
<tag class="attribute">xpath-version</tag> “1.0” imports a library that
does not specify a version, the implementation may elect to make the
implementation-defined XPath version of the steps in the library also “1.0”.
If the same implementation imports that library into a pipeline explicitly
labled with <tag class="attribute">xpath-version</tag> “2.0”, it can make
the implementation-defined version of those steps “2.0”.
</para>
</note>

<para>The following rules determine how the indicated version and the
implementation's actual version interact:</para>

<orderedlist>
<listitem>
<para>If the indicated version and the implementation version are the same,
then that version is used.</para>
</listitem>
<listitem>
<para>If the indicated version is 1.0 and the implementation uses XPath 2.0
(or later), the expression <rfc2119>must</rfc2119> be evaluated in
XPath 1.0 compatibility mode. <error code="S0046">It is a
<glossterm>static error</glossterm> if the processor does not support
XPath 1.0 compatibility mode.</error>
</para>
</listitem>
<listitem>
<para>If the indicated version is 2.0 (or later) and the implementation uses
XPath 1.0, the implementation <rfc2119>must not</rfc2119> evaluate any
expression that it cannot determine will give the same result in XPath 1.0
that it would have given if XPath 2.0 had been used. <error code="S0047">It is a
<glossterm>static error</glossterm> if the processor cannot determine that
the expression would yield the same result.</error>
</para>
</listitem>
</orderedlist>

<section xml:id="xproc-xpath-context">
<title>Processor XPath Context</title>

<para>When the XProc processor evaluates an XPath expression using
XPath 1.0, unless otherwise indicated by a particular step, it does so
with the following context:</para>

<variablelist>
<varlistentry>
<term>context node</term>
<listitem>
<para>The document node of a document. The document is either
specified with a <glossterm>binding</glossterm> or is taken from the
<glossterm>default readable port</glossterm>. <error code="D0008">It
is a <glossterm>dynamic error</glossterm> if a document sequence appears
where a document to be used as the context node is expected.</error>
</para>
<para>If there is no binding and there is no default readable port then
the context node is an empty document node.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>context position and context size</term>
<listitem>
<para>The context position and context size are both “1”.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>variable bindings</term>
<listitem>
<para>The union of the in-scope options and variables are available as variable
bindings to the XPath processor.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>function library</term>
<listitem>
<para>The <biblioref linkend="xpath"/> core function library and the
<xref linkend="xpath-extension-functions"/>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>in-scope namespaces</term>
<listitem>
<para>The namespace bindings in-scope on the element where the expression occurred.
</para>
</listitem>
</varlistentry>
</variablelist>

<para>When the XProc processor evaluates an XPath expression using
XPath 2.0, unless otherwise indicated by a particular step, it does so
with the following static context:</para>

<variablelist>
<varlistentry>
<term>XPath 1.0 compatibility mode</term>
<listitem>
<para>Is true if the indicated XPath version is 1.0, false otherwise.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Statically known namespaces</term>
<listitem>
<para>The namespace declarations in-scope for the containing element or
made available through <tag>p:namespaces</tag>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Default element/type namespace</term>
<listitem>
<para>The null namespace.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Default function namespace</term>
<listitem>
<para>The <biblioref linkend="xpath2"/> function namespace.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>In-scope schema definitions</term>
<listitem>
<para>None.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>In-scope variables</term>
<listitem>
<para>The union of the in-scope options and variables are available as variable
bindings to the XPath processor.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Context item static type</term>
<listitem>
<para>Document.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Function signatures</term>
<listitem>
<para>The signatures of the <biblioref linkend="xpath-functions"/> and the
<xref linkend="xpath-extension-functions"/>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Statically known collations</term>
<listitem>
<para>Implementation defined but <rfc2119>must</rfc2119> include
the Unicode codepoint collation.
<impl>The version of Unicode supported is
<glossterm>implementation-defined</glossterm>, but it is recommended that the most
recent version of Unicode be used.</impl>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Default collation</term>
<listitem>
<para>Unicode codepoint collation.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Base URI</term>
<listitem>
<para>The base URI of the element on which the expression occurs.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Statically known documents</term>
<listitem>
<para>None.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Statically known collections</term>
<listitem>
<para>None.</para>
</listitem>
</varlistentry>
</variablelist>

<para>And the following dynamic context:</para>

<variablelist>
<varlistentry>
<term>context item</term>
<listitem>
<para>The document node of a document. The document is either
specified with a <glossterm>binding</glossterm> or is taken from the
<glossterm>default readable port</glossterm>. <error code="D0008">It
is a <glossterm>dynamic error</glossterm> if a document sequence appears
where a document to be used as the context node is expected.</error>
</para>
<para>If there is no binding and there is no default readable port then
the context node is undefined.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>context position and context size</term>
<listitem>
<para>The context position and context size are both “1”.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Variable values</term>
<listitem>
<para>The union of the in-scope options and variables are available as variable
bindings to the XPath processor.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Function implementations</term>
<listitem>
<para>The <biblioref linkend="xpath-functions"/> and the
<xref linkend="xpath-extension-functions"/>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Current dateTime</term>
<listitem>
<para><impl>The point in time returned as the current dateTime is
<glossterm>implementation-defined</glossterm>.</impl>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Implicit timezone</term>
<listitem>
<para><impl>The implicit timezone is <glossterm>implementation
defined</glossterm>.</impl></para>
</listitem>
</varlistentry>
<varlistentry>
<term>Available documents</term>
<listitem>
<para><impl>The set of available documents (those that may be retrieved with
a URI) is <glossterm>implementation dependent</glossterm>.</impl></para>
</listitem>
</varlistentry>
<varlistentry>
<term>Available collections</term>
<listitem>
<para>None.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Default collection</term>
<listitem>
<para>None.
</para>
</listitem>
</varlistentry>
</variablelist>
</section>

<section xml:id="step-xpath-context">
<title>Step XPath Context</title>

<para>When <emphasis>a step</emphasis> evaluates an XPath expression using
XPath 1.0, it does
so with the following context:</para>

<variablelist>
<varlistentry>
<term>context node</term>
<listitem>
<para>The document node that appears on the primary input port of the step,
unless otherwise specified by the step.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>context position and context size</term>
<listitem>
<para>The position and size are both “1”, unless otherwise specified by the step.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>variable bindings</term>
<listitem>
<para>None, unless otherwise specified by the step.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>function library</term>
<listitem>
<para>The <biblioref linkend="xpath"/> core function library, unless otherwise
specified by the step.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>in-scope namespaces</term>
<listitem>
<para>The set of namespace bindings provided by the XProc processor. The processor
computes this set of bindings by taking a union of the bindings on the step
element itself as well as the bindings on any of the options and parameters
used in computing values for the step
(see <xref linkend="opt-param-bindings"/>).</para>

<para><impl>The results of computing the union of namespaces in the
presence of conflicting declarations for a particular prefix are
<glossterm>implementation-dependent</glossterm>.</impl></para>
</listitem>
</varlistentry>
</variablelist>

<para>When a step evaluates an XPath expression using XPath 2.0,
unless otherwise indicated by a particular step, it does so
with the following static context:</para>

<variablelist>
<varlistentry>
<term>XPath 1.0 compatibility mode</term>
<listitem>
<para>Is true if the indicated XPath version is 1.0, false otherwise.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Statically known namespaces</term>
<listitem>
<para>The namespace declarations in-scope for the containing element or
made available through <tag>p:namespaces</tag>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Default element/type namespace</term>
<listitem>
<para>The null namespace.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Default function namespace</term>
<listitem>
<para>The <biblioref linkend="xpath2"/> function namespace.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>In-scope schema definitions</term>
<listitem>
<para>None.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>In-scope variables</term>
<listitem>
<para>None, unless otherwise specified by the step.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Context item static type</term>
<listitem>
<para>Document.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Function signatures</term>
<listitem>
<para>The signatures of the <biblioref linkend="xpath-functions"/>.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Statically known collations</term>
<listitem>
<para>Implementation defined but <rfc2119>must</rfc2119> include
the Unicode codepoint collation.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Default collation</term>
<listitem>
<para>Unicode codepoint collation.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Base URI</term>
<listitem>
<para>The base URI of the element on which the expression occurs.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Statically known documents</term>
<listitem>
<para>None.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Statically known collections</term>
<listitem>
<para>None.</para>
</listitem>
</varlistentry>
</variablelist>

<para>And the following dynamic context:</para>

<variablelist>
<varlistentry>
<term>context item</term>
<listitem>
<para>The document node of the document that appears on the primary
input of the step, unless otherwise specified by the step.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>context position and context size</term>
<listitem>
<para>The context position and context size are both “1”, unless
otherwise specified by the step.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Variable values</term>
<listitem>
<para>None, unless otherwise specified by the step.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Function implementations</term>
<listitem>
<para>The <biblioref linkend="xpath-functions"/>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Current dateTime</term>
<listitem>
<para>An implementation defined point in time.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Implicit timezone</term>
<listitem>
<para><impl>The implicit timezone is <glossterm>implementation
defined</glossterm>.</impl></para>
</listitem>
</varlistentry>
<varlistentry>
<term>Available documents</term>
<listitem>
<para><impl>The set of available documents (those that may be retrieved with
a URI) is <glossterm>implementation dependent</glossterm>.</impl></para>
</listitem>
</varlistentry>
<varlistentry>
<term>Available collections</term>
<listitem>
<para>None.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Default collection</term>
<listitem>
<para>None.
</para>
</listitem>
</varlistentry>
</variablelist>
</section>

<section xml:id="xpath-extension-functions">
<title>XPath Extension Functions</title>

<para>The XProc processor <rfc2119>must</rfc2119> support a few
additional functions in XPath expressions evaluated by the
processor.</para>

<para>In the following descriptions, the names of types (<type>string</type>,
<type>boolean</type>, etc.) should be taken to mean the corresponding
<biblioref linkend="xmlschema-2"/> data types for an implementation that
uses XPath 2.0 and as the most appropriate XPath 1.0 types for an XPath 1.0
implementation.</para>

<section xml:id="f.system-property">
<title>System Properties</title>

<para>XPath expressions within a pipeline document can interrogate the
processor for information about the current state of the pipeline.
Various aspects of the processor are exposed through the
<function>p:system-property</function> function in the pipeline
namespace:</para>

<para>Function: <type>string</type> <function>p:system-property</function>(<type>string</type> <varname>property</varname>)</para>

<para>The <varname>property</varname> string must have the form of a QName; the 
QName is expanded into a name using the namespace declarations in scope for the
expression. The <function>p:system-property</function> function returns the string
representing the value of the system property identified by the QName.
If there is no such property, the empty string <rfc2119>must</rfc2119> be returned.</para>

<para>Implementations <rfc2119>must</rfc2119> provide the following system
properties, which are all in the XProc namespace:</para>

<variablelist>
<varlistentry><term><varname>p:episode</varname></term>
<listitem>
<para>Returns a string which <rfc2119>should</rfc2119> be
unique for each invocation of the pipeline processor. In other words, if a processor is
run several times in succession, or if several processors are running simultaneously,
each invocation of each processor should get a distinct value from <varname>p:episode</varname>.</para>
<para>The unique identifier must consist of alphanumeric characters and must
start with an alphabetic character. Thus, the string is syntactically an XML
name.</para>
</listitem>
</varlistentry>
<varlistentry><term><varname>p:language</varname></term>
<listitem>
<para>Returns a string which identifies the current language, for example,
for message localization purposes.
<impl>The exact format of the language string is
<glossterm>implementation defined</glossterm> but
<rfc2119>should</rfc2119> be the same as the <tag class="attribute">xml:lang</tag>
attribute.</impl></para>
</listitem>
</varlistentry>
<varlistentry><term><varname>p:product-name</varname></term>
<listitem>
<para>Returns a string containing the name of the implementation,
as defined by the implementer. This should normally remain constant from one
release of the product to the next. It should also be constant across
platforms in cases where the same source code is used to produce compatible
products for multiple execution platforms.</para>
</listitem>
</varlistentry>
<varlistentry><term><varname>p:product-version</varname></term>
<listitem>
<para>Returns a string identifying the version of the
implementation, as defined by the implementer. This should normally vary
from one release of the product to the next, and at the discretion of the
implementer it may also vary across different execution platforms.
</para>
</listitem>
</varlistentry>
<varlistentry><term><varname>p:vendor</varname></term>
<listitem>
<para>Returns a string which identifies the vendor of the processor.</para>
</listitem>
</varlistentry>
<varlistentry><term><varname>p:vendor-uri</varname></term>
<listitem>
<para>Returns a URI which identifies the vendor of the processor. Often, this is
the URI of the vendor's web site.</para>
</listitem>
</varlistentry>
<varlistentry><term><varname>p:version</varname></term>
<listitem>
<para>Returns the version of XProc implemented by the processor; for processors 
implementing the version of XProc specified by this document, the value is “1.0”.
The value of the version attribute is a token (i.e., an
<literal>xs:token</literal> per <biblioref linkend="xmlschema-2"/>).</para>
</listitem>
</varlistentry>
<varlistentry><term><varname>p:xpath-version</varname></term>
<listitem>
<para>Returns the version of XPath implemented by the processor for evaluating
XPath expressions on XProc elements.</para>
</listitem>
</varlistentry>
</variablelist>
</section>
<section xml:id="f.step-available">
<title>Step Available</title>

<para>The <function>p:step-available</function> function reports whether or not
a particular type of step is understood by the processor.</para>

<para>Function: <type>boolean</type> <function>p:step-available</function>(<type>string</type> <varname>step-type</varname>)</para>

<para>The <varname>step-type</varname> string must have the form of a QName; the 
QName is expanded into a name using the namespace declarations in scope for the
expression. The <function>p:step-available</function> function returns true if and
only if the processor knows how to evaluate steps of the specified type.</para>
</section>

<section xml:id="f.iteration-position">
<title>Iteration Position</title>

<para>In the context of a <tag>p:for-each</tag> or a
<tag>p:viewport</tag>, the <function>p:iteration-position</function>
function reports the position of the document being processed in the
sequence of documents that will be processed. In the context of other
standard XProc compound steps, it returns 1.</para>

<para>Function: <type>integer</type> <function>p:iteration-position</function>()</para>

<para><impl>In the context of an extension compound step, the value returned by
<function>p:iteration-position</function> is
<glossterm>implementation-defined</glossterm>.</impl></para>

</section>

<section xml:id="f.iteration-size">
<title>Iteration Size</title>

<para>In the context of a <tag>p:for-each</tag> or a
<tag>p:viewport</tag>, the <function>p:iteration-size</function>
function reports the number of documents in the
sequence of documents that will be processed. In the context of other
standard XProc compound steps, it returns 1.</para>

<para>Function: <type>integer</type> <function>p:iteration-size</function>()</para>

<para><impl>In the context of an extension compound step, the value returned by
<function>p:iteration-size</function> is
<glossterm>implementation-defined</glossterm>.</impl></para>
</section>

<section xml:id="f.base-uri">
<title>Base URI</title>

<para>Returns the base URI of the specified node, if it has one.
The semantics of this function are the same as the semantics
of the XPath 2.0 <literal>fn:base-uri()</literal> function. </para>

<para>Function: <type>string</type> <function>p:base-uri</function>()</para>
<para>Function: <type>string</type> <function>p:base-uri</function>(Node node)</para>

<para>If no node is specified, the base URI of the context node is returned.
</para>

</section>

<section xml:id="f.resolve-uri">
<title>Resolve URI</title>

<para>Resolves a relative URI with respect to a particular base URI.
The semantics of this function are the same as the semantics
of the XPath 2.0 <literal>fn:resolve-uri()</literal> function. </para>

<para>Function: <type>string</type> <function>p:resolve-uri</function>(String relative)</para>
<para>Function: <type>string</type> <function>p:resolve-uri</function>(String relative, String base)</para>

<para>If no base is specified, the base URI of the context node is used.
</para>

</section>

<section xml:id="other-xpath-extension-functions">
<title>Other XPath Extension Functions</title>

<para><impl>It is <glossterm>implementation defined</glossterm> if the
processor supports any other XPath extension functions.</impl>
</para>
</section>

</section>
</section>

<section xml:id="variables">
<title>Variables</title>

<para>Variables are name/value pairs. Pipeline authors can create
variables to hold computed values.</para>

<para><termdef xml:id="dt-variable">A <firstterm>variable</firstterm> is
a name/value pair where the name is an
<link xlink:href="http://www.w3.org/TR/REC-xml-names/#dt-expname">expanded name</link>
and the value <rfc2119>must</rfc2119> be a string.</termdef> If a
document, node, or other value is given, its XPath string value is
computed and that string is used.
</para>

<para>Variables and options share the same scope and may shadow each other.
</para>
</section>

<section xml:id="options">
<title>Options</title>

<para>Some steps accept options. Options are name/value pairs, like
variables. Unlike variables, the value of an option can be changed by
the caller.</para>

<para><termdef xml:id="dt-option">An <firstterm>option</firstterm> is
a name/value pair where the name is an
<link xlink:href="http://www.w3.org/TR/REC-xml-names/#dt-expname">expanded name</link>
and the value <rfc2119>must</rfc2119> be a string.</termdef> If a
document, node, or other value is given, its XPath string value is
computed and that string is used.
</para>

<para><termdef xml:id="dt-declared-options">The options declared on a
step are its <firstterm>declared options</firstterm>.</termdef> Option
names are always expressed as literal values, pipelines cannot
construct option names dynamically.
</para>

<para><termdef xml:id="dt-specified-options">The options on a step which have
specified values, either because a <tag>p:with-option</tag> element specifies
a value or because the declaration included a default value,
are its <firstterm>specified options</firstterm>.</termdef>
</para>
</section>

<section xml:id="parameters">
<title>Parameters</title>

<para>Some steps accept parameters. Parameters are name/value pairs, like
variables and options.
Unlike variables and options, which have names known in advance to the
pipeline, parameters are not declared and their names may be unknown
to the pipeline author. Pipelines can dynamically construct sets of
parameters. Steps can read dynamically constructed sets on
<glossterm baseform="parameter input port">parameter input ports</glossterm>.
</para>

<para><termdef xml:id="dt-parameter">A <firstterm>parameter</firstterm> is
a name/value pair where the name is an
<link xlink:href="http://www.w3.org/TR/REC-xml-names/#dt-expname">expanded name</link>
and the value <rfc2119>must</rfc2119> be a string.</termdef>
If a document, node, or other value is given, its XPath string value
is computed and that string is used.
</para>

<para><termdef xml:id="dt-parameter-input-port">A
<firstterm>parameter input port</firstterm> is a distinguished kind of
input port which accepts (only) dynamically constructed parameter name/value
pairs.</termdef> See <xref linkend="parameter-inputs"/>.</para>

<para>Analogous to
<glossterm baseform="primary input port">primary input ports</glossterm>, steps
that have parameter inputs may designate at most one parameter input
port as a primary parameter input port.</para>

<para><termdef xml:id="dt-primary-parameter-input-port">If a step has
a parameter input port which is explicitly marked “<code>primary='true'</code>”,
or if it has exactly one parameter input port and that port is
<emphasis>not</emphasis> explicitly marked “<code>primary='false'</code>”, then
that parameter input port is the <firstterm>primary parameter input port</firstterm>
of the step.</termdef>
If a step has a single parameter input port and that port is
explicitly marked “<code>primary='false'</code>”, or if a step has
more than one parameter input port and none is explicitly marked as
the primary, then the primary parameter input port of that step is
undefined.</para>

<para><impl>How an implementation maps parameters specified to the
application, or through some API, to parameters accepted by the
top level pipeline is
<glossterm>implementation-defined</glossterm>.</impl></para>
</section>

<section xml:id="security-considerations">
<title>Security Considerations</title>

<para>An XProc pipeline may attempt to access arbitrary network
resources: steps such as <tag>p:load</tag> and
<tag>p:http-request</tag> can attempt to read from an arbitrary URI;
steps such as <tag>p:store</tag> can attempt to write to an arbitrary
location; <tag>p:exec</tag> can attempt to execute an arbitrary program.
Note, also, that some steps, such as <tag>p:xslt</tag>
and <tag>p:xquery</tag>, include extension mechanisms which may attempt
to execute arbitrary code.
</para>

<para>In some environments, it may be inappropriate to provide the XProc
pipeline with access to these resources. In a server environment, for
example, it may be impractical to allow pipelines to store data. In
environments where the pipeline cannot be trusted, allowing the
pipeline to access arbitrary resources or execute arbitrary code
may be a security risk.</para>

<para><error code="D0021">It is a <glossterm>dynamic error</glossterm>
for a pipeline to attempt to access a resource for which it has
insufficient privileges or perform a step which is forbidden.</error>
</para>

<para>A conformant XProc processor may limit the resources available to any
or all steps in a pipeline. A conformant implementation may raise
dynamic errors, or take any other corrective action, for any security
problems that it detects.</para>
</section>

<section xml:id="versioning-considerations">
<title>Versioning Considerations</title>

<para>A pipeline author may identify the version of XProc against which a
particular pipeline was authored by explicitly importing the
library that identifies the steps defined by that version of XProc. For the
version defined by this specification, the library is
“<uri>http://www.w3.org/2008/xproc-1.0.xpl</uri>”.</para>

<para>If the version is not explicitly identified, the implicit version
<rfc2119>should</rfc2119> be the most recent version known to the 
processor.</para>

<para>When a processor encounters a version it does not recognize, it
proceeds in forwards-compatible mode. In forwards-compatible mode:</para>

<orderedlist>
<listitem>
<para>The library that identifies the version of XProc is imported,
see <tag>p:import</tag>. This provides the processor with declarations
for any new step types.</para>
</listitem>
<listitem>
<para>It is a dynamic error to attempt to evaluate a step type for which
no implementation is known, but conditional processing and the
<function>step-available</function> function can be used to write
backwards-compatible pipelines.</para>
</listitem>
<listitem>
<para>It is a static error if the signature of a known step in the
version library has changed, except for new options.</para>
</listitem>
<listitem>
<para>New options on known steps are ignored in the pipeline.</para>
</listitem>
</orderedlist>

<para>As a consequence, future specifications <rfc2119>must
not</rfc2119> change the semantics of existing step types without
changing their names.</para>
</section>
</section>

<section xml:id="syntax">
<title>Syntax Overview</title>

<para>This section describes the normative XML syntax of XProc. This
syntax is sufficient to represent all the aspects of a pipeline, as
set out in the preceding sections.
<termdef xml:id="dt-XML">XProc is intended to work equally
well with <biblioref linkend="xml10"/> and <biblioref linkend="xml11"/>.
Unless otherwise noted, the term “<firstterm>XML</firstterm>”
refers equally to both versions.</termdef>
<termdef xml:id="dt-Namespaces-in-XML">Unless otherwise noted, the term
<firstterm>Namespaces in XML</firstterm> refers equally to
<biblioref linkend="xmlns10"/> and <biblioref linkend="xmlns11"/>.</termdef>
<impl>Support for
pipeline documents written in XML 1.1 and pipeline inputs and outputs
that use XML 1.1 is <glossterm>implementation-defined</glossterm>.</impl>
</para>

<para>Elements in a pipeline document represent the pipeline, the
steps it contains, the connections between those steps, the steps and
connections contained within them, and so on. Each step is represented
by an element; a combination of elements and attributes specify how
the inputs and outputs of each step are connected and how options and
parameters are passed.</para>

<para>Conceptually, we can speak of steps as objects that have
inputs and outputs, that are connected together and which may
contain additional steps. Syntactically, we need a mechanism
for specifying these relationships.</para>

<para><glossterm baseform="container">Containment</glossterm> is
represented naturally using nesting of XML elements. If a particular element
identifies a <glossterm>compound step</glossterm> then the step elements that
are its immediate children form its <glossterm>subpipeline</glossterm>.</para>

<para>The connections
between steps are expressed using names and references to those
names.</para>

<para>Six kinds of things are named in XProc:</para>

<orderedlist spacing="compact">
<listitem>
<simpara>Step types,</simpara>
</listitem>
<listitem>
<simpara>Steps,</simpara>
</listitem>
<listitem>
<simpara>Input ports (both parameter and document),</simpara>
</listitem>
<listitem>
<simpara>Output ports,</simpara>
</listitem>
<listitem>
<simpara>Options and variables, and</simpara>
</listitem>
<listitem>
<simpara>Parameters</simpara>
</listitem>
</orderedlist>

<section xml:id="namespaces">
<title>XProc Namespaces</title>

<para>There are four namespaces associated with XProc:</para>

<variablelist>
<varlistentry>
<term><uri type="xmlnamespace">http://www.w3.org/ns/xproc</uri></term>
<listitem>
<para>The namespace of the XProc XML vocabulary described by this
specification; by convention, the namespace prefix
“<literal>p:</literal>” is used for this namespace.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><uri type="xmlnamespace">http://www.w3.org/ns/xproc-step</uri></term>
<listitem>
<para>The namespace used for documents that are inputs to and outputs
from several standard and optional steps described in this
specification. Some steps, such as <tag>p:http-request</tag> and
<tag>p:store</tag>, have defined input or output vocabularies. We use
this namespace for all of those documents. The conventional prefix
“<literal>c:</literal>” is used for this namespace.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><uri type="xmlnamespace">http://www.w3.org/ns/xproc-error</uri></term>
<listitem>
<para>The namespace used for errors. The conventional prefix
“<literal>err:</literal>” is used for this namespace.
</para>
</listitem>
</varlistentry>
</variablelist>
</section>

<section xml:id="scoping">
<title>Scoping of Names</title>

<para>Names are used to identify step types, steps, ports, options and
variables, and parameters. Step types, options, variables, and parameters
are named with QNames. Steps and ports are named with NCNames.
The scope of a name is a measure of where
it is available in a pipeline. <termdef xml:id="dt-visible">If two
names are in the same scope, we say that they are
<firstterm>visible</firstterm> to each other. </termdef></para>

<para>The scope of the names of the step types is the union of all the
pipelines and pipeline libraries available directly or via
<tag>p:import</tag>. In other words, the step types visible in a pipeline or
library are:</para>

<itemizedlist>
<listitem><para>The standard, built-in types (<tag>p:pipeline</tag>, <tag>p:choose</tag>, etc.).
</para></listitem>
<listitem><para>Any implementation-provided types.
</para></listitem>
<listitem><para>The types visible in any library that is imported.
</para></listitem>
<listitem><para>The step types declared in the pipeline or library.
</para></listitem>
<listitem><para>The pipelines that are imported.
</para></listitem>
<listitem><para>For a pipeline, the pipeline itself.
</para></listitem>
<listitem><para>For a pipeline in a library, the types visible in the containing library.
</para></listitem>
</itemizedlist>

<para><error code="S0036">All the step types in a pipeline
<rfc2119>must</rfc2119> have unique names: it is a <glossterm>static
error</glossterm> if any step type name is built-in and/or declared or defined
more than once in the
same scope.</error></para>

<para>The scope of the names of the steps themselves is determined by
the <glossterm>environment</glossterm> of each step. In general, the
name of a step, the names of its sibling steps, the names of any steps
that it contains directly, the names of its ancestors, and the names
of the siblings of its ancestors are all in a common scope.
<error code="S0002">All steps in the same scope
<rfc2119>must</rfc2119> have unique names: it is a <glossterm>static
error</glossterm> if two steps with the same name appear in the same
scope.</error></para>

<para>The scope of an input or output port name is the step on
which it is defined. The names of all the ports on any step
<rfc2119>must</rfc2119> be unique.</para>

<para>Taken together, these uniqueness constraints guarantee that the
combination of a step name and a port name uniquely identifies
exactly one port on exactly one in-scope step.</para>

<para>The scope of option and variable names is determined by where
they are declared. When an option is declared with <tag>p:option</tag>
(or a variable with <tag>p:variable</tag>), unless otherwise specified,
its scope consists of
the sibling elements that follow its declaration and the descendants
of those siblings.</para>

<para>Parameter names are not scoped; they are distinct on each step.</para>
</section>

<section xml:id="xml-base-attribute">
<title>Base URIs and xml:base</title>

<para>When a relative URI appears in an option value, the base URI
against which it <rfc2119>must</rfc2119> be made absolute is the base
URI of the <tag>p:option</tag> element. If an option value is specified
using a <link linkend="option-shortcut">syntactic shortcut</link>, the
base URI of the step on which the shortcut attribute appears <rfc2119>must</rfc2119>
be used. In general, whenever a relative URI appears, its base URI is the
base URI of the nearest ancestor element.</para>

<para>The pipeline author can control the base URIs of elements within
the pipeline document with the <tag class="attribute">xml:base</tag> attribute.
The <tag class="attribute">xml:base</tag> attribute <rfc2119>may</rfc2119>
appear on any element in a pipeline and has the semantics outlined in
<biblioref linkend="xml-base"/>.</para>
</section>

<section xml:id="xml-id-attribute">
<title>Unique identifiers</title>

<para>A pipeline author can provide a globally unique identifier for any
element in a pipeline with the <tag class="attribute">xml:id</tag>
attribute.</para>

<para>The <tag class="attribute">xml:id</tag> attribute <rfc2119>may</rfc2119>
appear on any element in a pipeline and has the semantics outlined in
<biblioref linkend="xml-id"/>.</para>
</section>

<section xml:id="syntax-docs-ports">
<title>Associating Documents with Ports</title>

<para><termdef xml:id="dt-binding">A <firstterm>binding</firstterm> associates an input
or output port with some data source.</termdef>
A document or a sequence of documents can be bound to a port in
four ways: <glossterm>by source</glossterm>, <glossterm>by URI</glossterm>,
by providing an <glossterm>inline document</glossterm>, or by making it
<glossterm baseform="empty-sequence">explicitly empty</glossterm>.
Each of these mechanisms is allowed on the
<tag>p:input</tag>, <tag>p:output</tag>, <tag>p:xpath-context</tag>,
<tag>p:iteration-source</tag>, and <tag>p:viewport-source</tag>
elements.</para>

<variablelist>
<varlistentry>
<term>Specified by URI</term>
<listitem>
<para><termdef xml:id="dt-by-URI">A document is specified
<firstterm>by URI</firstterm> if it is referenced with a URI.</termdef>
The <tag class="attribute">href</tag>
attribute on the <tag>p:document</tag> element is used to refer
to documents by URI.</para>

<para>In this example, the input to the <tag>p:identity</tag> step
named “<literal>otherstep</literal>” comes from
“<uri>http://example.com/input.xml</uri>”.
</para>

<programlisting>&lt;p:output port="result"/&gt;

&lt;p:identity name="otherstep"&gt;
  &lt;p:input port="source"&gt;
    &lt;p:document href="http://example.com/input.xml"/&gt;
  &lt;/p:input&gt;
&lt;/p:identity&gt;
</programlisting>

<para><error code="D0002">It is a <glossterm>dynamic error</glossterm> if the processor
attempts to retrieve the URI specified on a <tag>p:document</tag>
and fails.</error> (For example, if the
resource does not exist or is not accessible with the user's
authentication credentials.)</para>
</listitem>
</varlistentry>

<varlistentry>
<term>Specified by source</term>
<listitem>
<para><termdef xml:id="dt-by-source">A document is specified
<firstterm>by source</firstterm> if it references a specific port
on another step.</termdef> The 
<tag class="attribute">step</tag> and
<tag class="attribute">port</tag> attributes on the <tag>p:pipe</tag>
element are used for this purpose.
</para>

<para>In this example, the “<literal>source</literal>” input to the
<tag>p:xinclude</tag> step named
“<literal>expand</literal>” comes from the “<literal>result</literal>”
port of the step named “<literal>otherstep</literal>”.</para>

<programlisting>&lt;p:xinclude name="expand"&gt;
  &lt;p:input port="source"&gt;
    &lt;p:pipe step="otherstep" port="result"/&gt;
  &lt;/p:input&gt;
&lt;/p:xinclude&gt;
</programlisting>

<para>When a <tag>p:pipe</tag> is used, the specified port must be
in the <glossterm>readable ports</glossterm> of the current environment.
<error code="S0003">It is a <glossterm>static error</glossterm> if the port specified by a
<tag>p:pipe</tag> is not in the <glossterm>readable ports</glossterm> of the
environment.</error></para>
</listitem>
</varlistentry>

<varlistentry>
<term>Specified inline</term>
<listitem>
<para><termdef xml:id="dt-inline-document">An
<firstterm>inline document</firstterm> is specified directly in
the body of the element that binds it.</termdef>
The content of the <tag>p:inline</tag> element is used for this purpose.
</para>

<para>In this example, the “<literal>stylesheet</literal>”
input to the XSLT step named
“<literal>xform</literal>” comes from the content of the
<tag>p:input</tag> element itself.</para>

<programlisting>&lt;p:xslt name="xform"&gt;
  &lt;p:input port="stylesheet"&gt;
    &lt;p:inline&gt;
      &lt;xsl:stylesheet version="1.0"&gt;
        ...
      &lt;/xsl:stylesheet&gt;
    &lt;/p:inline&gt;
  &lt;/p:input&gt;
&lt;/p:xslt&gt;
</programlisting>

<para>Inline documents are considered “quoted”. The pipeline processor
passes them literally to the port, even if they contain elements from
the XProc namespace or other namespaces that would have other
semantics outside of the <tag>p:inline</tag>.</para>
</listitem>
</varlistentry>

<varlistentry>
<term>Specified explicitly empty</term>
<listitem>
<para><termdef xml:id="dt-empty-sequence">An
<firstterm>empty sequence</firstterm> of documents is specified with the
<tag>p:empty</tag> element.</termdef>
</para>

<para>In this example, the “<literal>source</literal>”
input to the XSLT 2.0 step named
“<literal>generate</literal>” is explicitly empty:</para>

<programlisting>&lt;p:xslt name="generate" version="2.0"&gt;
  &lt;p:input port="source"&gt;
    &lt;p:empty/&gt;
  &lt;/p:input&gt;
  &lt;p:input port="stylesheet"&gt;
    &lt;p:inline&gt;
      &lt;xsl:stylesheet version="2.0"&gt;
        ...
      &lt;/xsl:stylesheet&gt;
    &lt;/p:inline&gt;
  &lt;/p:input&gt;
  &lt;p:with-option name="template-name" select="'someName'"/&gt;
&lt;/p:xslt&gt;
</programlisting>

<para>If you omit the binding on a primary input port, a binding to the
<glossterm>default readable port</glossterm> will be assumed. Making the
binding explicitly empty guarantees that the binding will be to an empty
sequence of documents.</para>

<para xml:id="empty-xpath-context">It is inconsistent with the
<biblioref linkend="xpath"/> specification to specify an empty binding
as the context for evaluating an XPath expression. When an empty
binding is specified for an XPath 1.0 expression, an empty document node
<rfc2119>must</rfc2119> be used instead as the context node.</para>

</listitem>
</varlistentry>
</variablelist>

<para>Note that a <tag>p:input</tag> or <tag>p:output</tag> element
may contain more than one <tag>p:pipe</tag>, <tag>p:document</tag>,
or <tag>p:inline</tag>
element. If more than one <glossterm>binding</glossterm> is provided, then the 
specified sequence of documents is made available on that port in the same
order as the bindings.</para>

</section>

<section xml:id="documentation">
<title>Documentation</title>

<para>Pipeline authors may add documentation to their pipeline documents
with the <tag>p:documentation</tag> element. Except when it appears as a descendant of
<tag>p:inline</tag>, the <tag>p:documentation</tag> element is
completely ignored by pipeline processors, it exists simply for documentation
purposes. (If a <tag>p:documentation</tag> is provided as a descendant of <tag>p:inline</tag>,
it has no special semantics, it is treated literally as part of the document
to be provided on that port.)</para>

<para>Pipeline processors that inspect the contents of
<tag>p:documentation</tag> elements and behave differently on the
basis of what they find are <emphasis>not conformant</emphasis>.
Processor extensions <rfc2119>must</rfc2119> be specified with
<tag>p:pipeinfo</tag>.</para>
</section>

<section xml:id="annotations">
<title>Processor annotations</title>

<para>Pipeline authors may add annotations to their pipeline documents
with the <tag>p:pipeinfo</tag> element. <impl>The semantics of
<tag>p:pipeinfo</tag> elements are
<glossterm>implementation-defined</glossterm>.</impl> Processors
<rfc2119>should</rfc2119> specify a way for their annotations to be
identified, perhaps with <link linkend="extension-attributes">extension attributes</link>.</para>

<para>Where <tag>p:documentation</tag> is intended for human consumption,
<tag>p:pipeinfo</tag> elements are intended for processor consumption.
A processor might, for example, use annotations to identify some particular
aspect of an implementation, to request additional, perhaps non-standard
features, to describe parallelism constraints, etc.</para>

<para>When a <tag>p:pipeinfo</tag> appears as a descendant of
<tag>p:inline</tag>, it has no
special semantics; in that context it <rfc2119>must</rfc2119>
be treated literally as part of the document to be provided on that
port.</para>
</section>

<section xml:id="extension-attributes">
<title>Extension attributes</title>

<para><termdef xml:id="dt-extension-attribute">An element from the
XProc namespace <rfc2119>may</rfc2119> have any attribute not from the
XProc namespace, provided that the expanded-QName of the attribute has
a non-null namespace URI. Such an attribute is called an
<firstterm>extension attribute</firstterm>.</termdef>
</para>

<para>The presence of
an extension attribute must not cause the connections between steps to
differ from the connections that would arise in the absence of the
attribute. They must not cause the processor to fail to signal an
error that would be signalled in the absence of the attribute.</para>

<para>A processor which encounters an extension attribute that it does not
recognize <rfc2119>must</rfc2119> behave as if the attribute was not present.</para>

</section>

<section xml:id="syntax-summaries">
<title>Syntax Summaries</title>

<para>The description of each element in the pipeline namespace is accompanied
by a syntactic summary that provides a quick overview of the element's
syntax:</para>

<e:rng-fragment name="SomeElement" role="nosummary">
  <grammar xmlns="http://relaxng.org/ns/structure/1.0">
    <define xmlns:sa="http://xproc.org/ns/syntax-annotations" name="SomeElement" sa:class="language-example">
      <element name="some-element">
	<optional>
	  <attribute name="some-attribute">
	    <data type="some-type"/>
	  </attribute>
	</optional>
	<zeroOrMore>
	  <choice>
	    <ref name="Some"/>
	    <ref name="Elements"/>
	    <ref name="Allowed"/>
	  </choice>
	</zeroOrMore>
	<optional>
	  <ref name="OtherElements"/>
	</optional>
      </element>
    </define>

    <define name="Some"><element name="some"><empty/></element></define>
    <define name="Elements"><element name="elements"><empty/></element></define>
    <define name="Allowed"><element name="allowed"><empty/></element></define>
    <define name="OtherElements"><element name="other-elements"><empty/></element></define>
  </grammar>
</e:rng-fragment>

<para>For clarity of exposition, some attributes and elements are elided from
the summaries:</para>

<itemizedlist>
<listitem>
<para>An <tag class="attribute">xml:id</tag> attribute is allowed on any element.
It has the semantics of <biblioref linkend="xml-id"/>.</para>
</listitem>
<listitem>
<para>An <tag class="attribute">xml:base</tag> attribute is allowed on any element.
It has the semantics of <biblioref linkend="xml-base"/>.</para>
</listitem>
<listitem>
<para>The <tag>p:documentation</tag> and <tag>p:pipeinfo</tag> elements are
not shown, but they are allowed anywhere.</para>
</listitem>
<listitem>
<para>Attributes that are <link linkend="option-shortcut">syntactic shortcuts
for option values</link> are not shown.</para>
</listitem>
</itemizedlist>

<para>The types given for attributes should be understood as
follows:</para>

<itemizedlist>
<listitem>
<para><type>ID</type>, <type>NCName</type>, <type>NMTOKEN</type>,
<type>NMTOKENS</type>, <type>anyURI</type>, <type>boolean</type>,
<type>integer</type>, <type>string</type>: As per <biblioref linkend="xmlschema-2"/> including whitespace
normalization as appropriate.</para>
</listitem>
<listitem>
<para><type>QName</type>: With whitespace normalization as per
<biblioref linkend="xmlschema-2"/> and according to the following
definition: <termdef xml:id="dt-QName">In the context of XProc, a
<firstterm>QName</firstterm> is almost always a QName in the
<glossterm>Namespaces in XML</glossterm> sense.
Note, however, that <tag>p:option</tag> and
<tag>p:with-param</tag> values can get their namespace declarations in
a non-standard way (with <tag>p:namespaces</tag>) and QNames that have
no prefix are always in no-namespace, irrespective of the default
namespace.</termdef>
</para>
</listitem>
<listitem>
<para><type>PrefixList</type>:
As a list with <literal role="infoset-property">item type</literal>
<type>NMTOKEN</type>,
per <biblioref linkend="xmlschema-2"/>, including whitespace normalization.
</para>
</listitem>
<listitem>
<para><type>XPathExpression</type>, <type>XSLTMatchPattern</type>:
As a string per <biblioref linkend="xmlschema-2"/>, including
whitespace normalization, and the further requirement to be a
conformant Expression per <biblioref linkend="xpath"/> or
<biblioref linkend="xpath2"/>, as appropriate,
or Match pattern per <biblioref linkend="xslt10"/> or
<biblioref linkend="xslt20"/>, as appropriate.
</para>
</listitem>
</itemizedlist>

<para>A number of errors apply generally:</para>

<itemizedlist>
<listitem>
<para><error code="S0008">It is a <glossterm>static error</glossterm>
if any element in the XProc namespace has attributes not defined by
this specification unless they are <glossterm baseform="extension attribute">extension attributes</glossterm>.</error>
</para>
</listitem>
<listitem>
<para><error code="S0038">It is a <glossterm>static error</glossterm>
if any required attribute is not provided.</error>
</para>
</listitem>
<listitem>
<para><error code="S0043">It is a <glossterm>static error</glossterm>
if any attribute value does not satisfy the type required for that
attribute.</error></para>
</listitem>
<listitem>
<para><error code="S0045">It is a <glossterm>static error</glossterm>
if any string that must be interpreted as a <glossterm>QName</glossterm>
uses a prefix for which there is not a namespace binding.</error>
</para>
</listitem>
<listitem>
<para><error code="S0044">It is a <glossterm>static error</glossterm>
if any element in the XProc namespace or any step has element children
other than those specified for it by this specification.
</error></para>
</listitem>
<listitem>
<para><error code="S0037">It is a <glossterm>static error</glossterm>
if any step directly contains text nodes that do not consist entirely of
whitespace.</error>
</para>
</listitem>
<listitem>
<para><error code="D0019">It is a <glossterm>dynamic error</glossterm>
if any option value does not satisfy the type required for that
option.</error>
</para>
</listitem>
<listitem>
<para><error code="S0015">It is a <glossterm>static error</glossterm> if a
compound step has no <glossterm>contained steps</glossterm>.</error>
</para>
</listitem>
</itemizedlist>

<para>If an XProc processor can determine statically that a dynamic
error will <emphasis>always</emphasis> occur, it
<rfc2119>may</rfc2119> report that error statically provided that the
error <emphasis>does not</emphasis>
occur among the descendants of a <tag>p:try</tag>. Errors
inside a <tag>p:try</tag> must always be raised dynamically so that
<tag>p:catch</tag> processing may be performed on them.
</para>

</section>
</section>

<section xml:id="steps">
<title>Steps</title>

<para>This section describes the core steps of XProc.
</para>

<section xml:id="p.pipeline">
<title>p:pipeline</title>

<para>A <tag>p:pipeline</tag> declares a pipeline that can
be evaluated by an XProc processor. It encapsulates the behavior of a
<glossterm>subpipeline</glossterm>. Its children declare inputs,
outputs, and options that the pipeline exposes and identify the steps
in its subpipeline. (A <tag>p:pipeline</tag> is a simplified form of
<link linkend="p.declare-step">step declaration</link>.)
</para>

<para>All <tag>p:pipeline</tag> pipelines have an implicit 
<glossterm>primary input port</glossterm> named “<port>source</port>”
and an implicit <glossterm>primary output port</glossterm> named
“<port>result</port>”. Any input or output ports that the <tag>p:pipeline</tag>
declares explicitly are <emphasis>in addition</emphasis> to those ports
and may not be declared primary.</para>

<e:rng-pattern name="Pipeline"/>

<para>Viewed from the outside, a <tag>p:pipeline</tag> is a
black box which performs some calculation on its inputs and produces
its outputs. From the pipeline author's perspective, the computation
performed by the pipeline is described in terms of <glossterm>contained
steps</glossterm> which read the pipeline's inputs and produce
the pipeline's outputs.</para>

<para>If a pipeline does not have a <tag class="attribute">type</tag>
then that pipeline cannot be invoked as a step.</para>

<para>The <tag>p:pipeline</tag> element is just a simplified form of
step declaration. A document that reads:</para>

<programlisting>&lt;p:pipeline <replaceable>some-attributes</replaceable>&gt;
  <replaceable>some-content</replaceable>
&lt;/p:pipeline&gt;</programlisting>

<para>can be interpreted as if it read:</para>

<programlisting>&lt;p:declare-step <replaceable>some-attributes</replaceable>&gt;
  &lt;p:input port='source' primary='true'/&gt;
  &lt;p:input port='paramters' kind='parameters' primary='true'/&gt;
  &lt;p:output port='result' primary='true'&gt;
  <replaceable>some-content</replaceable>
&lt;/p:declare-step&gt;</programlisting>

<para>See <tag>p:declare-step</tag> for more details.</para>

<section xml:id="example-pipeline" role="tocsuppress">
<title>Example</title>

<para>A pipeline might accept a document as input;
perform XInclude, validation, and transformation; and produce the
transformed document as its output.</para>

<example xml:id="ex.p.pipeline">
<title>A Sample Pipeline Document</title>
<programlisting>&lt;p:pipeline xmlns:p="http://www.w3.org/ns/xproc"&gt;

&lt;p:xinclude/&gt;

&lt;p:validate-with-xml-schema&gt;
  &lt;p:input port="schema"&gt;
    &lt;p:document href="http://example.com/path/to/schema.xsd"/&gt;
  &lt;/p:input&gt;
&lt;/p:validate-with-xml-schema&gt;

&lt;p:xslt&gt;
  &lt;p:input port="stylesheet"&gt;
    &lt;p:document href="http://example.com/path/to/stylesheet.xsl"/&gt;
  &lt;/p:input&gt;
&lt;/p:xslt&gt;

&lt;/p:pipeline&gt;
</programlisting>
</example>
</section>
</section>

<section xml:id="p.for-each">
<title>p:for-each</title>

<para>A for-each is specified by the <tag>p:for-each</tag> element. It
is a <glossterm>compound step</glossterm> that processes a sequence of
documents, applying its <glossterm>subpipeline</glossterm> to each
document in turn.</para>

<e:rng-pattern name="ForEach"/>

<para>When a pipeline needs to process a sequence of documents using a
subpipeline that only processes a single document, the
<tag>p:for-each</tag> construct can be used as a wrapper around that
subpipeline. The <tag>p:for-each</tag> will apply that subpipeline to
each document in the sequence in turn.</para>

<para>The result of the <tag>p:for-each</tag> is a sequence of
documents produced by processing each individual document in the input
sequence.
If the <tag>p:for-each</tag> has one or more output ports, what appears on
each of those ports is the sequence of documents that is the
concatenation of the sequence produced by each iteration of the
loop on the port to which it is connected. If the iteration source
for a <tag>p:for-each</tag> is an empty sequence, then the subpipeline is
never run and an empty sequence
is produced on all of the outputs.
</para>

<para>The <tag>p:iteration-source</tag> is an anonymous input:
its <glossterm>binding</glossterm>
provides a sequence of documents to the <tag>p:for-each</tag>
step. If no iteration sequence is explicitly provided, then the
iteration source is read from the
<glossterm>default readable port</glossterm>.</para>

<para>The processor provides each document, one at a time, to the
<glossterm>subpipeline</glossterm> represented by the children of the
<tag>p:for-each</tag> on a port named
<code>current</code>.</para>

<para>For each declared output, the processor collects all the
documents that are produced for that output from all the iterations,
in order, into a sequence. The result of the <tag>p:for-each</tag> on
that output is that sequence of documents.</para>

<!--
<para>The <glossterm>environment</glossterm> of a <tag>p:for-each</tag> is its
inherited environment
with the <link linkend="dt-standard-modifications">standard modifications</link>.</para>
-->

<para>The environment inherited by the <glossterm>contained steps</glossterm>
of a <tag>p:for-each</tag> is the <glossterm>inherited environment</glossterm>
with these modifications:</para>

<itemizedlist>
<listitem>
<para>The port named “<code>current</code>” on the <tag>p:for-each</tag> is
added to the <glossterm>readable ports</glossterm>.</para>
</listitem>
<listitem>
<para>The port named “<code>current</code>” on the <tag>p:for-each</tag> is
made the <glossterm>default readable port</glossterm>.</para>
</listitem>
</itemizedlist>

<para>If the <tag>p:for-each</tag> has a <glossterm>primary output
port</glossterm> (explicit or
<link linkend="primary-input-output">supplied by default</link>)
and that port has no <glossterm>binding</glossterm>,
then it is bound to the <glossterm>primary output port</glossterm> of
the <glossterm>last step</glossterm> in the
<glossterm>subpipeline</glossterm>. <error code="S0006">It is a
<glossterm>static error</glossterm> if the primary output port has no
binding and the <glossterm>last step</glossterm> in the subpipeline
does not have a primary output port.</error></para>

<para>Note that outputs declared for a <tag>p:for-each</tag> serve a
dual role. Inside the <tag>p:for-each</tag>, they are used to read
results from the subpipeline. Outside the <tag>p:for-each</tag>, they
provide the aggregated results.</para>

<para>The <tag class="attribute">sequence</tag> attribute on a
<tag>p:output</tag> inside a <tag>p:for-each</tag> only applies inside the
step. From the outside, all of the outputs produce sequences.</para>

<section xml:id="for-each-xpath-context">
<title>XPath Context</title>

<para>Within a <tag>p:for-each</tag>, the
<function>p:iteration-position</function> and
<function>p:iteration-size</function>
are taken from the sequence of documents that will be processed by the
<tag>p:for-each</tag>. The total number of documents is the
<function>p:iteration-size</function>; the
ordinal value of the current document (the document appearing on the
<literal>current</literal> port) is the
<function>p:iteration-position</function>.</para>


<note xml:id="impl1">
<title>Note to implementers</title>
<para>In the case where no XPath expression that must
be evaluated by the processor makes any reference to
<function>p:iteration-size</function>, its value
does not actually have to be calculated (and the entire
input sequence does not, therefore, need to be buffered so that its size can
be calculated before processing begins).</para>
</note>
</section>

<section xml:id="example-for-each" role="tocsuppress">
<title>Example</title>

<para>A <tag>p:for-each</tag> might accept a sequence of chapters as its input,
process each chapter in turn with XSLT, a step that accepts only a
single input document, and produce a sequence of formatted chapters as
its output.</para>

<example xml:id="ex.p.for-each">
<title>A Sample For-Each</title>
<programlisting>&lt;p:for-each name="chapters"&gt;
  &lt;p:iteration-source select="//chapter"/&gt;
  &lt;p:output port="html-results"&gt;
    &lt;p:pipe step="make-html" port="result"/&gt;
  &lt;/p:output&gt;
  &lt;p:output port="fo-results"&gt;
    &lt;p:pipe step="make-fo" port="result"/&gt;
  &lt;/p:output&gt;

  &lt;p:xslt name="make-html"&gt;
    &lt;p:input port="stylesheet"&gt;
      &lt;p:document href="http://example.com/xsl/html.xsl"/&gt;
    &lt;/p:input&gt;
  &lt;/p:xslt&gt;

  &lt;p:xslt name="make-fo"&gt;
    &lt;p:input port="source"&gt;
      &lt;p:pipe step="chapters" port="current"/&gt;
    &lt;/p:input&gt;
    &lt;p:input port="stylesheet"&gt;
      &lt;p:document href="http://example.com/xsl/fo.xsl"/&gt;
    &lt;/p:input&gt;
  &lt;/p:xslt&gt;
&lt;/p:for-each&gt;
</programlisting>
</example>

<para>The <code>//chapter</code> elements of the document are
selected. Each chapter is transformed into HTML and XSL Formatting Objects using an
XSLT step. The resulting HTML and FO documents are aggregated together
and appear on the <literal>html-results</literal> and <literal>fo-results</literal>
ports, respectively, of the <literal>chapters</literal> step itself.</para>
</section>
</section>

<section xml:id="p.viewport">
<title>p:viewport</title>

<para>A viewport is specified by the <tag>p:viewport</tag> element. It
is a <glossterm>compound step</glossterm> that processes a single
document, applying its <glossterm>subpipeline</glossterm> to one or
more subtrees of the document.
</para>

<e:rng-pattern name="Viewport"/>

<para>The result of the <tag>p:viewport</tag> is a copy of the original
document where the selected subtrees have been replaced by the results of
applying the subpipeline to them.</para>

<para>The <tag>p:viewport-source</tag> is an anonymous input: its
<glossterm>binding</glossterm> provides a single document to the
<tag>p:viewport</tag> step. If no document is explicitly provided,
then the viewport source is read from the <glossterm>default readable
port</glossterm>. <error code="D0003">It is a <glossterm>dynamic
error</glossterm> if the viewport source does not provide exactly one
document. </error></para>

<para>The <tag class="attribute">match</tag> attribute specifies an
XSLT match pattern. Each matching node in the source document
is wrapped in a document node, as necessary, and provided, one at a time, to the viewport's
<glossterm>subpipeline</glossterm> on a port named <port>current</port>.  The base
URI of the resulting document that is passed to the subpipeline is the base URI of the 
matched element or document. 
<error code="D0010">It is a <glossterm>dynamic error</glossterm>
if the <tag class="attribute">match</tag> expression on
<tag>p:viewport</tag> does not match an element or document.</error>
</para>

<para>After a match is found, the entire subtree rooted at that match
is processed as a unit. No further attempts are made to match nodes
among the descendants of any matched node.</para>

<para>The environment inherited by the <glossterm>contained steps</glossterm>
of a <tag>p:viewport</tag> is the <glossterm>inherited environment</glossterm>
with these modifications:</para>

<itemizedlist>
<listitem>
<para>The port named “<code>current</code>” on the <tag>p:viewport</tag> is
added to the <glossterm>readable ports</glossterm>.</para>
</listitem>
<listitem>
<para>The port named “<code>current</code>” on the <tag>p:viewport</tag> is
made the <glossterm>default readable port</glossterm>.</para>
</listitem>
</itemizedlist>

<para>The <tag>p:viewport</tag> must contain a single,
<glossterm>primary output port</glossterm>
explicit declared explicitly or
<link linkend="primary-input-output">supplied by default</link>.
If that
port has no <glossterm>binding</glossterm>, then it is
bound to the <glossterm>primary output port</glossterm> of
the <glossterm>last step</glossterm> in the <glossterm>subpipeline</glossterm>.
<error code="S0006">It is a <glossterm>static error</glossterm> if the
primary output port has no binding and the <glossterm>last
step</glossterm> in the subpipeline does not have a primary output
port.</error></para>

<para>What appears on the output from the <tag>p:viewport</tag> will
be a copy of the input document where each matching node is
replaced by the result of applying the subpipeline to the subtree
rooted at that node.
In other words, if the match pattern matches a particular element
then that element is wrapped in a document node and
provided on the <port>current</port> port,
the subpipeline in the <tag>p:viewport</tag> is evaluated, and the result
that appears on the <port>output</port> port replaces the matched element.
</para>

<para>If no documents appear on the <port>output</port> port, the matched
element will effectively be deleted. If exactly one document appears, the
contents of that document will replace the matched element. If a sequence
of documents appears, then the contents of each document in that sequence
(in the order it appears in the sequence)
will replace the matched element.</para>

<para>The output of the <tag>p:viewport</tag> itself is a single document
that appears on a port named “<literal>result</literal>”. Note that
the semantics of <tag>p:viewport</tag> are special.
The <port>output</port> port in the
<tag>p:viewport</tag> is used only to access the results of the subpipeline.
The output of the step itself appears on a port with the fixed name
“<literal>result</literal>” that is never explicitly declared.</para>

<section xml:id="viewport-xpath-context">
<title>XPath Context</title>

<para>Within a <tag>p:viewport</tag>, the
<function>p:iteration-position</function> and
<function>p:iteration-size</function>
are taken from the sequence of documents that will be processed by the
<tag>p:viewport</tag>. The total number of documents is the
<function>p:iteration-size</function>; the
ordinal value of the current document (the document appearing on the
<literal>current</literal> port) is the
<function>p:iteration-position</function>.</para>

<note xml:id="impl2">
<title>Note to implementers</title>
<para>In the case where no XPath expression that must
be evaluated by the processor makes any reference to
<function>p:iteration-size</function>, its value
does not actually have to be calculated (and the entire
input sequence does not, therefore, need to be buffered so that its size can
be calculated before processing begins).</para>
</note>
</section>

<section xml:id="example-viewport" role="tocsuppress">
<title>Example</title>

<para>A <tag>p:viewport</tag> might accept an XHTML document as its input,
add an <tag>hr</tag> element at the beginning of all <tag>div</tag> elements that
have the class value “chapter”, 
and return an XHTML document that is the same as the original except
for that change.</para>

<example xml:id="ex.p.viewport">
<title>A Sample Viewport</title>
<programlisting>&lt;p:viewport match="h:div[@class='chapter']"
            xmlns:h="http://www.w3.org/1999/xhtml"&gt;
  &lt;p:insert position="first-child"&gt;
    &lt;p:input port="insertion"&gt;
      &lt;p:inline&gt;
        &lt;hr xmlns="http://www.w3.org/1999/xhtml"/&gt;
      &lt;/p:inline&gt;
    &lt;/p:input&gt;
  &lt;/p:insert&gt;
&lt;/p:viewport&gt;
</programlisting>
</example>

<para>The nodes which match <code>h:div[@class='chapter']</code>
in the input document are selected.
An <code>hr</code> is inserted as the first child of each <code>h:div</code>
and the resulting version replaces the original <code>h:div</code>.
The result of the whole step is
a copy of the input document with a horizontal rule as the first child of each
selected <code>h:div</code>.</para>
</section>
</section>

<section xml:id="p.choose">
<title>p:choose</title>

<para>A choose is specified by the <tag>p:choose</tag> element. It
is a <glossterm>multi-container step</glossterm> that
selects exactly one of a list of alternative
<glossterm baseform="subpipeline">subpipelines</glossterm> based on the
evaluation of XPath expressions.</para>

<e:rng-pattern name="Choose"/>

<para>A <tag>p:choose</tag> has no inputs. It contains an arbitrary number of
alternative <glossterm baseform="subpipeline">subpipelines</glossterm>,
exactly one of which
will be evaluated.</para>

<para>The list of alternative subpipelines consists of zero or more
subpipelines guarded by an XPath expression, followed optionally by a
single default subpipeline.</para>

<para>The <tag>p:choose</tag> considers each subpipeline in turn and selects
the first (and only the first) subpipeline for which the guard
expression evaluates to true in its context.
If there are no subpipelines for which the expression evaluates to
true, the default subpipeline, if it was specified, is
selected.</para>

<para>After a <glossterm>subpipeline</glossterm> is selected, it is
evaluated as if only it had been present.</para>

<para>The outputs of the <tag>p:choose</tag> are taken from the outputs
of the selected <glossterm>subpipeline</glossterm>. The <tag>p:choose</tag>
has the same number of outputs as the selected subpipeline with the same
names. If the selected subpipeline has a <glossterm>primary output
port</glossterm>, the port with the same name on the <tag>p:choose</tag>
is also a primary output port.</para>

<para>In order to ensure that the output of the <tag>p:choose</tag> is
consistent irrespective of the <glossterm>subpipeline</glossterm> chosen,
each <glossterm>subpipeline</glossterm> must
declare the same number of outputs with the same names and the same settings
with respect to sequences. If any of the subpipelines
specifies a <glossterm>primary output port</glossterm>, each subpipeline must
specify exactly the same output as primary.
<error code="S0007">It is a 
<glossterm>static error</glossterm> if two
<glossterm baseform="subpipeline">subpipelines</glossterm>
in a <tag>p:choose</tag> declare different outputs.</error></para>

<para><error code="D0004">It is a <glossterm>dynamic error</glossterm>
if no <glossterm>subpipeline</glossterm> is selected by the <tag>p:choose</tag>
and no default is provided.</error></para>

<para>The <tag>p:choose</tag> can specify the context node against
which the XPath
expressions that occur on each branch are evaluated. The context node
is specified as a <glossterm>binding</glossterm> for the
<tag>p:xpath-context</tag>. If no binding is provided, the default
<tag>p:xpath-context</tag> is the document on the <glossterm>default
readable port</glossterm>.
</para>

<para>Each conditional <glossterm>subpipeline</glossterm> is
represented by a <tag>p:when</tag> element. The default branch is
represented by a <tag>p:otherwise</tag> element.</para>

<section xml:id="p.xpath-context">
<title>p:xpath-context</title>

<para>A <tag>p:xpath-context</tag> element specifies the context
against which an XPath expression will be evaluated. When it appears
in a <tag>p:when</tag>, it specifies the context for that
<tag>p:when</tag>’s <tag class="attribute">test</tag> attribute. When
it appears in <tag>p:choose</tag>, it specifies the default context
for all of the <tag>p:when</tag> elements in that
<tag>p:choose</tag>.</para>

<e:rng-pattern name="XPathContext"/>

<para>Only one <glossterm>binding</glossterm> is allowed and it works
the same way that bindings work on a <tag>p:input</tag>.
No <tag class="attribute">select</tag> expression is allowed.
<error code="D0005">It is a <glossterm>dynamic error</glossterm>
if the <tag>xpath-context</tag> is bound to a sequence of
documents.</error></para>

<para>In an XPath 1.0 implementation, if the context node is bound to
<tag>p:empty</tag>, or is unbound and the <glossterm>default readable
port</glossterm> is undefined, an <link linkend="empty-xpath-context">empty document node</link> is used
instead as the context. In an XPath 2.0 implementation, the context item
is undefined.</para>
</section>

<section xml:id="p.when">
<title>p:when</title>

<para>A when specifies one subpipeline guarded by a test expression.
</para>

<e:rng-pattern name="When"/>

<para>Each <tag>p:when</tag> branch of the <tag>p:choose</tag> has a
<tag class="attribute">test</tag> attribute which
<rfc2119>must</rfc2119> contain an XPath
expression. That XPath expression's effective boolean value is the
guard for the <glossterm>subpipeline</glossterm> contained
within that <tag>p:when</tag>.</para>

<para>The <tag>p:when</tag> can specify a context node against which
its <tag class="attribute">test</tag> expression is to be evaluated.
That context node is specified as a <glossterm>binding</glossterm>
for the <tag>p:xpath-context</tag>.
If no context is specified on the <tag>p:when</tag>, the context
of the <tag>p:choose</tag> is used.</para>
</section>

<section xml:id="p.otherwise">
<title>p:otherwise</title>

<para>An otherwise specifies the default branch; the subpipeline selected if
no test expression on any preceding <tag>p:when</tag> evaluates to true.</para>

<e:rng-pattern name="Otherwise"/>
</section>

<!--
<para>The <glossterm>environment</glossterm> of the selected subpipeline is the
inherited environment
with the <link linkend="dt-standard-modifications">standard modifications</link>.</para>

<para>The environment inherited by its <glossterm>contained steps</glossterm>
is the <link linkend="dt-standard-inheritance">standard inheritance</link>.
</para>
-->

<!--
<para>If there is no <glossterm>binding</glossterm> for
the <glossterm>primary output port</glossterm> of the selected subpipeline,
then it is bound to the <glossterm>primary output port</glossterm> of
the <glossterm>last step</glossterm> in the selected subpipeline.
<error code="S0006">It is a <glossterm>static error</glossterm> if the
primary output port has no binding and the last step in the selected subpipeline does
not have a primary output port.</error></para>
-->

<section xml:id="example-choose" role="tocsuppress">
<title>Example</title>

<para>A <tag>p:choose</tag> might test the version attribute of the document
element and validate with an appropriate schema.</para>

<example xml:id="ex.p.choose">
<title>A Sample Choose</title>
<programlisting>&lt;p:choose name="version"&gt;
  &lt;p:when test="/*[@version = 2]"&gt;
    &lt;p:validate-with-xml-schema&gt;
      &lt;p:input port="schema"&gt;
	&lt;p:document href="v2schema.xsd"/&gt;
      &lt;/p:input&gt;
    &lt;/p:validate-with-xml-schema&gt;
  &lt;/p:when&gt;

  &lt;p:when test="/*[@version = 1]"&gt;
    &lt;p:validate-with-xml-schema&gt;
      &lt;p:input port="schema"&gt;
	&lt;p:document href="v1schema.xsd"/&gt;
      &lt;/p:input&gt;
    &lt;/p:validate-with-xml-schema&gt;
  &lt;/p:when&gt;

  &lt;p:when test="/*[@version]"&gt;
    &lt;p:identity/&gt;
  &lt;/p:when&gt;

  &lt;p:otherwise&gt;
    &lt;p:output port="result"&gt;
      &lt;!-- this output is necessary so that all the branches have
           the same outputs; it'll never really matter because
	   we're just about to raise an error. --&gt;
      &lt;p:inline&gt;
	&lt;nop/&gt;
      &lt;/p:inline&gt;
    &lt;/p:output&gt;
    &lt;p:error code="NOVERSION"&gt;
      &lt;p:input port="source"&gt;
	&lt;p:inline&gt;
	  &lt;message&gt;Required version attribute missing.&lt;/message&gt;
	&lt;/p:inline&gt;
      &lt;/p:input&gt;
    &lt;/p:error&gt;
  &lt;/p:otherwise&gt;
&lt;/p:choose&gt;
</programlisting>
</example>
</section>
</section>

<section xml:id="p.group">
<title>p:group</title>

<para>A group is specified by the <tag>p:group</tag> element. In a 
<tag>p:try</tag>, it is a non-step wrapper,
everywhere else, it is a <glossterm>compound step</glossterm>.
A group
encapsulates the behavior of its <glossterm>subpipeline</glossterm>.</para>

<e:rng-pattern name="Group"/>

<para>A <tag>p:group</tag> is a convenience wrapper for a collection of steps.
</para>

<section xml:id="example-group" role="tocsuppress">
<title>Example</title>

<example xml:id="ex.p.group">
<title>An Example Group</title>
<programlisting>&lt;p:group&gt;
  &lt;p:variable name="db-key"
	      select="'some-long-string-of-nearly-random-characters'"/&gt;

  &lt;p:choose&gt;
    &lt;p:when test="/config/output = 'fo'"&gt;
      &lt;p:xslt&gt;
	&lt;p:with-param name="key" select="$db-key"/&gt;
	&lt;p:input port="stylesheet"&gt;
	  &lt;p:document href="fo.xsl"/&gt;
	&lt;/p:input&gt;
      &lt;/p:xslt&gt;
    &lt;/p:when&gt;
    &lt;p:when test="/config/output = 'svg'"&gt;
      &lt;p:xslt&gt;
	&lt;p:with-param name="key" select="$db-key"/&gt;
	&lt;p:input port="stylesheet"&gt;
	  &lt;p:document href="svg.xsl"/&gt;
	&lt;/p:input&gt;
      &lt;/p:xslt&gt;
    &lt;/p:when&gt;
    &lt;p:otherwise&gt;
      &lt;p:xslt&gt;
	&lt;p:with-param name="key" select="$db-key"/&gt;
	&lt;p:input port="stylesheet"&gt;
	  &lt;p:document href="html.xsl"/&gt;
	&lt;/p:input&gt;
      &lt;/p:xslt&gt;
    &lt;/p:otherwise&gt;
  &lt;/p:choose&gt;
&lt;/p:group&gt;
</programlisting>
</example>
</section>
</section>

<section xml:id="p.try">
<title>p:try</title>

<para>A try/catch is specified by the <tag>p:try</tag> element. It
is a <glossterm>multi-container step</glossterm> that
isolates a <glossterm>subpipeline</glossterm>, preventing any dynamic errors
that arise within it from being exposed to the rest of the
pipeline.</para>

<e:rng-pattern name="Try"/>

<para>The <tag>p:group</tag> represents the initial subpipeline and
the recovery (or “catch”) pipeline is identified with a
<tag>p:catch</tag> element.</para>

<para>The <tag>p:try</tag> step evaluates the initial subpipeline and,
if no errors occur, the outputs of that pipeline are the outputs of
the <tag>p:try</tag> step. However, if any errors occur, the
<tag>p:try</tag> abandons the first subpipeline, discarding any output
that it might have generated, and evaluates the recovery
subpipeline.</para>

<para>If the recovery subpipeline is evaluated, the outputs of the
recovery subpipeline are the outputs of the <tag>p:try</tag> step. If
the recovery subpipeline is evaluated and a step within that
subpipeline fails, the <tag>p:try</tag> fails.</para>

<para>The outputs of the <tag>p:try</tag> are taken from the outputs
of the initial subpipeline or the recovery subpipeline if an error occurred
in the initial subpipeline. The <tag>p:try</tag>
has the same number of outputs as the selected subpipeline with the same
names. If the selected subpipeline has a <glossterm>primary output
port</glossterm>, the port with the same name on the <tag>p:try</tag>
is also a primary output port.</para>

<para>In order to ensure that the output of the <tag>p:try</tag> is consistent
irrespective of whether the initial subpipeline provides its output or
the recovery subpipeline does, both subpipelines must declare the same
number of outputs with the same names and the same settings with respect to
sequences.
If either of the subpipelines
specifies a <glossterm>primary output port</glossterm>, both subpipelines must
specify exactly the same output as primary.
<error code="S0009">It is a <glossterm>static
error</glossterm> if the <tag>p:group</tag> and <tag>p:catch</tag> subpipelines
declare different outputs.</error></para>

<para>A pipeline author can cause an error to occur with the 
<tag>p:error</tag> step.</para>


<!--
<para>The <glossterm>environment</glossterm> of a <tag>p:try</tag> is its
inherited environment with the <link linkend="dt-standard-modifications">standard
modifications</link>.</para>

<para>The environment inherited by its initial subpipeline, the
<tag>p:group</tag>, is the environment of the <tag>p:try</tag>.</para>
-->

<para>The recovery subpipeline of a <tag>p:try</tag>
is identified with a <tag xml:id="p.catch">p:catch</tag>:</para>

<e:rng-pattern name="Catch"/>

<!--
<para>The environment of a <tag>p:catch</tag> is the environment of
its containing <tag>p:try</tag>.</para>
-->

<para>The environment inherited by the <glossterm>contained
steps</glossterm> of the <tag>p:catch</tag> is the <glossterm>inherited
environment</glossterm> with
this modification:</para>

<itemizedlist>
<listitem>
<para>The port named “<code>error</code>” on the <tag>p:catch</tag> is
added to the <glossterm>readable ports</glossterm>.</para>
</listitem>
</itemizedlist>

<para>What appears on the <code>error</code> output port is an
<link linkend="err-vocab">error document</link>. The error
document may contain messages generated by steps that were part of the initial
subpipeline. Not all messages that appear are indicative of errors; for example,
it is common for all <tag>xsl:message</tag> output from the XSLT component to
appear on the <code>error</code> output port. It is possible that the component which
fails may not produce any messages at all. It is also possible that the failure
of one component may cause others to fail so that there may be multiple failure messages
in the document.</para>

<section xml:id="err-vocab">
<title>The Error Vocabulary</title>

<para>In general, it is very difficult to predict error behavior. Step failure
may be catastrophic (programmer error), or it may be be the result of user error,
resource failures, etc. Steps may detect more than one error, and the failure of
one step may cause other steps to fail as well.</para>

<para>The <tag>p:try</tag>/<tag>p:catch</tag> mechanism gives pipeline authors
the opportunity to process the errors that caused the <tag>p:try</tag> to fail.
In order to facilitate some modicum of interoperability among processors, errors
that are reported on the <literal>error</literal> output port of a
<tag>p:catch</tag> <rfc2119>should</rfc2119> conform to the format described here.
</para>

<section xml:id="cv.errors">
<title>c:errors</title>

<para>The error vocabulary consists of a root element, <tag>c:errors</tag>
which contains zero or more <tag>c:error</tag> elements.</para>

<e:rng-pattern name="Errors"/>
</section>

<section xml:id="cv.error">
<title>c:error</title>

<para>Each specific error is represented by an <tag>c:error</tag> element:</para>

<e:rng-pattern name="Error"/>

<para>The <tag class="attribute">name</tag> and <tag class="attribute">type</tag>
attributes identify the name and type, respectively, of the step which failed.</para>

<para>The <tag class="attribute">code</tag> is a QName which identifies the error.
For steps which have defined error codes, this is an opportunity for the step
to identify the error in a machine-processable fashion. Many steps omit this
because they do not include the concept of errors identified by QNames.</para>

<para>If the error was caused by a specific document, or by the location of some
erroneous construction in a specific document, the
<tag class="attribute">href</tag>, <tag class="attribute">line</tag>,
<tag class="attribute">column</tag>, and <tag class="attribute">offset</tag>
attributes identify this location. Generally, the error location is identified
either with line and column numbers or with an offset from the beginning of
the document, but not usually both.</para>

<para>The content of the <tag>c:error</tag> element is any well-formed XML.
Specific steps, or specific implementations, may provide more detail about the
format of the content of an error message.</para>
</section>

<section xml:id="error-example">
<title>Error Example</title>

<para>Consider the following XSLT stylesheet:</para>

<programlisting><![CDATA[<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

<xsl:template match="/">
  <xsl:message terminate="yes">
    <xsl:text>This styl