Variables

In the following example the use of variables in XProc will be introduced.

p:variable

<p:variable
  name = QName
  select = XPathExpression>
   ((p:empty |
   p:pipe |
   p:document |
   p:inline |
   p:data)? &
  p:namespaces*)
</p:variable>

Variables can only be generated in Compound Steps. They include values which are defined via an appropriate XPath expression. The name of a variable has to be a QName. They can be further used within this step. They can also be loaded with <p:empty>, <p:pipe>, <p:document>, <p:inline> or <p:data> (afterwards, the "select" attribute extracts the desired contents and stores them in the variable).

Example

In the following example the use of <p:variable> is demonstrated.

<?xml version="1.0" encoding="UTF-8"?>
<p:declare-step xmlns:p="http://www.w3.org/ns/xproc" xmlns:c="http://www.w3.org/ns/xproc-step" version="1.0">
  <p:input port="source">
    <p:inline>
      <block>
        <fileName>test.xml</fileName>
        <text>This is just a small example text.</text>
      </block>
    </p:inline>
  </p:input>
  <p:output port="result">
    <p:empty/>
  </p:output>
  <p:group>
    <p:variable name="Inline" select="block/fileName"/>
    <p:identity/>
    <p:store>
      <p:with-option name="href" select="$Inline"/>
    </p:store>
  </p:group>
</p:declare-step>

In the example a XML document is generated by <p:inline>. This document has a value in the “fileName“ element which shall be assigned to the variable. In order to be able to generate a variable, it has to be within a Compound Step. Therefore, <p:group>, a Compound Step, is used as enclosing element. The variable gets the name “Inline“ and “text.xml“ as value, this corresponds to the return value of the indicated XPath expression. The <p:identity> step re-outputs the entire content of <p:inline> which then becomes the content of <p:store> and is stored by it. As file name the value stored in the variable is used. The content of the variable is resolved by the preceding Dollar character ($) and so it can be used as file name.

The result of this pipeline is as follows:

<block xmlns:c="http://www.w3.org/ns/xproc-step">
   <dateiname>test.xml</dateiname>
   <text>This is just a small example text.</text>
</block>

The result is the content of the “text.xml“ file generated by the pipeline.

<< back next >>