title: xslt: How can I apply two templates to the same node during processing?
body: <p>I have an XSL template that matches any element with a <code>&lt;var&gt;</code> child:</p>
  
  <pre><code>&lt;xsl:template match="*[var]"&gt;
      &lt;xsl:copy &gt;
          &lt;xsl:attribute name="editable"&gt;
              &lt;xsl:for-each select="var[@attr]"&gt;
                  &lt;xsl:value-of
                                select="concat(@attr,
                                               substring(' ',
                                                         1 div (position()!=last())))"/&gt;
              &lt;/xsl:for-each&gt;
          &lt;/xsl:attribute&gt;
          &lt;xsl:attribute name="constraints"&gt;
              &lt;xsl:for-each select="var[@ok]"&gt;
                  &lt;xsl:value-of
                                select="concat(@attr,':',@ok,
                                               substring(';',
                                                         1 div (position()!=last())))"/&gt;
              &lt;/xsl:for-each&gt;
          &lt;/xsl:attribute&gt;
          &lt;!-- if this is an &lt;a&gt; then we have to put the stuff inside it inside it --&gt;
          &lt;xsl:apply-templates select="@*" /&gt;
          &lt;xsl:apply-templates /&gt;
      &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>It concatenates the <code>attr</code>s of the var elements into the <code>editable</code> attribute of the parent; and the <code>ok</code>s into <code>constraint</code>s.</p>
  
  <p>Then I have a template that matches any <code>&lt;field&gt;</code> element:</p>
  
  <pre><code>&lt;xsl:template match="field"&gt;
       &lt;span&gt;
          &lt;xsl:attribute name="field"&gt;
              &lt;xsl:choose&gt;
                  &lt;xsl:when test="boolean(@name)"&gt;
                     &lt;xsl:value-of select="./@name"/&gt;
                  &lt;/xsl:when&gt;
                  &lt;xsl:otherwise&gt;
                      &lt;xsl:text&gt;true&lt;/xsl:text&gt;
                  &lt;/xsl:otherwise&gt;
              &lt;/xsl:choose&gt;
          &lt;/xsl:attribute&gt;
          &lt;xsl:apply-templates /&gt;
      &lt;/span&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>This simply converts it to <code>&lt;span field="name"&gt;</code> with a name the same of the field, if the field had one, or else 'true'.</p>
  
  <p>Problem I'm having is, <code>*[var]</code> matches a field if the field has a <code>&lt;var&gt;</code> as a child. But what I want to happen is for the <code>*[var]</code> to match <em>first</em>, and then <code>field</code> to match <em>as well</em>, but <em>afterwards</em>.</p>
  
  <p>Currently, with an input of</p>
  
  <pre><code>&lt;field name="field1"&gt;
      &lt;var attr="class" ok="small,smaller" /&gt;
      Text
  &lt;/field&gt;
  </code></pre>
  
  <p>I get</p>
  
  <pre><code>&lt;field name="field1" editable="class" constraints="class:small,smaller"&gt;
      Text
  &lt;/field&gt;
  </code></pre>
  
  <p>But I want</p>
  
  <pre><code>&lt;span field="field1" editable="class" constraints="class:small,smaller"&gt;
      Text
  &lt;/span&gt;
  </code></pre>
  
  <p>I found some answers on SO about doing two passes but I'm not sure whether it's the right answer, nor quite how to implement the answers I did find. How should I approach this, and if there's an easy answer, what is it? :)</p>
  
  <p>TIA 
  Altreus</p>