title: XSLT1.0 Check if a descendant exists whose ancestors don't have a specific attribute but if they do it's value must be in a RTF variable
body: <p>I have a <code>xsl:variable</code> ($features-list) specified in my XSL which contains a tree fragment from an external XML file. </p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;xsl:variable name="features-list" select="document('features-list.xml', /)" /&gt;
  </code></pre>
  
  <p>This file contains a list of "feature" elements where each element's value specifies a feature name. In the XML which I'm trying to transform (it's actually a RNG schema) certain elements have attributes conveniently named "if-feature" which specify that a copy of such an element (or rather an entire subtree) should only appear in the transformed document if such an attribute is not present OR if the attribute appears but has a same value as one of the feature elements in the external file. This may potentially mean that entire subtrees may be omitted in the result document since the attribute is "inherited" from ancestors.</p>
  
  <p>I've successfuly used this variable to fire or omit certain templates using checks like</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;xsl:when test="not(@if-feature) or @if-feature and $features-list//*[.=current()/@if-feature]"&gt;
  &lt;/xsl:when&gt;
  </code></pre>
  
  <p>in any template. But this will only work when comparing the entries in the features list with the element which is currently being processed by the template. What I'd need now is something like this:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;xsl:apply-templates select="descendant::foo[ancestor-or-self::*[not(@if-feature) or $features-list//*[.=current()/@if-feature]]]"/&gt;
  </code></pre>
  
  <p>where current() refers to the one of the ancestor-or-self elements. I'm actually trying to fire a template based on a condition like this: only process foo descendants of the current node of which ancestors (and itself) - if they have an if-feature attribute - it's value must match one of the feature element values in $feature-list.</p>
  
  <p>Can I express such a condition using XPath only? How? </p>
  
  <p>Would an XSLT solution be more appropriate here? This would identify offending elements but doesn't help me much because of XSLT limitations.</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;xsl:for-each select="descendant::foo"&gt;
      &lt;xsl:for-each select="ancestor-or-self::*"&gt;
        &lt;xsl:choose&gt;
          &lt;xsl:when test="@if-feature and not($features-list//*[.=current()/@if-feature])"&gt;
            &lt;!-- we have a hit, break and do not proceed - but wait, this is XSLT, not Java --&gt;
          &lt;/xsl:when&gt;
        &lt;/xsl:choose&gt;
      &lt;/xsl:for-each&gt;
   &lt;/xsl:for-each&gt;
  </code></pre>
  
  <p>Trying my best to learn XSLT 1.0 here, so please refrain from providing solutions for 2.0.</p>
  
  <p>What I'm trying to accomplish is to extract a valid RNG schema from an existing one which specifies feature based subtrees. I've actually done that already but it doesn't work for <strong><em>ONE</em></strong> tiny smallish rarely-occuring case which is now driving me nuts. Through walls of fire. And bricks.</p>
  
  <p>Edit: the specification of my condition was unclear, so I corrected it.</p>