[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] how with xPath 1


Subject: Re: [xsl] how with xPath 1
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Sat, 14 Apr 2012 11:53:35 -0400

At 2012-04-14 09:05 +0200, Leo Studer wrote:
in xPath 2.0 I retrieve the newest CDs for each artist like that

for $i in distinct-values(//artist) return (//cd[artist eq $i and year= max(//cd[artist eq $i ]/year)])

How can I do that with xPath 1?

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


-- Public XSLT, XSL-FO, UBL and code list classes in Europe -- May 2012 Contact us for world-wide XML consulting and instructor-led training Free 5-hour lecture: http://www.CraneSoftwrights.com/links/udemy.htm Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Google+ profile: https://plus.google.com/116832879756988317389/about Legal business disclaimers: http://www.CraneSoftwrights.com/legal


Current Thread
Keywords