title: XPath/XSLT nested predicates: how to get the context of outer predicate?
body: <p>It seems that this question was not discussed on stackoverflow before, save for <a href="https://stackoverflow.com/questions/963451/working-with-nested-xpath-predicates-refined">Working With Nested XPath Predicates ... Refined</a> where the solution not involving nested predicates was offered.</p>
  
  <p>So I tried to write the oversimplified sample of what I'd like to get:</p>
  
  <p>Input:</p>
  
  <pre><code>&lt;root&gt;
      &lt;shortOfSupply&gt;
          &lt;food animal="doggie"/&gt;
          &lt;food animal="horse"/&gt;
      &lt;/shortOfSupply&gt;
      &lt;animalsDictionary&gt;
          &lt;cage name="A" animal="kittie"/&gt;
          &lt;cage name="B" animal="dog"/&gt;
          &lt;cage name="C" animal="cow"/&gt;
          &lt;cage name="D" animal="zebra"/&gt;
      &lt;/animals&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>&lt;root&gt;
      &lt;hungryAnimals&gt;
          &lt;cage name="B"/&gt;
          &lt;cage name="D"/&gt;
      &lt;/hungryAnimals&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>or, alternatively, if there is no intersections,</p>
  
  <pre><code>&lt;root&gt;
      &lt;everythingIsFine/&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>And i want to get it using a nested predicates:</p>
  
  <pre><code>&lt;xsl:template match="cage"&gt;
      &lt;cage&gt;
          &lt;xsl:attribute name="name"&gt;
              &lt;xsl:value-of select="@name"/&gt;
          &lt;/xsl:attribute&gt;
      &lt;/cage&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="/root/animalsDictionary"&gt;
      &lt;xsl:choose&gt;
          &lt;!--                                                             in &lt;food&gt;     in &lt;cage&gt;       --&gt;
          &lt;xsl:when test="cage[/root/shortOfSupply/food[ext:isEqualAnimals(./@animal, ?????/@animal)]]"&gt;
              &lt;hungryAnimals&gt;
                  &lt;xsl:apply-templates select="cage[/root/shortOfSupply/food[ext:isEqualAnimals(@animal, ?????/@animal)]]"/&gt;
              &lt;/hungryAnimals&gt;
          &lt;/xsl:when&gt;
          &lt;xsl:otherwise&gt;
              &lt;everythingIsFine/&gt;
          &lt;/xsl:otherwise&gt;
      &lt;/xsl:choose&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>So what should i write in place of that <code>?????</code>?</p>
  
  <p>I know i could rewrite the entire stylesheet using one more template and extensive usage of variables/params, but it makes even this stylesheet significantly more complex, let alone the real stylesheet i have for real problem.</p>
  
  <p>It is written in XPath reference that the dot <code>.</code> sign means the current context node, but it doesn't tell whether there is any possibility to get the node of context before that; and i just can't believe XPath is missing this obvious feature.</p>