Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well, I gave up on the keys and Muenchian grouping for the time being. I barely understand it, and hacking away at it didn't yield the desired results. I understand recursion, tho, and so I went with this recursive approach that produces the desired output. I found it at <a href="http://www.biglist.com/lists/xsl-list/archives/200412/msg00865.html" rel="nofollow noreferrer">http://www.biglist.com/lists/xsl-list/archives/200412/msg00865.html</a></p> <p>The discussion thread warns that performance suffers on large input vs. the Muenchian approach, and the solution below is verbose and repetitive (I could prolly refactor it to make it smaller and harder to understand ;-) but 1) it actually <strong>works</strong> for me, and 2) for my present problem the input sets are pretty small, no more than a dozen or so bottom-level Part nodes.</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;!-- recursive grouping http://www.biglist.com/lists/xsl-list/archives/200412/msg00865.html --&gt; &lt;xsl:template match="//Plans"&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;test grouping&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;ol&gt; &lt;xsl:call-template name="PlanGrouping"&gt; &lt;xsl:with-param name="list" select="Plan"/&gt; &lt;/xsl:call-template&gt; &lt;/ol&gt; &lt;/body&gt; &lt;/html&gt; &lt;/xsl:template&gt; &lt;xsl:template name="PlanGrouping"&gt; &lt;xsl:param name="list"/&gt; &lt;!-- Selecting the first Area ID as group identifier and the group itself--&gt; &lt;xsl:variable name="group-identifier" select="$list[1]/@AreaID"/&gt; &lt;xsl:variable name="group" select="$list[@AreaID = $group-identifier]"/&gt; &lt;!-- Do some work for the group --&gt; &lt;li&gt; Area &lt;xsl:value-of select="$group-identifier"/&gt;: &lt;ol&gt; &lt;xsl:call-template name="AreaGrouping"&gt; &lt;xsl:with-param name="list" select="$list[(@AreaID = $group-identifier)]"/&gt; &lt;/xsl:call-template&gt; &lt;/ol&gt; &lt;/li&gt; &lt;!-- If there are other groups left, calls itself --&gt; &lt;xsl:if test="count($list)&gt;count($group)"&gt; &lt;xsl:call-template name="PlanGrouping"&gt; &lt;xsl:with-param name="list" select="$list[not(@AreaID = $group-identifier)]"/&gt; &lt;/xsl:call-template&gt; &lt;/xsl:if&gt; &lt;/xsl:template&gt; &lt;xsl:template name="AreaGrouping"&gt; &lt;xsl:param name="list"/&gt; &lt;!-- Selecting the first Unit ID as group identifier and the group itself--&gt; &lt;xsl:variable name="group-identifier" select="$list[1]/@UnitID"/&gt; &lt;xsl:variable name="group" select="$list[@UnitID = $group-identifier]"/&gt; &lt;!-- Do some work for the group --&gt; &lt;li&gt; Unit &lt;xsl:value-of select="$group-identifier"/&gt;: &lt;ol&gt; &lt;xsl:call-template name="Parts"&gt; &lt;xsl:with-param name="list" select="$list[(@UnitID = $group-identifier)]"/&gt; &lt;/xsl:call-template&gt; &lt;/ol&gt; &lt;/li&gt; &lt;!-- If there are other groups left, calls itself --&gt; &lt;xsl:if test="count($list)&gt;count($group)"&gt; &lt;xsl:call-template name="AreaGrouping"&gt; &lt;xsl:with-param name="list" select="$list[not(@UnitID = $group-identifier)]"/&gt; &lt;/xsl:call-template&gt; &lt;/xsl:if&gt; &lt;/xsl:template&gt; &lt;xsl:template name="Parts"&gt; &lt;xsl:param name="list"/&gt; &lt;xsl:for-each select="$list/Part"&gt; &lt;li&gt; Part &lt;xsl:value-of select="@ID"/&gt; (&lt;xsl:value-of select="@Name"/&gt;) &lt;/li&gt; &lt;/xsl:for-each&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </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