title: XSLT : How to get the value of the parent tag based on the condition which is dependent on the values of its child tags, in a highly nested XML
body: <p>I've been trying to get the solution for this, but still cannot find a proper solution.
  I referred few links <a href="https://stackoverflow.com/questions/10436237/nested-for-each-loops-accessing-outer-element-with-variable-from-the-inner-loop">Nested for-each loops, accessing outer element with variable from the inner loop</a> and <a href="https://stackoverflow.com/questions/16413801/how-to-extract-child-tags-text-and-extended-text-of-parent-tag-from-xml-using-xs">How to extract child tags text and extended text of parent tag from xml using xslt</a> but these questions don't have the nested tags.</p>
  
  <p>My XML:</p>
  
  <pre><code>&lt;catalog title="TitleABC1"&gt;
    &lt;cd&gt;
      &lt;title code="Y"&gt;Picture book&lt;/title&gt;
      &lt;artist&gt;Simply Red&lt;/artist&gt;
      &lt;country&gt;EU&lt;/country&gt;
    &lt;/cd&gt;
    &lt;catalog title="TitleABC2"&gt;
      &lt;cd&gt;
        &lt;cd&gt;
          &lt;title code="N"&gt;Empire Burlesque&lt;/title&gt;
          &lt;artist&gt;Bob Dylan&lt;/artist&gt;
          &lt;country&gt;USA&lt;/country&gt;
        &lt;/cd&gt;
      &lt;/cd&gt;
      &lt;cd&gt;
        &lt;cd&gt;
          &lt;cd&gt;
            &lt;title code="Y"&gt;Hide your heart&lt;/title&gt;
            &lt;artist&gt;Bonnie Tyler&lt;/artist&gt;
            &lt;country&gt;UK&lt;/country&gt;
          &lt;/cd&gt;
        &lt;/cd&gt;
      &lt;/cd&gt;
      &lt;cd&gt;
        &lt;catalog title="TitleABC3"&gt;
          &lt;cd&gt;
            &lt;title code="N"&gt;Red&lt;/title&gt;
            &lt;artist&gt;The Communards&lt;/artist&gt;
            &lt;country&gt;UK&lt;/country&gt;
          &lt;/cd&gt;
        &lt;/catalog&gt;
      &lt;/cd&gt;
      &lt;cd&gt;
        &lt;title code="N"&gt;Unchain my heart&lt;/title&gt;
        &lt;artist&gt;Joe Cocker&lt;/artist&gt;
        &lt;country&gt;USA&lt;/country&gt;
      &lt;/cd&gt;
    &lt;/catalog&gt;
  &lt;/catalog&gt;
  </code></pre>
  
  <p>For above XML, the condition is that titles of only those <code>catalog</code> tags will be displayed if any of the child/descendants <code>title</code> tags have the code attribute as 'Y'.</p>
  
  <p>Hence, the Output should be like:</p>
  
  <pre><code>TitleABC1
  TitleABC2
  </code></pre>
  
  <p>I was trying at the below logic for XSLT, but couldn't get the desired solution.</p>
  
  <pre><code> &lt;xsl:template match="catalog"&gt;
  &lt;!-- Store the value in a variable --&gt;
                &lt;xsl:for-each select="//title"&gt;
  &lt;!-- &lt;xsl:if&gt; to check for the code attrib --&gt;                                                 
                &lt;/xsl:for-each&gt;
              &lt;/xsl:template&gt;
  </code></pre>