Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 29th, 2014, 01:28 PM
Registered User
 
Join Date: Jan 2014
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Grouping with XSLT 1.0

I have an XML of that kind:

Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <Returns>
     <Row>
      <Application>
       app1
      </Application>
      <Component>
       comp1
      </Component>
      <Version>
       1
      </Version>
     </Row>
     <Row>
      <Application>
       app1
      </Application>
      <Component>
       comp2
      </Component>
      <Version>
       1
      </Version>
     </Row>
     <Row>
      <Application>
       app1
      </Application>
      <Component>
       comp1
      </Component>
      <Version>
       2
      </Version>
     </Row>
     <Row>
      <Application>
       app1
      </Application>
      <Component>
       comp2
      </Component>
      <Version>
       2
      </Version>
     </Row>
     <Row>
      <Application>
       app2
      </Application>
      <Component>
       comp1
      </Component>
      <Version>
       1
      </Version>
     </Row>
      <Row>
      <Application>
       app2
      </Application>
      <Component>
       comp2
      </Component>
      <Version>
       1
      </Version>
     </Row>
      <Row>
      <Application>
       app2
      </Application>
      <Component>
       comp1
      </Component>
      <Version>
       2
      </Version>
     </Row>
      <Row>
      <Application>
       app2
      </Application>
      <Component>
       comp2
      </Component>
      <Version>
       2
      </Version>
     </Row>
    </Returns>
I need to transform it to something like this:

Code:
        <?xml version="1.0" encoding="UTF-8"?>
    <Components>
       <Component application="app1" version="2" name="comp1"/>
       <Component application="app1" version="2" name="comp2"/>
       <Component application="app2" version="2" name="comp1"/>
       <Component application="app2" version="2" name="comp2"/>
    </Components>
The goal is to show only the components that belong to the latest version of each application.

I wrote this XSL template that uses muenchian grouping:

Code:
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" />
    <xsl:key name="application" match="Row" use="Application" />
    <xsl:template match="Returns">
        <Components>
            <xsl:apply-templates select="Row[generate-id(.)=generate-id(key('application',Application)[1])]">
            </xsl:apply-templates>
        </Components>
    </xsl:template>
     <xsl:template match="Row">
            <xsl:for-each select="key('application', Application)">
                        <xsl:sort select="Version" order="descending"/>
                      <xsl:for-each select="Version">
                        <Component application="{normalize-space(../Application)}" version="{normalize-space(current())}" name="{normalize-space(../Component)}"></Component>                      
                     </xsl:for-each>
            </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>
And now I get this output:

Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <Components>
       <Component application="app1" version="2" name="comp1"/>
       <Component application="app1" version="2" name="comp2"/>
       <Component application="app1" version="1" name="comp1"/>
       <Component application="app1" version="1" name="comp2"/>
       <Component application="app2" version="2" name="comp1"/>
       <Component application="app2" version="2" name="comp2"/>
       <Component application="app2" version="1" name="comp1"/>
       <Component application="app2" version="1" name="comp2"/>
    </Components>
How can I filter the components to get only the ones that belong to the latest version of the same application?

Thanks,
Valerio





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT grouping - need help Half-Saint XSLT 9 January 23rd, 2009 11:10 AM
Grouping with XSLT 1.0 rodmcleay XSLT 1 January 14th, 2008 11:42 PM
XSLT Grouping vernc XSLT 2 October 2nd, 2007 03:59 AM
XSLT 1.0 Grouping kwilliams XSLT 0 January 11th, 2006 06:30 PM
XSLT Grouping Help Missy XSLT 0 December 14th, 2005 10:28 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.