xsl multiple grouping

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • GMKal
    New Member
    • Aug 2010
    • 2

    xsl multiple grouping

    hello, I have a difficulty achieving the results I need and I hope that someone here can help me

    I have an xml like this
    Code:
    <products>
            <Product>
                    <ID>1</ID>
                    <Brand>brand1</Brand>
                    <Features>
                            <Feature>feature1</Feature>
                            <Feature>feature2</Feature>
                            <Feature>feature3</Feature>
                    </Features>
            </Product>
            <Product>
                    <ID>2</ID>
                    <Brand>brand1</Brand>
                    <Features>
                            <Feature>feature2</Feature>                        
                            <Feature>feature3</Feature>
                    </Features>
            </Product>
            <Product>
                    <ID>3</ID>
                    <Brand>brand2</Brand>
                    <Features>
                            <Feature>feature1</Feature>
                            <Feature>feature2</Feature>                        
                    </Features>
            </Product>
    </products>
    I need to group the products both by brand AND feature. That means that the result should look like this:
    Code:
    <groups>
            <group>
                    <Brand>1</Brand>
                    <Feature>feature1</feature>
                    <Product>1</Product>
            </group>
            <group>
                    <Brand>1</Brand>
                    <Feature>feature2</Feature>
                    <Product>1</Product>
                    <Product>2</Product>
            </group>
            <group>
                    <Brand>1</Brand>
                    <Feature>feature3</Feature>
                    <Product>1</Product>
                    <Product>2</Product>
            </group>
            <group>
                    <Brand>2</Brand>
                    <Feature>feature1</Feature>
                    <Product>3</Product>                
            </group>
            <group>
                    <Brand>2</Brand>
                    <Feature>feature2</Feature>
                    <Product>3</Product>                
            </group>
    </groups>
    Any help would be very welcome....
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Assuming brand feature is unique, Muenchian grouping:

    Code:
    <xsl:key name="brandfeature" match="feature" use="concat(../../Brand, .)"/>
    
    <xsl:for-each select="/products/Product/Features/Feature[count(.|key('brandfeature', concat(../../Brand, .))[1]) = 1]">
    	<group>
    		<Brand><xsl:value-of select="../../Brand"/></Brand>
    		<Feature><xsl:value-of select="."/></Feature>
    		<xsl:for-each select="key('brandfeature', concat(../../Brand, .))">
    			<Product><xsl:value-of select="../../ID"/></Product>
    		</xsl:for-each>
    	</group>
    </xsl:for-each>

    Comment

    • GMKal
      New Member
      • Aug 2010
      • 2

      #3
      That worked, than you very much

      Comment

      Working...