OK, I KNOW that variations on this have been asked and answered; I've been reading them all day, but I'm still stuck. So, here goes:
I need to create a summary list in HTML from some XML.
Given this XML:
<Root><!-- yes, I know I don't need a 'Root' element! Legacy code... -->
<Plans>
<Plan AreaID="1" UnitID="83">
<Part ID="9122" Name="foo" />
<Part ID="9126" Name="bar" />
</Plan>
<Plan AreaID="1" UnitID="86">
<Part ID="8650" Name="baz" />
</Plan>
<Plan AreaID="2" UnitID="26">
<Part ID="215" Name="quux" />
</Plan>
<Plan AreaID="1" UnitID="95">
<Part ID="7350" Name="meh" />
</Plan>
</Plans>
</Root>
I need to emit:
<ol>
<li>Area 1:
<ol><!-- units in Area 1 -->
<li>Unit 83:
<ol>
<li>Part 9122 (foo)</li>
<li>Part 9126 (bar)</li>
</ol>
</li>
<li>Unit 86:
<ol>
<li>Part 8650 (baz)</li>
</ol>
<li>Unit 95:
<ol>
<li>Part 7350 (meh)</li>
</ol>
</li>
</ol><!-- /units in Area 1-->
</li>
<li>Area 2:
<ol><!-- units in Area 2 -->
<li>Unit 26:
<ol>
<li>Part 215 (quux)</li>
</ol>
</li>
</ol><!-- /units in Area 2-->
</li>
</ol>
I have the outer grouping working -- I get top-level list elements for Area 1 and 2. But I can't get the sequences of Units in the Areas -- I either get no output, or repeating the same value. I haven't even got down to the Part level :-(
I've been working on a stylesheet like this:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
<xsl:output method="html" indent="yes"/>
<xsl:key name="kAreaID" match="Plan" use="@AreaID" />
<xsl:key name="kUnitID" match="Plan" use="@UnitID" />
<xsl:template match="/Root/Plans">
<html><head><title>test grouping</title></head>
<body>
<ol>
<xsl:for-each select="./Plan[generate-id(.) =
generate-id( key( 'kAreaID', @AreaID )[1] )]"
>
<xsl:sort order="ascending" select="./@AreaID" />
<li>Area <xsl:value-of select="@AreaID"/>:
<ol>
<xsl:for-each select="key( 'kUnitID', @UnitID )">
<li>Unit <xsl:value-of select="@UnitID"/>:
<ol>
<li>(Parts go here...)</li>
</ol>
</li>
</xsl:for-each>
</ol>
</li>
</xsl:for-each>
</ol>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Any help is greatly appreciated!
This does what you want but with recursion, not grouping. Sorry I am still learning how to use grouping too:
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 http://www.biglist.com/lists/xsl-list/archives/200412/msg00865.html
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 works 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.
I don't think you need to use the kUnitID key at all. Instead replace the following line...
..with this line instead, which should loop over all parts matching the current AreaID
And within this loop, for your (Parts go here...) code, you can then simply loop over the parts
Here is the Muenchian grouping solution you are looking for.
Going from the original XML you provided, I thought grouping by AreaID would be enough, but it turns out that a second grouping by UnitID is also needed.
Here is my modified XSLT 1.0 solution. It's not a lot more complex than the original solution:
Since your XML is missing it, I added a UnitID to group on:
And here is the output:
Since you seem to have a hard time with the XSL key, here my attempt of an explanation:
An
<xsl:key>
is absolutely equivalent to the associative array (map, hash, whatever you call it) known to many programming languages. This:generates a data structure that could be expressed in JavaScript like this:
The function to access the data structure is called
key()
. So, this XPath expression:is the logical equivalent of (in JavaScript, again):
returning an array (a node-set, more correctly) of all nodes matching the given key string (the key is always a string). This node-set can be used like any other node-set in XSLT, i.e. like the ones you retrieve through "traditional" XPath. This means you can apply conditions (predicates) to it:
or use it as a base for XPath navigation:
and so on. This also means we can use it as a "select" expression for
<xsl:apply-templates>
, and we can use it as a base for grouping. Which leads us to the core of the above stylesheet (if you have wrapped your head around this one, you've understood the rest of the solution as well):In JavaScript again, this could be expressed as:
After the expression is done, only those nodes are selected that are the respective first ones with a given "AreaID, UnitID" combination - effectively we have grouped them on their "AreaID, UnitID" combination.
Applying a template to this node-set causes every combination to appear only once. My
<xsl:template match="Plan" mode="unit-group">
then retrieves the full list again to achieve complete output for each group.I hope the use of JavaScript to explain the concept was a helpful idea.