Previous Up Next


p:set-attributes

p:set-attributes — Sets a number of attributes at once.

Synopsis

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

Description

The p:set-attributes step is a shortcut for several consecutive p:add-attribute steps. Instead of taking the attributes as options, it takes a second document. All of the attributes on the document element of that document are copied onto matching elements in the source document.

If an attribute with the same name as one of the attributes to be copied already exists, the value specified on the attribute port's document is used.

If an attribute named xml:base is added or changed, the base URI of the element is amended accordingly.

Related Steps

The p:set-attributes step uses an input document to specify any number of attributes. If you only need to set a single attribute, you may find p:add-attribute simpler.

Errors

Error Description
err:C0023 Occurs if the match pattern matches anything other than element nodes.

Examples

This pipeline adds both role and version attributes to any matching section.

  1 <p:pipeline xmlns:p="http://www.w3.org/ns/xproc"
                xmlns:h="http://www.w3.org/1999/xhtml"
                version="1.0">
    
  5   <p:set-attributes match="section[not(@role)]">
        <p:input port="attributes">
          <p:inline>
            <irrelevant-element-name role="new-role"
                                     version="new-version"/>
 10       </p:inline>
        </p:input>
      </p:set-attributes>
    </p:pipeline>
Input Output
1 <doc>
<section role="introduction">
<para>section content</para>
</section>
5 <section>
<para>other section content</para>
</section>
</doc>
 
1 <doc>
<section role="introduction">
<para>section content</para>
</section>
5 <section role="new-role" version="new-version">
<para>other section content</para>
</section>
</doc>

The p:set-attributes step replaces attributes if they already exist:

Input Output
1 <doc>
<section role="introduction" version="old-version">
<para>section content</para>
</section>
5 <section version="old-version">
<para>other section content</para>
</section>
</doc>
 
1 <doc>
<section role="introduction" version="old-version">
<para>section content</para>
</section>
5 <section role="new-role" version="new-version">
<para>other section content</para>
</section>
</doc>