title: Execute condition in for-each loop only once (break for-each) in XSLT
body: <p>I'm very new to XSLT and have this problem. I have found many posts and material that there is no way of breaking for-each loop in XSLT and variables cannot be changed after you declare them, but I couldn't find the solution to my problem. If there is one already answered, please provide a link.</p>
  
  <p>I want to check if in a given node-set exists at least one element that contains a specific value. If it does, I want to create an element with an attribute ONLY ONCE in the transform.</p>
  
  <pre><code>&lt;xsl:variable name="name" select="$path/to/something"/&gt;
      &lt;xsl:if test="count($name) &gt; 0"&gt;
          &lt;xsl:for-each select="$name"&gt;
              &lt;xsl:if test="ctvf:trim(./@name) = 'P'"&gt;
                  &lt;name&gt;
                      &lt;xsl:attribute name="Name"&gt;
                          &lt;xsl:value-of select="ctvf:trim(./@name)"/&gt;
                      &lt;/xsl:attribute&gt;
                  &lt;/name&gt;
              &lt;/xsl:if&gt;
          &lt;/xsl:for-each&gt;
      &lt;/xsl:if&gt;
  </code></pre>
  
  <p>Currently if condition check is met <code>&lt;xsl:if test="ctvf:trim(./@name) = 'P'"&gt;</code> more than once, it creates several the same elements, I only need to create that element once.</p>
  
  <p><strong>Sample XML</strong></p>
  
  <pre><code>&lt;names&gt;
      &lt;name name="P"&gt;&lt;/name&gt;
      &lt;name name="B"&gt;&lt;/name&gt;
      &lt;name name="A"&gt;&lt;/name&gt;
      &lt;name name="P"&gt;&lt;/name&gt;
  &lt;/names&gt;
  </code></pre>
  
  <p><strong>Expected output</strong></p>
  
  <pre><code>&lt;name Name="P"&gt;&lt;/name&gt;
  </code></pre>
  
  <p><strong>Current Output</strong></p>
  
  <pre><code>&lt;name Name="P"&gt;&lt;/name&gt;
  &lt;name Name="P"&gt;&lt;/name&gt;
  </code></pre>
  
  <p><strong>What would be simple solution to this?</strong></p>