title: XSLT2: How to reference attributes about the current node in XPath2 predicates
body: <p>I had posted <a href="https://stackoverflow.com/questions/7921120/xslt2-how-to-set-the-value-of-a-variable-in-a-udf-requiring-a-path-call">another question</a> with this as one aspect of it. I was told to clarify the question, but that question was already pretty long and complicated, so I created a new one.</p>
  
  <p>I want to know if there is standard way of referencing the <em>current</em> node's attribute in an XPath expression testing for another one.</p>
  
  <p>For an example, consider the following XSLT</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"&gt;
  &lt;xsl:template match="/"&gt;
      &lt;xsl:for-each select="potato/stem[eye]"&gt;
          In session &lt;xsl:value-of select="@sessionID"/&gt;, the potato had &lt;xsl:value-of select="/potato/stem[@sessionID=@sessionID][scc]/scc/@leafnumber"/&gt; s.c.c. leaves.
      &lt;/xsl:for-each&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p></p>
  
  <p>(XML source at bottom of this question.)
  (Note that the for-each references nodes of type stem[eye] but the second value requested references nodes of type stem[scc], which are on a different branch of the source XML tree.)</p>
  
  <p>Now, obviously, the "@sessionID=@sessoinID" part of that is mostly meaningless because XPath perceives this as "The value of the sessionID attribute of the node should equal ... the value of the sessionID attribute of the node."</p>
  
  <p>But what I want to say is "Test to make sure the value of the seesionID attribute of that node (the one in the XPath expression) is the same as the sessionID of the node of whatever /stem[eye] node I'm in right now."</p>
  
  <p>I can't do this with a variable, because you are not allowed to declare a variable within a for-each clause.</p>
  
  <p>For reference, this is the XML source. Its structure is not what one would like, but it is what I have to work with.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;potato&gt;
  &lt;stem sessionID="1"&gt;
      &lt;eye number = "25"/&gt;
  &lt;/stem&gt;
  &lt;stem sessionID="3"&gt;
      &lt;eye number = "33"/&gt;
  &lt;/stem&gt;
  
  &lt;stem sessionID="1"&gt;
      &lt;scc leafnumber = "234" /&gt;
  &lt;/stem&gt;
  &lt;stem sessionID="2"&gt;
      &lt;scc leafnumber = "433"/&gt;
  &lt;/stem&gt;
  &lt;stem sessionID="3"&gt;
      &lt;scc leafnumber = "463"/&gt;
  &lt;/stem&gt;
  
  &lt;stem sessionID="1"&gt;
      &lt;agd leafnumber = "154"/&gt;
  &lt;/stem&gt;
  &lt;stem sessionID="2"&gt;
      &lt;agd leafnumber = "233"/&gt;
  &lt;/stem&gt;
  &lt;stem sessionID="3"&gt;
      &lt;agd leafnumber = "113"/&gt;
  &lt;/stem&gt; 
  &lt;/potato&gt;
  </code></pre>
  
  <p>The output I'm looking for is:</p>
  
  <p>In session 1, the potato had 234 s.c.c. leaves.</p>
  
  <p>In session 3, the potato had 463 s.c.c. leaves.</p>
  
  <p>(Of course, this is all just sandbox example. I realize there are probably easy ways to accomplish the above output in a completely different manner, but I hope this example gets across my question, which is how to use values associated with the current node (say, in a for-each) in a predicate for an XPath searching out a different one.)</p>