[Home] [By Thread] [By Date] [Recent Entries]
At 2012-04-14 09:05 +0200, Leo Studer wrote:
in xPath 2.0 I retrieve the newest CDs for each artist like that You don't give much data to experiment with, nor do you say what you've tried in XSLT 1.0 so far to know how to guide you to a solution. But I think the example below does what you need. More testing is required but inspecting the select= address I'm pretty sure it is meets your need. I hope this helps. . . . . . . . . . Ken T:\ftemp>type leo.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<cdcatalog>
<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>The Times They Are a-Changin'</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1964</year>
</cd>
<cd>
<title>Faster Than The Speed of Light</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>Columbia</company>
<price>9.90</price>
<year>1983</year>
</cd>
<cd>
<title>Time Out of Mind</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1997</year>
</cd>
</cdcatalog>
T:\ftemp>xslt leo.xml leo.xsl
<?xml version="1.0" encoding="utf-8"?>
For Bonnie Tyler found Hide your heart 1988
For Bob Dylan found Time Out of Mind 1997
T:\ftemp>type leo.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"><xsl:key name="cds-by-artist" match="cd" use="artist"/> <xsl:template match="cdcatalog">
<!--select those in the latest year for each artist-->
<xsl:for-each select="cd[not(year < key('cds-by-artist',artist)/year)]">
<!--report findings-->
<xsl:text>
</xsl:text>
<xsl:value-of select="concat('For ',artist,' found ',title,' ',year)"/>
</xsl:for-each>
</xsl:template></xsl:stylesheet> T:\ftemp>xquery leo.xquery <?xml version="1.0" encoding="UTF-8"?> <cd> <title>Time Out of Mind</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1997</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> T:\ftemp>type leo.xquery for $d in doc('leo.xml') return for $i in distinct-values($d//artist) return ($d//cd[artist eq $i and year= max( $d//cd[artist eq $i ]/year)]) T:\ftemp>
|

Cart



