<?xml version="1.0" encoding="UTF-8"?>
<specification xmlns="http://docbook.org/ns/docbook"
               xmlns:xlink="http://www.w3.org/1999/xlink"
               xmlns:xi="http://www.w3.org/2001/XInclude"
               xmlns:p="http://www.w3.org/ns/xproc"
               xmlns:e="http://www.w3.org/1999/XSL/Spec/ElementSyntax"
               class="note" version="5.0-extension w3c-xproc">
  <info><title>Document Templating Steps for XProc</title>
    <w3c-shortname>xproc-template</w3c-shortname>
    <pubdate>2011-01-25</pubdate>
    <bibliorelation type="isformatof" xlink:href="xproc-template.xml">XML</bibliorelation>
    <authorgroup>
      <author>
        <personname>Norman Walsh</personname>
        <affiliation>
          <orgname>MarkLogic Corporation</orgname>
        </affiliation>
        <email>norman.walsh@marklogic.com</email>
      </author>
    </authorgroup>
<abstract>
<para>This note describes two new XProc steps designed to make it
easier to construct documents within an XProc pipeline using values
computed by that pipeline.</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 is the first publication of this document as a Working
Group Note. This document is a product of the
<link xlink:href="http://www.w3.org/XML/Processing/">XML Processing Model
Working Group</link>
as part of the W3C
<link xlink:href="http://www.w3.org/XML/Activity">XML Activity</link>.
The English version of this
specification is the only normative version. However, for translations
of this document, see
<link xlink:href="http://www.w3.org/2003/03/Translations/byTechnology?technology=xproc-template"/>.
</para>

<para>This Note defines some additional optional steps for use in XProc
pipelines. The XML Processing Model Working Group expects that these
new steps will be widely implemented and used.</para>

<para>Please report errors in this document to the public
mailing list
<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>Publication as a Working Group Note 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 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>It's quite common in
<biblioref linkend="xproc"/> to construct documents using values computed by the pipeline.
This is particularly (but not exclusively) the case when the pipeline
uses the <tag>p:http-request</tag> step. The input to
<tag>p:http-request</tag> is a <tag>c:request</tag> document;
attributes on the <tag>c:request</tag> element control most of the request parameters; the
body of the document forms the body of request.</para>

<para>A typical example looks like this:</para>

<programlisting><![CDATA[<c:request method="POST" href="http://example.com/post"
           username="user" password="password">
<c:body>
  <computed-content/>
</c:body>
</c:request>]]></programlisting>

<para>If we assume that the <tag class="attribute">href</tag> value and the computed
content come from an input document, and the username and password are options, then a
typical pipeline to compute the request becomes quite complex.</para>

<programlisting><xi:include href="examples/doctemp-1.xml" parse="text"/></programlisting>

<para>There's nothing wrong with this pipeline, but it requires
several steps to accomplish with the pipeline author probably
considers a single operation. What's more, the result of these steps
is not immediately obvious on casual inspection.</para>

<para>In order to make this simple construction case both literally
and conceptually simpler, this note introduces two new XProc steps in
the XProc namespace. Support for these steps is optional, but we
strongly encourage implementors to provide them.</para>

<para>The new steps are <tag>p:in-scope-names</tag> and
<tag>p:template</tag>. Taken together, they greatly simplify
the pipeline:</para>

<programlisting><xi:include href="examples/doctemp-2.xml" parse="text"/></programlisting>

<para>The <tag>p:in-scope-names</tag> step provides all of the in-scope options and variables
in a <tag>c:param-set</tag> (this operation is exactly analagous to what the
<tag>p:parameters</tag> step does, except that it operates on the options and variables instead
of on parameters).</para>

<para>The <tag>p:template</tag> step searches for XPath
expressions, delimited by curly braces, in a template document and replaces each with the
result of evaluating the expression. All of the parameters passed to the
<tag>p:template</tag> step are available as in-scope variable names when evaluating
each XPath expression.</para>

<para>Where the expressions occur in attribute values, their string value is used. Where
they appear in text content, their node values are used.</para>
</section>

<!-- ************************************************************************-->

<section xml:id="terminology">
<title>Terminology</title>

<para>In this note the words <rfc2119>must</rfc2119>,
<rfc2119>must not</rfc2119>, <rfc2119>should</rfc2119>,
<rfc2119>should not</rfc2119>, <rfc2119>may</rfc2119> and
<rfc2119>recommended</rfc2119> are to be interpreted as described in
<biblioref linkend="rfc2119"/>.</para>

</section>

<!-- ************************************************************************-->

<section xml:id="c.in-scope-names" role="ext-optional">
<title>p:in-scope-names</title>

<para>The <code>p:in-scope-names</code> step exposes all of the
in-scope variables and options as a set of parameters in a
<tag>c:param-set</tag> document.</para>

<p:declare-step type="p:in-scope-names">
  <p:output port="result" primary="false"/>
</p:declare-step>

<para>Each in-scope variable and option is converted into a
<tag>c:param</tag> element.
The resulting <tag>c:param</tag> elements are wrapped in a 
<tag>c:param-set</tag> and the parameter set document is written
to the <port>result</port> port.
<impl>The
order in which <tag>c:param</tag> elements occur in the <tag>c:param-set</tag> is
<glossterm>implementation-dependent</glossterm>.</impl></para>

<para>For consistency and user convenience, if any of the variables or options
have names that are in a namespace, the
<tag class="attribute">namespace</tag> attribute on the
<tag>c:param</tag> element <rfc2119>must</rfc2119> be used. Each
<tag class="attribute">name</tag> <rfc2119>must</rfc2119> be an NCName.</para>

<para>The base URI of the output document is the URI of the pipeline document
that contains the step.</para>

<para>For consistency with the <tag>p:parameters</tag> step, the
<port>result</port> port is not primary.</para>

<section xml:id="ex-in-scope-names" role="tocsuppress">
<title>Example</title>

<para>This unlikely pipeline demonstrates the behavior of <tag>p:in-scope-names</tag>:
</para>

<programlisting><xi:include href="examples/doctemp-3.xml" parse="text"/></programlisting>

<para>Assuming the values supplied for the username and password
options are “<literal>user</literal>” and “<literal>pass</literal>”,
respectively, the output would be:</para>

<programlisting><xi:include href="examples/doctemp-3-result.xml" parse="text"/></programlisting>

</section>
</section>

<section xml:id="c.template" role="ext-optional">
<title>p:template</title>

<para>The <code>p:template</code> replaces each XPath expression, delimited with
curly braces, in the <port>template</port> document with the result of evaluating that
expression.</para>

<p:declare-step type="p:template">
  <p:input port="template"/>
  <p:input port="source" sequence="true" primary="true"/>
  <p:input port="parameters" kind="parameter"/>
  <p:output port="result"/>
</p:declare-step>

<para>While evaluating each expression, the names of any parameters passed to the
step are available as variable values in the XPath dynamic context.</para>

<para>The step searches for XPath expressions in attribute values,
text content (adjacent text nodes, if they occur in the data model,
must be coalesced; this step always processes maximal length text
nodes), processing instruction data, and comments. XPath expressions
are identified by curly braces, similar to attribute value templates
in XSLT or enclosed expressions in XQuery.</para>

<para>In order to allow curly braces to appear literally in content, they can be escaped
by doubling them. In other words, where “<code>{</code>” would start an XPath expression,
“<code>{{</code>” is simply a <emphasis>single</emphasis>, literal opening curly brace.
The same applies for closing curly braces.</para>

<para>Inside an XPath expression, strings quoted by single (<code>'</code>) or
double (<code>"</code>) quotes are treated literally. Outside of quoted text, it
is an error for an opening curly brace to occur. A closing curly brace ends the
XPath expression (whether or not it is followed immediately by another closing
curly brace).</para>

<para>These parsing rules can be described by the following algorithm, though implementations
are by no means required to implement the parsing in exactly this way, provided that they
achieve the same results.</para>

<itemizedlist>
<listitem><para>The parser begins in <emphasis>regular-mode</emphasis> at the start of
each unit of content where expansion may occur. In <emphasis>regular-mode</emphasis>:
</para>
<orderedlist>
<listitem><para>“<code>{{</code>” is replaced by a single “<code>{</code>”.</para></listitem>
<listitem><para>“<code>}}</code>” is replaced by a single “<code>}</code>”.
</para>
<para>Note:
<error code="C0067">It is a <glossterm>dynamic error</glossterm> to
encounter a single closing curly brace “<code>}</code>” that is not immediately
followed by another closing curly brace.</error>
</para></listitem>
<listitem><para>A single opening curly brace “<code>{</code>” (not
immediately followed by another opening curly brace) is discarded and
the parser moves into <emphasis>xpath-mode</emphasis>. The inital expression is empty.
</para></listitem>
<listitem><para>All other characters are copied without change.</para></listitem>
</orderedlist>
</listitem>
<listitem>
<para>In <emphasis>xpath-mode</emphasis>:</para>
<orderedlist>
<listitem><para><error code="C0067">It is a <glossterm>dynamic error</glossterm> to
encounter an opening curly brace “<code>{</code>”.</error></para></listitem>
<listitem><para>A closing curly brace “<code>}</code>” is discarded and ends the
expression. The expression is evaluated and the result of that
evaluation is copied to the output. The parser returns to
<emphasis>regular-mode</emphasis>.</para>
<para>Note: Braces cannot be escaped by doubling them
in <emphasis>xpath-mode</emphasis>.</para></listitem>
<listitem><para>A single quote (<code>'</code>) is added to the current expression and
the parser moves to <emphasis>single-quote-mode</emphasis>.</para></listitem>
<listitem><para>A double quote (<code>"</code>) is added to the current expression and
the parser moves to <emphasis>double-quote-mode</emphasis>.</para></listitem>
<listitem><para>All other characters are appended to the current expression.</para></listitem>
</orderedlist>
</listitem>
<listitem>
<para>In <emphasis>single-quote-mode</emphasis>:</para>
<orderedlist>
<listitem><para>A single quote (<code>'</code>) is added to the current expression and
the parser moves to <emphasis>xpath-mode</emphasis>.</para></listitem>
<listitem><para>All other characters are appended to the current expression.</para></listitem>
</orderedlist>
</listitem>
<listitem>
<para>In <emphasis>double-quote-mode</emphasis>:</para>
<orderedlist>
<listitem><para>A double quote (<code>"</code>) is added to the current expression and
the parser moves to <emphasis>xpath-mode</emphasis>.</para></listitem>
<listitem><para>All other characters are appended to the current expression.</para></listitem>
</orderedlist>
</listitem>
</itemizedlist>

<para><error code="C0067">It is a <glossterm>dynamic error</glossterm> if the parser reaches
the end of the unit of content and it is not in <emphasis>regular-mode</emphasis>.</error>
</para>

<para>The context node used for each expression is the document passed on the
<port>source</port> port.
<error code="C0068">It is a <glossterm>dynamic error</glossterm>
if more than one document appears on the <port>source</port> port.</error>
In an XPath 1.0 implementation, if
<tag>p:empty</tag> is given or implied on the <port>source</port> port, an
empty document node is used as
the context node. In an XPath 2.0 implementation, the context item is
undefined.
<error code="C0026">It is a <glossterm>dynamic error</glossterm> if
any XPath expression makes reference to the context node, size, or
position when the context item is undefined.</error></para>

<para>In an attribute value, processing instruction, or comment, the
string value of the XPath expression is used. In text content, an
expression that selects nodes will cause those nodes to be copied into
the template document.</para>

<note>
<para>Depending on which version of XPath an implementation supports,
and possibly on the <tag class="attribute">xpath-version</tag> setting on
the <tag>p:template</tag>, some implementations may report errors, or
different results, than other implementations in those cases where the
interpretation of an XPath expression differs between the versions of
XPath.</para>
</note>

<para>An example of <tag>p:document</tag> appears in
<xref linkend="introduction"/>.
</para>

</section>

<appendix xml:id="references">
<title>References</title>

<section xml:id="normative-references">
<title>Normative References</title>

<bibliolist>
<bibliomixed xml:id="rfc2119"><abbrev>RFC 2119</abbrev>
<citetitle xlink:href="http://www.ietf.org/rfc/rfc2119.txt"
>Key words for use in RFCs to Indicate Requirement Levels</citetitle>.
S. Bradner.
Network Working Group, IETF,
Mar 1997.
</bibliomixed>
<bibliomixed xml:id="xproc"><abbrev>XProc: An XML Pipeline Language</abbrev>
<citetitle xlink:href="http://www.w3.org/TR/xproc/">XML:
An XML Pipeline Language</citetitle>. Norman Walsh, Alex Milowski, and Henry S. Thompson,
editors. W3C Recommedation 11 May 2010.
</bibliomixed>
</bibliolist>
</section>
</appendix>

<appendix xmlns="http://docbook.org/ns/docbook" 
	  xml:id="errors-list">
<title>List of Error Codes</title>

<section xml:id="app.step-errors">
<title>Step Errors</title>

<para>The following <glossterm baseform="dynamic-error">dynamic errors</glossterm>
are explicitly called out in this note.</para>

<?step-error-list?>

<para>Other errors may also arise, see
<biblioref linkend="xproc"/> for a complete discussion of error codes.</para>

</section>
</appendix>

</specification>
