title: XSL Finding element by attribute in for-each
body: <p>In my XML document, I have the following kind of nodes:</p>
  
  <pre><code>&lt;parent&gt;
    &lt;value id="value1" name="Y" type="number"/&gt;
    &lt;value id="value2" name="X" type="number"/&gt;
    &lt;value id="value3" name="Z" type="operation" op="-" args="value1;value2"/&gt;
  &lt;/parent&gt;
  </code></pre>
  
  <p>And I want to transform this to get the full operation looking like this :</p>
  
  <pre><code>&lt;parent&gt;
    &lt;value id="value1" name="Y" type="number" /&gt;
    &lt;value id="value2" name="X" type="number" /&gt;
    &lt;operation&gt;
      &lt;name&gt;Z = Y - X&lt;/name&gt;
    &lt;/operation&gt;
  &lt;/parent&gt;
  </code></pre>
  
  <p>I'm struggling with my xsl template. Here is the full XSL code:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet version="2.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:str="http://exslt.org/strings"&gt;
  
      &lt;xsl:template match="@*|node()" priority="0"&gt;
          &lt;xsl:copy&gt;
              &lt;xsl:apply-templates select="@*|node()" /&gt;
          &lt;/xsl:copy&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template match="value[@type='operation']" priority="1"&gt;
          &lt;xsl:variable name="name"&gt;
              &lt;xsl:value-of select="concat(@name, ' = ')" /&gt;
              &lt;xsl:for-each select="str:tokenize(@args, ';')"&gt;
                  &lt;xsl:choose&gt;
                      &lt;xsl:when test="//value[@id=current()]"&gt;
                          &lt;xsl:value-of
                              select="concat(//value[@id=current()]/@name, ' ', @op, ' ')" /&gt;
                      &lt;/xsl:when&gt;
                      &lt;xsl:otherwise&gt;
                          &lt;xsl:value-of select="concat(current(), ' ', @op, ' ')" /&gt;
                      &lt;/xsl:otherwise&gt;
                  &lt;/xsl:choose&gt;
              &lt;/xsl:for-each&gt;
          &lt;/xsl:variable&gt;
  
          &lt;operation&gt;
              &lt;name&gt;
                  &lt;xsl:value-of select="$name" /&gt;
              &lt;/name&gt;
          &lt;/operation&gt;
      &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>Inside the fore-each I am checking if it can find the corrent node because sometimes, the @args of the operation can be something like <code>args="2.00;value1"</code> for example.</p>
  
  <p>There is obviously something wrong with my test inside the for-each because the result I am getting for the input file shown above is</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;parent&gt;
      &lt;value id="value1" name="Y" type="number" /&gt;
      &lt;value id="value2" name="X" type="number" /&gt;
      &lt;operation xmlns:str="http://exslt.org/strings"&gt;
          &lt;name&gt;Z = value1 value2  &lt;/name&gt;
      &lt;/operation&gt;
  &lt;/parent&gt;
  </code></pre>
  
  <p>What should the test be to get the name of the correct value ?</p>