Previous Up Next


p:replace

p:replace — Replaces matching content.

Synopsis

<p:declare-step type="p:replace">
     <p:input port="source" primary="true"/>
     <p:input port="replacement"/>
     <p:output port="result"/>
     <p:option name="match" required="true"/>                      <!-- XSLTMatchPattern -->
</p:declare-step>

Description

The p:replace step replaces matching nodes in its input with the content of the replacement document.

Errors

Error Description
err:C0023 Occurs if the match pattern matches anything other than element, text, processing-instruction, or comment nodes.

Examples

This pipeline replaces the paragraph with the “summary” role with a new paragraph.

  1 <p:pipeline xmlns:p="http://www.w3.org/ns/xproc"
                xmlns:h="http://www.w3.org/1999/xhtml"
                version="1.0">
    
  5   <p:replace match="para[@role='summary'>
        <p:input port="replacement">
          <p:inline>
            <para>Static text.</para>
          </p:inline>
 10     </p:input>
      </p:replace>
    </p:pipeline>
Input Output
1 <doc>
<para role="summary">Some text.</para>
<para>Some other text.</para>
</doc>
 
1 <doc>
<para>Static text.</para>
<para>Some other text.</para>
</doc>

The preceding example isn't very practical because the replacement text is static. For a more realistic example, consider this pipeline which uses XSLT to update the summary paragraph, then uses p:replace to replace the original with the new version.

  1 <p:pipeline xmlns:p="http://www.w3.org/ns/xproc" name="main"
                version="1.0">
    
      <p:xslt>
  5     <p:input port="source" select="//para[@role='summary'/>
        <p:input port="stylesheet">
          <p:inline>
            <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                            version="2.0">
 10           <xsl:template match="para">
                <xsl:copy>
                  <xsl:copy-of select="@*"/>
                  <xsl:text>In summary: </xsl:text>
                  <xsl:apply-templates/>
 15             </xsl:copy>
              </xsl:template>
            </xsl:stylesheet>
          </p:inline>
        </p:input>
 20   </p:xslt>
    
      <p:replace match="para[@role='summary'>
        <p:input port="source">
          <p:pipe step="main" port="source"/>
 25     </p:input>
      </p:replace>
    </p:pipeline>
Input Output
1 <doc>
<para role="summary">Some text.</para>
<para>Some other text.</para>
</doc>
 
1 <doc>
<para role="summary">In summary: Some text.</para>
<para>Some other text.</para>
</doc>

(Though a p:viewport would make more sense here.)