29. editAttribute

This command is similar to putAttribute except that it returns the attribute value as a string instead of setting the attribute on the selected element. This command is only useful to write macro commands.

Macro command example: set attribute style, but when the value chosen by the user is the empty string, remove attribute style (if any):

<command name="setStyle">
  <macro>
    <sequence>
      <command name="editAttribute" 
               parameter="[implicitElement] style" />
      <set variable="style" expression="%_"
           plainString="true" />

       <choice>
         <sequence>
          <test expression="not($style)" />
          <command name="removeAttribute" 
                   parameter="[implicitElement] style" />
         </sequence>

         <sequence>
          <get expression="replace($style, &quot;'&quot;, &quot;\\'&quot;, l)" />1
          <command name="putAttribute"
                   parameter="[implicitElement] style '%_'" />2
         </sequence>
      </choice>
    </sequence>
  </macro>
</command>

1

Escape single quote characters "'" by replacing them by "\'". This is done using XPath function replace() in XMLmind XML Editor - Support of XPath 1.0.

2

The value of attribute style may contain whitespace. Therefore it must quoted using single or double quotes. In the above example, it is quoted using single quotes.