Note that there are some explanatory texts on larger screens.

plurals
  1. POMuenchian Grouping - group within a node, not within the entire document
    text
    copied!<p>I'm trying to use Muenchian grouping in my XSLT to group matching nodes, but I only want to group within a parent node, not across the entire source XML document. </p> <p>Given XSLT and XML as follows (apologies for the length of my sample code): </p> <p><strong>XSLT</strong></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="contacts-by-surname" match="contact" use="surname" /&gt; &lt;xsl:template match="records"&gt; &lt;xsl:for-each select="contact[count(. | key('contacts-by-surname', surname)[1]) = 1]"&gt; &lt;xsl:sort select="surname" /&gt; &lt;xsl:value-of select="surname" /&gt;,&lt;br /&gt; &lt;xsl:for-each select="key('contacts-by-surname', 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><strong>XML</strong></p> <pre><code>&lt;root&gt; &lt;records&gt; &lt;contact id="0001"&gt; &lt;title&gt;Mr&lt;/title&gt; &lt;forename&gt;John&lt;/forename&gt; &lt;surname&gt;Smith&lt;/surname&gt; &lt;/contact&gt; &lt;contact id="0002"&gt; &lt;title&gt;Dr&lt;/title&gt; &lt;forename&gt;Amy&lt;/forename&gt; &lt;surname&gt;Jones&lt;/surname&gt; &lt;/contact&gt; &lt;contact id="0003"&gt; &lt;title&gt;Mrs&lt;/title&gt; &lt;forename&gt;Mary&lt;/forename&gt; &lt;surname&gt;Smith&lt;/surname&gt; &lt;/contact&gt; &lt;contact id="0004"&gt; &lt;title&gt;Ms&lt;/title&gt; &lt;forename&gt;Anne&lt;/forename&gt; &lt;surname&gt;Jones&lt;/surname&gt; &lt;/contact&gt; &lt;contact id="0005"&gt; &lt;title&gt;Mr&lt;/title&gt; &lt;forename&gt;Peter&lt;/forename&gt; &lt;surname&gt;Smith&lt;/surname&gt; &lt;/contact&gt; &lt;contact id="0006"&gt; &lt;title&gt;Dr&lt;/title&gt; &lt;forename&gt;Indy&lt;/forename&gt; &lt;surname&gt;Jones&lt;/surname&gt; &lt;/contact&gt; &lt;/records&gt; &lt;records&gt; &lt;contact id="0001"&gt; &lt;title&gt;Mr&lt;/title&gt; &lt;forename&gt;James&lt;/forename&gt; &lt;surname&gt;Smith&lt;/surname&gt; &lt;/contact&gt; &lt;contact id="0002"&gt; &lt;title&gt;Dr&lt;/title&gt; &lt;forename&gt;Mandy&lt;/forename&gt; &lt;surname&gt;Jones&lt;/surname&gt; &lt;/contact&gt; &lt;contact id="0003"&gt; &lt;title&gt;Mrs&lt;/title&gt; &lt;forename&gt;Elizabeth&lt;/forename&gt; &lt;surname&gt;Smith&lt;/surname&gt; &lt;/contact&gt; &lt;contact id="0004"&gt; &lt;title&gt;Ms&lt;/title&gt; &lt;forename&gt;Sally&lt;/forename&gt; &lt;surname&gt;Jones&lt;/surname&gt; &lt;/contact&gt; &lt;contact id="0005"&gt; &lt;title&gt;Mr&lt;/title&gt; &lt;forename&gt;George&lt;/forename&gt; &lt;surname&gt;Smith&lt;/surname&gt; &lt;/contact&gt; &lt;contact id="0006"&gt; &lt;title&gt;Dr&lt;/title&gt; &lt;forename&gt;Harry&lt;/forename&gt; &lt;surname&gt;Jones&lt;/surname&gt; &lt;/contact&gt; &lt;/records&gt; &lt;/root&gt; </code></pre> <p><strong>RESULT</strong></p> <pre><code>Jones, Amy (Dr) Anne (Ms) Harry (Dr) Indy (Dr) Mandy (Dr) Sally (Ms) Smith, Elizabeth (Mrs) George (Mr) James (Mr) John (Mr) Mary (Mrs) Peter (Mr) </code></pre> <p>How do I group within each <code>&lt;records&gt;</code> and achieve this 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>
 

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