Name

p:add-attribute — The standard p:add-attribute step.

Synopsis

The p:add-attribute step adds a single attribute to a set of matching elements. The input document specified on the source is processed for matches specified by the selection pattern in the match option. For each of these matches, the attribute whose name is specified by the attribute-name option is set to the attribute value specified by the attribute-value option.

The resulting document is produced on the result output port and consists of a exact copy of the input with the exception of the matched elements. Each of the matched elements is copied to the output with the addition of the specified attribute with the specified value.

Input portPrimarySequenceContent types
source✔  xml html 
Output portPrimarySequenceContent types
result✔  xml html 
Option nameTypeDefault valueRequired
attribute-namexs:QName ✔ 
attribute-valuexs:string ✔ 
matchXSLTSelectionPattern'/*' 
Declaration
 1 |<p:declare-step xmlns:p="http://www.w3.org/ns/xproc">
   |   <p:input port="source" content-types="xml html"/>
   |   <p:output port="result" content-types="xml html"/>
   |   <p:option xmlns:e="http://www.w3.org/1999/XSL/Spec/ElementSyntax"
 5 |             name="match"
   |             as="xs:string"
   |             select="'/*'"
   |             e:type="XSLTSelectionPattern"/>
   |   <p:option name="attribute-name" required="true" as="xs:QName"/>
10 |   <p:option name="attribute-value" required="true" as="xs:string"/>
   |</p:declare-step>
Errors
CodeDescription
err:XC0023It is a dynamic error (err:XC0023) if the selection pattern (err:XC0023) matches a node which is not an element.
err:XC0059It is a dynamic error (err:XC0059) if the QName value in the attribute-name option is “xmlns” or uses the prefix “xmlns” or any other prefix that resolves to the namespace name http://www.w3.org/2000/xmlns/.

Description

The p:add-attribute step is defined in the XProc 3.0: Standard Step Library. It is also described on XProcRef.org.

Examples

Applied to the running example, this pipeline:

 1 |<p:declare-step version="3.1"
   |                xmlns:p="http://www.w3.org/ns/xproc"
   |                xmlns:ex="https://xmlcalabash.com/ns/examples">
   |  <p:input port="source"/>
 5 |  <p:output port="result"/>
   | 
   |  <p:add-attribute match="//ex:doc"
   |                   attribute-name="status"
   |                   attribute-value="draft"/>
10 | 
   |  <p:add-attribute match="//ex:chap"
   |                   attribute-name="class"
   |                   attribute-value="chapter"/>
   |</p:declare-step>

Produces this output:

 1 |<?xml version="1.0" encoding="UTF-8"?><doc xmlns="https://xmlcalabash.com/ns/examples" xmlns:xi="http://www.w3.org/2001/XInclude" class="book" status="draft">
   |  <chap former="1" class="chapter">
   |    <title>First Chapter</title>
   |    <p></p>
 5 |  </chap>
   |  <xi:include href="default-ch2.xml"/>
   |  <chap class="chapter"><title>Third Chapter</title>
   |  <p></p>
   |  </chap>
10 |  <app><title>Appendix</title>
   |  <p></p>
   |  </app>
   |</doc>

This listing attempts to summarize what has changed from the original input.

<doc xmlns="https://xmlcalabash.com/ns/examples"
     class="book" status="draft">
<chap former="1" class="chapter">
<title>First Chapter</title>
<p>…</p>
</chap>
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
            href="default-ch2.xml"/>
<chap class="chapter">
<title>Third Chapter</title>
<p>…</p>
</chap>
<app>
<title>Appendix</title>
<p>…</p>
</app>
</doc>

When present, additions are shown in bold, green; deletions are shown in italic, red with strike through; and changed elements are shown in bold, italic orange.

 1 |<doc xmlns="https://xmlcalabash.com/ns/examples"
   |     xmlns:xi="http://www.w3.org/2001/XInclude"
   |     class="book">
   |  <chap former="1" class="test">
 5 |    <title>First Chapter</title>
   |    <p></p>
   |  </chap>
   |  <xi:include href="default-ch2.xml"/>
   |  <chap><title>Third Chapter</title>
10 |  <p></p>
   |  </chap>
   |  <app><title>Appendix</title>
   |  <p></p>
   |  </app>
15 |</doc>