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:wrap

<p:declare-step type="p:wrap">
   <p:input port="source"/>
   <p:output port="result"/>
   <p:option name="wrapper" required="true"/> <!-- QName -->
   <p:option name="wrapper-prefix"/> <!-- NCName -->
   <p:option name="wrapper-namespace"/> <!-- anyURI -->
   <p:option name="match" required="true"/> <!-- XSLTMatchPattern -->
   <p:option name="group-adjacent"/> <!-- XPathExpression -->
</p:declare-step>

The <p:wrap> step gives the user the possibility to embed new enclosing elements into the document. In this way, the target to be enclosed is defined by a XSLT expression (in the “match“ option). Within the “wrapper“ option, the new name for the element is indicated. Furthermore, a prefix and a namespace can be assigned to the new element with the optional options “wrapper-prefix“ and “wrapper-namespace“. The “group-adjacent“ option determines that elements which are directly adjacent and match with the XPath expression required by the option shall also be grouped into the new wrapper element. The source document is loaded at the input port (“source“). The result is written on the output port (“result“).

Example

In the following example all “Genre“ elements shall be enclosed with a new “GenreInfo“ element.

<?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:wrap wrapper="MyWrap" match="/FilmCollection/Film/Genre"/>
</p:declare-step>

After the execution of this process, the result would be as follows (excerpt):

<MyWrap>
   <Genre>SciFi</Genre>
</MyWrap> 
<MyWrap>
   <Genre>Adventure</Genre>
</MyWrap> 
   <Director>George Lucas</Director>

The initial document has deposited two genre entries for the film of this example. According to the current setting, an own wrapper is assigned to each “Genre“ element. In order to enter the genre entries in a single wrapper, the XProc stylesheet is extended:

<?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:wrap wrapper="MyWrap" match="/FilmCollection/Film/Genre" group-adjacent="/FilmCollection/Film/Genre"/>
</p:declare-step>

The result is as follows:

<MyWrap>
   <Genre>SciFi</Genre>
   <Genre>Adventure</Genre>
</MyWrap> 
   <Director>George Lucas</Director>

By the indication of a XPath expression for the “group-adjacent“ option which, in addition, addresses the said “Genre“ elements the processor is instructed to unite any adjacent “Genre“ elements within a wrapper.