title: How to find ancestor-or-self that is a child of an element with particular attribute?
body: <p>I'm working with a very generic XML structure, where everything is an item (well everything relevant to this question anyway). </p>
  
  <p>Based on knowing the item element I'm currently on and the item element that is the parent of the node I'm looking for, I need to find an item. I have a working xpath, but it's fairly resource intensive and I'm looking for something more elegant and cheaper.</p>
  
  <p>The item key=a node is the parent of the element I'm looking for (though it's not actually a child of the document root)</p>
  
  <p>XML:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;root&gt;
      &lt;item key="a"&gt;
          &lt;item key="b"&gt;
              &lt;item key="c"&gt;
                  &lt;item key="d"/&gt;
              &lt;/item&gt;
          &lt;/item&gt;
          &lt;item key="e"&gt;
              &lt;item key="f"&gt;
                  &lt;item key="g"/&gt;
              &lt;/item&gt;
          &lt;/item&gt;
      &lt;/item&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>The actual XML is much deeper and with far more branching.</p>
  
  <p>So for instance, if I'm on the item with key=g, e or f I need to return the item with key=e. If I'm on the item with key b,c or d I need to return the item with key=b.</p>
  
  <p>I'm using this xpath, which is working, but going up and then back down the ancestor-descendant axis seems a far longer trip than I need.</p>
  
  <pre><code>current()
     /ancestor-or-self::item[@key='a']
        /item[descendant-or-self::* = current()]
  </code></pre>
  
  <p>Is there a simpler way of doing this, bearing in mind that I only know 1) the node I'm on and 2) the key attribute of the parent of the node I'm looking for?</p>
  
  <p>Just for detail's sake: The XML is Sitecore generated, I'm not actually using the current() function, I'm using the sc_currentitem parameter to set the start node I need to begin processing at.</p>
  
  <p>Thanks in advance.</p>