title: Make conditional statements of attributes with templates in XSLT
body: <p>How can I get the value of an attribute and use it in a conditional statement. For example I have <code>&lt;O Option="a" LineNr="1"&gt;</code> and this is element is a child under another parent element that gets grouped with other elements. So the condition I want is: if [value-of Option is null] then [do nothing] else-if [check if the value of the attribute of the element under the next parent element with the same value is empty]. And if there are for example 3 parent elements that contain this element with the Option attribute and if all of the attributes are NOT empty - then group them. 
  I have the grouping done, but it is not connected with this attribute that I want to use now. So I need just the formulation of the conditional statements.</p>
  
  <p><strong>edit: giving input and output desired examples</strong>
  Input example 1:</p>
  
  <pre><code>&lt;Area&gt;
       &lt;Type&gt;A&lt;/Type&gt;
       &lt;Street&gt;
         &lt;Position&gt;5&lt;/Position&gt;
         &lt;House&gt;
         &lt;L Option="" LineNr="1"&gt;
         --&gt; some elements
         &lt;/L&gt;
         &lt;/House&gt;
       &lt;/Street&gt;
       &lt;Street&gt;
         &lt;Position&gt;5&lt;/Position&gt;
         &lt;Block&gt;
         &lt;O Option="" LineNr="1"&gt;
         --&gt; some elements
         &lt;/O&gt;
         &lt;/Block&gt;
       &lt;/Street&gt;
       &lt;Street&gt;
         &lt;Position&gt;6&lt;/Position&gt;
         &lt;House&gt;
         &lt;L Option="o" LineNr="1"&gt;
         --&gt; some elements
         &lt;/L&gt;
         &lt;/House&gt;
       &lt;/Street&gt;
       &lt;Street&gt;
         &lt;Position&gt;6&lt;/Position&gt;
         &lt;Block&gt;
         &lt;O Option="" LineNr="1"&gt;
         --&gt; some elements
         &lt;/O&gt;
         &lt;/Block&gt;
       &lt;/Street&gt;
     &lt;/Area&gt;
  </code></pre>
  
  <p>And desired output:</p>
  
  <pre><code>&lt;Area&gt;
     &lt;Type&gt;A&lt;/Type&gt;
       &lt;Street&gt;
         &lt;Position&gt;5&lt;/Position&gt;
         &lt;House&gt;
         &lt;L Option="" LineNr="1"&gt;
         --&gt; some elements
         &lt;/L&gt;
         &lt;/House&gt;
         &lt;Block&gt;
         &lt;O Option="" LineNr="1"&gt;
         --&gt; some elements
         &lt;/O&gt;
         &lt;/Block&gt;
       &lt;/Street&gt;
       &lt;Street&gt;
         &lt;Position&gt;6&lt;/Position&gt;
         &lt;House&gt;
         &lt;L Option="o" LineNr="1"&gt;
         --&gt; some elements
         &lt;/L&gt;
         &lt;/House&gt;
         &lt;Block&gt;
         &lt;O Option="" LineNr="1"&gt;
         --&gt; some elements
         &lt;/O&gt;
         &lt;/Block&gt;
       &lt;/Street&gt;
     &lt;/Area&gt;
  </code></pre>
  
  <p>But if in the same position number, all the occurrences have a value in the Option attribute of L or O, then I want them arranged under the other occurrence of a position number. Like that:</p>
  
  <p>Input example 2:</p>
  
  <pre><code>       &lt;Area&gt;
           &lt;Type&gt;A&lt;/Type&gt;
           &lt;Street&gt;
             &lt;Position&gt;5&lt;/Position&gt;
             &lt;House&gt;
             &lt;L Option="" LineNr="1"&gt;
             --&gt; some elements
             &lt;/L&gt;
             &lt;/House&gt;
           &lt;/Street&gt;
           &lt;Street&gt;
             &lt;Position&gt;5&lt;/Position&gt;
             &lt;Block&gt;
             &lt;O Option="" LineNr="1"&gt;
             --&gt; some elements
             &lt;/O&gt;
             &lt;/Block&gt;
           &lt;/Street&gt;
           &lt;Street&gt;
             &lt;Position&gt;6&lt;/Position&gt;
             &lt;House&gt;
             &lt;L Option="o" LineNr="1"&gt;
             --&gt; some elements
             &lt;/L&gt;
             &lt;/House&gt;
           &lt;/Street&gt;
           &lt;Street&gt;
             &lt;Position&gt;6&lt;/Position&gt;
             &lt;Block&gt;
             &lt;O Option="a" LineNr="1"&gt;
             --&gt; some elements
             &lt;/O&gt;
             &lt;/Block&gt;
           &lt;/Street&gt;
         &lt;/Area&gt;
  </code></pre>
  
  <p>Then the output will be: </p>
  
  <pre><code>     &lt;Area&gt;
         &lt;Type&gt;A&lt;/Type&gt;
           &lt;Street&gt;
             &lt;Position&gt;5&lt;/Position&gt;
             &lt;House&gt;
             &lt;L Option="" LineNr="1"&gt;
             --&gt; some elements
             &lt;/L&gt;
             &lt;/House&gt;
             &lt;Block&gt;
             &lt;O Option="" LineNr="1"&gt;
             --&gt; some elements
             &lt;/O&gt;
             &lt;/Block&gt;
  
             &lt;House&gt;
             &lt;L Option="o" LineNr="1"&gt;
             --&gt; some elements
             &lt;/L&gt;
             &lt;/House&gt;
             &lt;Block&gt;
             &lt;O Option="a" LineNr="1"&gt;
             --&gt; some elements
             &lt;/O&gt;
             &lt;/Block&gt;
           &lt;/Street&gt;
         &lt;/Area&gt;
  </code></pre>
  
  <p>as now position 6 is gone and its child elements are grouped under the upper occurrence position (which is 5 in this case in the same Type) as all of the attributes Option in the L or O elements had value (which can be either "a" or "o", if that is relevant).</p>
  
  <p>And this is the XSLT which I am using now. It only groups the elements of the different occurrences of the same position numbers under one number:</p>
  
  <pre><code>&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"&gt;
    &lt;xsl:strip-space elements="*" /&gt;
    &lt;xsl:output method="xml" indent="yes" /&gt;
  
    &lt;xsl:key name="streetByPosition" match="Street" use="concat(../Type, '|', Position)" /&gt;
  
    &lt;xsl:template match="@*|node()"&gt;
      &lt;xsl:copy&gt;&lt;xsl:apply-templates select="@*|node()" /&gt;&lt;/xsl:copy&gt;
    &lt;/xsl:template&gt;
  
    &lt;!-- for the first Street in each Position --&gt;
    &lt;xsl:template match="Street[generate-id() =
                              generate-id(key('streetByPosition', concat(../Type, '|', Position))[1])]"&gt;
      &lt;Street&gt;
        &lt;!-- copy in the Position element once only --&gt;
        &lt;xsl:apply-templates select="Position" /&gt;
        &lt;!-- copy in all sub-elements except Position from all matching Streets--&gt;
        &lt;xsl:apply-templates select="
              key('streetByPosition', concat(../Type, '|', Position))/*[not(self::Position)]" /&gt;
      &lt;/Street&gt;
    &lt;/xsl:template&gt;
  
    &lt;!-- ignore all other Street elements --&gt;
    &lt;xsl:template match="Street" /&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>