Anders
Anders

Reputation: 12716

XSLT Nested Grouping

I have a problem with a nested grouping in XSLT 2.0. I am able to almost achieve the desired results, but not quite, and I'm getting confused by the nesting, so hoping to get some help here.

The problem is, some elements have multiple product or subject classifications, and this works for the subject element grouping, but not for product. I'm sure it's a simple mistake, I just cannot see it right now...

Here's the sample XML file to be processed:

<metadata>
    <file id="1" title="Dimensions">
        <subject>Technical Data</subject>
        <product>A</product>
    </file>
    <file id="2" title="Noise and Vibrations">
        <subject>Technical Data</subject>
        <product>B</product>
    </file>
    <file id="3" title="Overview">
        <subject>Product Description</subject>
        <product>A</product>
        <product>B</product>
    </file>
    <file id="4" title="Main Components">
        <subject>Product Description</subject>
        <subject>Technical Data</subject>
        <product>B</product>
    </file>
</metadata>

And here's the XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">

        <xsl:output method="xml" indent="yes"/>
        <xsl:template match="metadata">
            <ul>
                <xsl:for-each-group select="file" group-by="subject">
                    <li>
                        <xsl:value-of select="subject"/>
                        <ul>
                            <xsl:for-each-group select="current-group()" group-by="product">
                                <li>
                                    <xsl:value-of select="product"/>
                                    <ul>
                                        <xsl:for-each select="current-group()">
                                            <li>
                                                <xsl:value-of select="@title"/>
                                            </li>
                                        </xsl:for-each>
                                    </ul>
                                </li>
                            </xsl:for-each-group>
                        </ul>
                    </li>
                </xsl:for-each-group>
            </ul>
        </xsl:template>

</xsl:stylesheet>

This is the current result:

<ul>
   <li>Technical Data
      <ul>
         <li>A
            <ul>
               <li>Dimensions</li>
            </ul>
         </li>
         <li>B
            <ul>
               <li>Noise and Vibrations</li>
               <li>Main Components</li>
            </ul>
         </li>
      </ul>
   </li>
   <li>Product Description<ul>
         <li>A B
            <ul>
               <li>Overview</li>
            </ul>
         </li>
         <li>A B
            <ul>
               <li>Overview</li>
               <li>Main Components</li>
            </ul>
         </li>
      </ul>
   </li>
</ul>

But what I want is this ("Overview" under both A and B product elements, but "Main Components" under just B):

<ul>
   <li>Technical Data
      <ul>
         <li>A
            <ul>
               <li>Dimensions</li>
            </ul>
         </li>
         <li>B
            <ul>
               <li>Noise and Vibrations</li>
               <li>Main Components</li>
            </ul>
         </li>
      </ul>
   </li>
   <li>Product Description<ul>
         <li>A
            <ul>
               <li>Overview</li>
            </ul>
         </li>
         <li>B
            <ul>
               <li>Overview</li>
               <li>Main Components</li>
            </ul>
         </li>
      </ul>
   </li>
</ul>

Upvotes: 1

Views: 668

Answers (1)

Joel M. Lamsen
Joel M. Lamsen

Reputation: 7173

change

<xsl:value-of select="product"/>

to

<xsl:value-of select="current-grouping-key()"/>

Upvotes: 3

Related Questions