title: XSLT-XPath: For-each path specification for re-grouping of elements
body: <p>I have XML documents which I want to transfer into another structure. An example of a document looks as follows:</p>
  
  <pre><code>&lt;application&gt;
      &lt;contactPerson&gt;
          &lt;name&gt;Dominik&lt;/name&gt;
          &lt;countryCode&gt;DE&lt;/countryCode&gt;
      &lt;/contactPerson&gt;
      &lt;contactPerson&gt;
          &lt;name&gt;Dorothea&lt;/name&gt;
          &lt;countryCode&gt;DE&lt;/countryCode&gt;
      &lt;/contactPerson&gt;
      &lt;contactPerson&gt;
          &lt;name&gt;Fiona&lt;/name&gt;
          &lt;countryCode&gt;FR&lt;/countryCode&gt;
      &lt;/contactPerson&gt;
      &lt;contactPerson&gt;
          &lt;name&gt;Fabian&lt;/name&gt;
          &lt;countryCode&gt;FR&lt;/countryCode&gt;
      &lt;/contactPerson&gt;
      &lt;contactPerson&gt;
          &lt;name&gt;Florian&lt;/name&gt;
          &lt;countryCode&gt;FR&lt;/countryCode&gt;
      &lt;/contactPerson&gt;
      &lt;contactPerson&gt;
          &lt;name&gt;Gabi&lt;/name&gt;
          &lt;countryCode&gt;GB&lt;/countryCode&gt;
      &lt;/contactPerson&gt;
      &lt;contactPerson&gt;
          &lt;name&gt;Gert&lt;/name&gt;
          &lt;countryCode&gt;GB&lt;/countryCode&gt;
      &lt;/contactPerson&gt;
  &lt;/application&gt;
  </code></pre>
  
  <p>Now what I want to do is group the elements by country, meaning the result should look like this:</p>
  
  <pre><code>&lt;application&gt;
      &lt;memberState&gt;
          &lt;countryCode&gt;De&lt;/countryCode&gt;
          &lt;contactPerson&gt;
              &lt;name&gt;Dominik&lt;/name&gt;
          &lt;/contactPerson&gt;
          &lt;contactPerson&gt;
              &lt;name&gt;Dorothea&lt;/name&gt;
          &lt;/contactPerson&gt;
      &lt;/memberState&gt;
      &lt;memberState&gt;
          &lt;countryCode&gt;FR&lt;/countryCode&gt;
          &lt;contactPerson&gt;
              &lt;name&gt;Fiona&lt;/name&gt;
          &lt;/contactPerson&gt;
          &lt;contactPerson&gt;
              &lt;name&gt;Fabian&lt;/name&gt;
          &lt;/contactPerson&gt;
          &lt;contactPerson&gt;
              &lt;name&gt;Florian&lt;name&gt;
          &lt;/contactPerson&gt;
      &lt;/memberState&gt;
      &lt;memberState&gt;
          &lt;countryCode&gt;GB&lt;/countryCode&gt;
          &lt;contactPerson&gt;
              &lt;name&gt;Gabi&lt;/name&gt;
          &lt;/contactPerson&gt;
          &lt;contactPerson&gt;
              &lt;name&gt;Gert&lt;/name&gt;
          &lt;/contactPerson&gt;
      &lt;/memberState&gt;
  &lt;/application&gt;
  </code></pre>
  
  <p>I am using an XPath for-each pattern to select all countries, but it doesn't do what it is supposed to do. My pattern looks as follows:</p>
  
  <pre><code>  &lt;xsl:template match="/"&gt;
      &lt;application&gt;
        &lt;xsl:for-each select="/application/contactPerson/countryCode[not(.=preceding-sibling::*/application/contactPerson/countryCode)]"&gt;
          &lt;memberState&gt;
            &lt;countryCode&gt;
              &lt;xsl:value-of select="."/&gt;
            &lt;/countryCode&gt;
            &lt;contactPerson&gt;
              &lt;name&gt;
                &lt;xsl:value-of select="../name"/&gt;
              &lt;/name&gt;
            &lt;/contactPerson&gt;
          &lt;/memberState&gt;
        &lt;/xsl:for-each&gt;
      &lt;/application&gt;
    &lt;/xsl:template&gt;
  </code></pre>
  
  <p>The error is probably somewhere in the XPath expression which does not compile. I changed it to the following</p>
  
  <pre><code>&lt;xsl:for-each select="/application/contactPerson/countryCode[not(.=preceding-sibling::*)]"&gt;
  </code></pre>
  
  <p>because I think I am already at the right position of my tree. This solution compiles, but it doesn't give me a unique list of countries as I intended by using "preceding-sibling" but the complete list instead.</p>
  
  <p>Besides that I need the solution to my problem, I would be especially thankful for some help on understanding what is actually happening here.</p>
  
  <ol>
  <li>Is it possible to give a relative path as in my second solution or do I have to give the complete path each time?</li>
  <li>Am I on the right track for making a unique list of country codes, or would that generally be implemented differently?</li>
  <li>Is the solution I am looking for actually implementable with XSLT?</li>
  </ol>
  
  <p>I appreciate your help very much.</p>