[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: how with xPath 1

Subject: Re: how with xPath 1
From: Leo Studer <leo.studer@xxxxxxxxxxx>
Date: Sat, 14 Apr 2012 22:03:50 +0200
Re:  how with xPath 1
Hi Ken

thank you for your lines.

The question came up in class. I teach xPath and asked the students how to
retrieve the first CD of each artist.
Here the xPath 1 which does the job:
	//cd[not(artist = preceding-sibling::cd/artist)]

A student asked how to retrieve the newest CD of each artist.
I told the student, that I know how to do that with xPath 2.0 and gave this
solution
	for $i in distinct-values(//artist) return (//cd[artist eq $i and year=
max(//cd[artist eq $i ]/year)])

The problem is, that in xPath 1 we have neither "for" nor "distinct-values()".
So I wonder, is it possible to retrieve the newest cd of each artist with
xPath 1 only.

Your proposition with XSLT 1 is nice, thank you.

Greetings
Leo




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 &lt; 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




T:\ftemp>

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.