How to to group sort and combine XML data?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jamie Moore

    How to to group sort and combine XML data?

    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <catalog>
    	<cd>
    		<title>Empire Burlesque</title>
    		<artist>Bob Dylan</artist>
    		<country>USA</country>
    		<company>Columbia</company>
    		<price>10.90</price>
    		<year>1985</year>
    	</cd>
    	<cd>
    		<title>Hide your heart</title>
    		<artist>Bonnie Tyler</artist>
    		<country>UK</country>
    		<company>CBS Records</company>
    		<price>9.90</price>
    		<year>1988</year>
    	</cd>
    	<cd>
    		<title>Greatest Hits</title>
    		<artist>Dolly Parton</artist>
                    <country>USA</country>
    		<company>RCA</company>
    		<price>9.90</price>
    		<year>1982</year>
    	</cd>
    	<cd>
    		<title>Still got the blues</title>
    		<artist>Dolly Parton</artist>
    		<country>UK</country>
    		<company>Virgin records</company>
    		<price>10.20</price>
    		<year>1991</year>
    	</cd>
    	<cd>
    		<title>Eros</title>
    		<artist>Gary Moore</artist>
    		<country>EU</country>
    		<company>BMG</company>
    		<price>9.90</price>
    		<year>1997</year>
    	</cd>
    	<cd>
    		<title>One night only</title>
    		<artist>Bee Gees</artist>
    		<country>UK</country>
    		<company>Polydor</company>
    		<price>10.90</price>
    		<year>1998</year>
    	</cd>
    </catalog>
    *************** ******
    I need the output to look like:Group by Artist, sort by title
    Dolly Parton
    1. Greatest Hits, USA, RCA, 9.90, 1982
    2. Still got the Blues, UK, Virgin Records, 10.20, 1991

    BeeGees
    1. One night only, UK, Polydor, 10.90, 1998

    etc
    Last edited by MMcCarthy; Oct 27 '10, 11:21 AM. Reason: added code tags
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2

    Code:
    <xsl:key name="cd-by-artist" match="cd" use="artist" />
    
    <xsl:template match="catalog">
    	<xsl:for-each select="cd[count(. | key('cd-by-artist', artist)[1]) = 1]">
    		<xsl:sort select="artist" />
    		<xsl:value-of select="artist" />,<br />
    		<xsl:for-each select="key('cd-by-artist', artist)">
    			<xsl:sort select="title" />
    			<xsl:value-of select="title" /> (<xsl:value-of select="other information etc... " />)<br />
    		</xsl:for-each>
    	</xsl:for-each>
    </xsl:template>

    Comment

    Working...