Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Took me some time ... I was about to give up but continued nevertheless :)</p> <p>The drawback of the key function is that the key generated will always be for the entire xml. Hence you should concatenate additional information in your key to make it more specific. In the e.g. below, I am concatenating the position of records node, so that I get keys for distinct surnames per records.</p> <p>Here's the 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="html" indent="yes"/&gt; &lt;xsl:key name="distinct-surname" match="contact" use="concat(generate-id(..), '|', surname)"/&gt; &lt;xsl:template match="records"&gt; &lt;xsl:for-each select="contact[generate-id() = generate-id(key('distinct-surname', concat(generate-id(..), '|', surname))[1])]"&gt; &lt;xsl:sort select="surname" /&gt; &lt;xsl:value-of select="surname" /&gt;,&lt;br /&gt; &lt;xsl:for-each select="key('distinct-surname', concat(generate-id(..), '|', surname))"&gt; &lt;xsl:sort select="forename" /&gt; &lt;xsl:value-of select="forename" /&gt; (&lt;xsl:value-of select="title" /&gt;)&lt;br /&gt; &lt;/xsl:for-each&gt; &lt;/xsl:for-each&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>This is the result:</p> <pre><code>Jones, Amy (Dr) Anne (Ms) Indy (Dr) Smith, John (Mr) Mary (Mrs) Peter (Mr) Jones, Harry (Dr) Mandy (Dr) Sally (Ms) Smith, Elizabeth (Mrs) George (Mr) James (Mr) </code></pre> <p>Please note that the result is sorted on the forenames too. If you don't want to sort it on forenames, you need to remove the line <code>&lt;xsl:sort select="forename" /&gt;</code></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload