XProc steps

A | B | C| D | E | F | G | H | I | J | K | L | M | N | O | P| Q | R | S | T | U | V | W | X | Y | Z

p:choose

<p:choose name? = NCName>
  (p:xpath-context?,
   p:variable*,
   p:when*,
   p:otherwise?)
</p:choose>

With the help of <p:choose>, case-related decision-making processes can be implemented. Based on an appropriate XSLT expression which is defined in <p:when> the corresponding subpipelines are executed depending on the result. If no condition of <p:when> applies, <p:otherwise> (if indicated) will be performed, which corresponds to a default value.

<p:when
  test = XPathExpression>
  (p:xpath-context?,
  (p:output |
   p:log)*,
   subpipeline)
</p:when>

The <p:when> step has deposited under “test“ a XPath expression as attribute. If it applies, a subpipeline is executed. In addition, the step can be configured and extended by <p:xpath-context>, <p:log> and <p:output>.

<p:otherwise>
  ((p:output |
   p:log)*,
   subpipeline)
</p:otherwise>

<p:otherwise> is the standard way used by <p:choose> in the event that none of the query conditions will be met. So, the appropriate subpipeline of the step is executed and the result is written on the output port.

Example

In the following example the <p:choose> step is introduced.

<?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:document href="FilmCollection.xml"/>
   </p:input>
   <p:output port="result">
      <p:empty/>
   </p:output>
   <p:choose>
      <p:when test="count(//Film) &gt; 1">
         <p:store href="morethanonefile.xml"/>
      </p:when>
      <p:when test="count(//Film)= 1">
         <p:store href="onefile.xml"/>
      </p:when>
      <p:otherwise>
         <p:store href="standard.xml"/>
      </p:otherwise>
   </p:choose>
</p:declare-step>

In the example the appropriate XPath expressions within <p:when> examine how many “Film“ elements the source document does contain. Depending on the case, an appropriate file is generated. The example shall only explain the functionality of the step.