The logo is a pig framed between two angle brackets.

Declaring Variables

As you are used to from other programming languages, you can declare variables in XProc. You can insert them at almost every point in your pipeline. This example shows how to store the filename in the variable of the same name and use it within p:store to save the text document with that filename.

<?xml version="1.0" encoding="UTF-8"?>
<p:declare-step xmlns:p="http://www.w3.org/ns/xproc" version="3.0">
  
  <p:input port="source">
    <p:inline document-properties="map{'content-type':'text/plain'}">
      Hello XPorc!
    </p:inline>
  </p:input>
  
  <p:variable name="filename" select="'my-file.txt'"/>
  
  <p:store serialization="map{'method':'text'}">
    <p:with-option name="href" select="$filename"/>
  </p:store>
  
</p:declare-step>

In case you’re wondering why no output is specified, there is no output port because the pipeline doesn’t declare one and <p:store/> doesn’t include an output port as well.

XProc variables are not limited to simple key/value pairs. You can connect the outputs of other steps with @pipe, declare the variable value with p:inline or use p:document to load external files. The pipeline above has been slightly modified to demonstrate this approach:

<?xml version="1.0" encoding="UTF-8"?>
<p:declare-step xmlns:p="http://www.w3.org/ns/xproc" 
  version="3.0" name="my-pipeline">
  
  <p:input port="source" primary="true">
    <p:inline document-properties="map{'content-type':'text/plain'}">
      Hello XPorc!
    </p:inline>
  </p:input>
  
  <p:input port="files" primary="false">
    <p:inline>
      <file name="my-file.txt"/>
    </p:inline>
  </p:input>
  
  <p:variable name="filename" select="'' || /file/@name || ''" 
              pipe="files@my-pipeline"/>
  
  <p:store serialization="map{'method':'text'}">
    <p:with-option name="href" select="$filename"/>
  </p:store>
  
</p:declare-step>

Please note that we transformed the variable value into a string. This is necessary because the @href attribute from p:store requires a string rather than an attribute node.

If you use XPath expressions in your variable, please be aware that the context of the variable is defined by the implicit port connection to the previous step. However, you can use the @pipe attribute to overwrite the implicit port connection.

Read more…