title: How to compare a node's atrribute with all preceding siblings' attributes in xslt 1.0? (without keys)
body: <p>I have the following XML-file.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;goods&gt;
      &lt;item class="fruit"&gt;apple&lt;/item&gt;
      &lt;item class="fruit"&gt;pineapple&lt;/item&gt;
      &lt;item class="vegetables"&gt;tomato&lt;/item&gt;
      &lt;item class="fruit"&gt;lemon&lt;/item&gt;
      &lt;item class="fruit"&gt;melon&lt;/item&gt;
      &lt;item class="fruit"&gt;orange&lt;/item&gt;
      &lt;item class="vegetables"&gt;cucumber&lt;/item&gt;
      &lt;item class="vegetables"&gt;onion&lt;/item&gt;
      &lt;item class="vegetables"&gt;garlic&lt;/item&gt;
  &lt;/goods&gt;
  </code></pre>
  
  <p>I need to get from it (but without using keys or Muenchian method) XML like this:</p>
  
  <pre><code>&lt;fruit&gt;
      &lt;item class="fruit"&gt;apple&lt;/item&gt;
      &lt;item class="fruit"&gt;lemon&lt;/item&gt;
      &lt;item class="fruit"&gt;melon&lt;/item&gt;
      &lt;item class="fruit"&gt;orange&lt;/item&gt;
      &lt;item class="fruit"&gt;pineapple&lt;/item&gt;
  &lt;/fruit&gt;
  &lt;vegetables&gt;
      &lt;item class="vegetables"&gt;cucumber&lt;/item&gt;
      &lt;item class="vegetables"&gt;garlic&lt;/item&gt;
      &lt;item class="vegetables"&gt;onion&lt;/item&gt;
      &lt;item class="vegetables"&gt;tomato&lt;/item&gt;
  &lt;/vegetables&gt;
  </code></pre>
  
  <p>As you can see I need to group the items according to their class attribute (and as an extra to sort them in alphabetical order but this is not neccesary now). </p>
  
  <p>My XSLT is:</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"&gt;
  
      &lt;xsl:template match="/"&gt;
          &lt;xsl:for-each select="/*/*"&gt;
              &lt;xsl:sort select="@class"/&gt;
              &lt;xsl:sort select="." order="ascending"/&gt;
              &lt;xsl:choose&gt;
                  &lt;xsl:when test="position() = 1"&gt;
                      &lt;xsl:apply-templates select="."/&gt;
                  &lt;/xsl:when&gt;
                  &lt;xsl:otherwise&gt;
                      &lt;xsl:variable name="p" select="position()"/&gt;
                          &lt;xsl:if test="./@class != ./preceding-sibling::*[1]/@class"&gt;
                              &lt;xsl:apply-templates select="."/&gt;
                          &lt;/xsl:if&gt;
                  &lt;/xsl:otherwise&gt;
              &lt;/xsl:choose&gt;
          &lt;/xsl:for-each&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template match="/*/*"&gt;
          &lt;xsl:variable name="id" select="@class"/&gt;
  
          &lt;xsl:element name="{$id}"&gt;
              &lt;xsl:copy-of select="." /&gt;
              &lt;xsl:for-each select="following-sibling::*[@class = $id]"&gt;
                  &lt;xsl:sort select="." order="ascending"/&gt;
                  &lt;xsl:copy-of select="." /&gt;
              &lt;/xsl:for-each&gt;
          &lt;/xsl:element&gt;
      &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>Unfortunately, with this code I don't get the expected result. 
  What I don't undestand is how to compare a node's attribute with the atrributes of all the preceding siblings. I tried to do it like this:</p>
  
  <pre><code>&lt;xsl:if test="./@class != ./preceding-sibling::*/@class"&gt;
  </code></pre>
  
  <p>but it didn't work. So i tried this way:</p>
  
  <pre><code>&lt;xsl:if test="./@class != ./preceding-sibling::*[1]/@class"&gt;
  </code></pre>
  
  <p>but of course it compares only with the previous sibling so this is not the correct code either.</p>
  
  <p>Maybe someone could help me with this? Any help would be appreciated.</p>
  
  <p>A huge thanks in advance!</p>