title: XSLT: run for-each body only once for multiple satisfied if conditions
body: <p>I am new to XSLT and I have to make some changes in an existing XSLT file.</p>
  
  <p>I have the following sample xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Item&gt;
    &lt;SubItems&gt;
      &lt;SubItem&gt;
        &lt;InnerItem&gt;
          &lt;ValueToSearch&gt;XYZ&lt;/ValueToSearch&gt;
          &lt;Level&gt;0&lt;/Level&gt;
          &lt;InnerItem&gt;
            &lt;ValueToSearch&gt;Value1&lt;/ValueToSearch&gt;
            &lt;Level&gt;1&lt;/Level&gt;
            &lt;InnerItem&gt;
              &lt;ValueToSearch&gt;ABC&lt;/ValueToSearch&gt;
              &lt;Level&gt;2&lt;/Level&gt;
              &lt;InnerItem&gt;
                &lt;ValueToSearch&gt;Value2&lt;/ValueToSearch&gt;
                &lt;Level&gt;3&lt;/Level&gt;
              &lt;/InnerItem&gt;
            &lt;/InnerItem&gt;
          &lt;/InnerItem&gt;
        &lt;/InnerItem&gt;
        &lt;InnerItem&gt;
          &lt;ValueToSearch&gt;IJK&lt;/ValueToSearch&gt;
          &lt;Level&gt;0&lt;/Level&gt;
        &lt;/InnerItem&gt;
        &lt;InnerItem&gt;
          &lt;ValueToSearch&gt;Value3&lt;/ValueToSearch&gt;
          &lt;Level&gt;0&lt;/Level&gt;
          &lt;InnerItem&gt;
            &lt;ValueToSearch&gt;HYT&lt;/ValueToSearch&gt;
            &lt;Level&gt;1&lt;/Level&gt;
          &lt;/InnerItem&gt;
        &lt;/InnerItem&gt;
      &lt;/SubItem&gt;
    &lt;/SubItems&gt;
  &lt;/Item&gt;
  </code></pre>
  
  <p>In the above XML, I want to loop through all <code>&lt;InnerItem&gt;</code> tags (including the nested one's) and read the value <code>&lt;ValueToSearch&gt;</code> tag value and check if it is present in a variable with multiple space separated values. My XSLT:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"&gt;
    &lt;xsl:output method="xml" indent="yes" /&gt;
    &lt;xsl:variable name="List" select="'Value1 Value2 Value3'"&gt;&lt;/xsl:variable&gt;
    &lt;xsl:template match="/"&gt;
      &lt;xsl:for-each select="//Item/SubItems/SubItem"&gt;
          &lt;xsl:for-each select=".//InnerItem[contains( concat(' ', $List, ' '), concat(' ', ValueToSearch , ' '))]"&gt;
              &lt;Value&gt;
                &lt;xsl:value-of select="ValueToSearch"/&gt;
              &lt;/Value&gt;
          &lt;/xsl:for-each&gt;
      &lt;/xsl:for-each&gt;
    &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>In the above XSLT, all the <code>&lt;InnerItem&gt;</code> tags (including the nested one's) are looped through and value of <code>&lt;ValueToSearch&gt;</code> search tag is checked if it is present in the <code>$List</code> variable which is working fine.</p>
  
  <p>My problem here is since there are two matching values present in the <code>&lt;ValueToSearch&gt;</code> tags, the body of the <code>for-each</code> tag runs two times. If there are more matching values in the XML for <code>&lt;ValueToSearch&gt;</code> tags, the body of the loop runs that many number of times. </p>
  
  <p>Current output:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;ValueToSearch&gt;Value1&lt;/ValueToSearch&gt;
  &lt;ValueToSearch&gt;Value2&lt;/ValueToSearch&gt;
  &lt;ValueToSearch&gt;Value3&lt;/ValueToSearch&gt;
  </code></pre>
  
  <p>Expected output:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;ValueToSearch&gt;Value1&lt;/ValueToSearch&gt;
  </code></pre>
  
  <p>My requirement is that if the matching value is found, just run the the body of the <code>for-each</code> tag only once by adding another <code>xsl:if</code> which will satisfy only once for all matching values. I understand there is not way to break the loop in XSLT but is there a simpler way to achiever what I am trying to do here?</p>