Add (or replace) an attribute on a set of elements.
<p:declare-step type="p:add-attribute"> <input port="source" primary="true" content-types="xml html" sequence="false"/> <output port="result" primary="true" content-types="xml html" sequence="false"/> <option name="attribute-name" as="xs:QName" required="true"/> <option name="attribute-value" as="xs:string" required="true"/> <option name="match" as="xs:string" required="false" select="'/*'"/> </p:declare-step>
The p:add-attribute
step adds (or replaces) an attribute. This is done for the element(s) matched by the match
option.
Ports:
Port | Type | Primary? | Content types | Seq? | Description |
---|---|---|---|---|---|
|
|
|
|
| The document to add (or replace) the attribute on. |
|
|
|
|
| The resulting document. |
Options:
The p:add-attribute
step:
Takes the document appearing on its source
port.
Processes the elements that match the pattern in the match
option:
If a selected element does not contain an attribute with the name given in the attribute-name
option, an attribute with this name and a value as given in the attribute-value
option is added to it.
If a selected element already has such an attribute, its value is replaced with the value given in the
attribute-value
option.
The resulting document appears on its result
port.
The p:add-attribute
step can only set a single attribute. The p:set-attributes
step can
be used to set multiple attributes at once.
This example adds a type="special"
attribute to all <text>
elements. One of the input <text>
elements already
has such an attribute, but with a different value. This existing attribute is replaced.
Source document:
<texts> <text>Hello there!</text> <text>This is funny…</text> <text type="normal">And that's normal.</text> </texts>
Pipeline document:
<p:declare-step xmlns:p="http://www.w3.org/ns/xproc" version="3.0"> <p:input port="source"/> <p:output port="result"/> <p:add-attribute match="text" attribute-name="type" attribute-value="special"/> </p:declare-step>
Result document:
<texts> <text type="special">Hello there!</text> <text type="special">This is funny…</text> <text type="special">And that's normal.</text> </texts>
p:add-attribute
preserves all document-properties of the document(s) appearing on its source
port.
If an attribute called xml:base
is added or changed, the base URI of the element is updated accordingly. See also category
Base URI related.
You cannot use this step to add or change a namespace declaration. Attempting to do so will result in error XC0059
.
Note, however, that it is possible to add an attribute whose namespace is not in scope on the element it is added to. The XProc namespace fixup mechanism will take care of handling this and add the appropriate namespace declarations.
This description of the p:add-attribute
step is for XProc version: 3.1. This is a required step (an XProc 3.1 processor must support this).
The formal specification for the p:add-attribute
step can be found here.
The p:add-attribute
step is part of categories:
The p:add-attribute
step is also present in version:
3.0.