473,382 Members | 1,665 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Very noob question about XSLT, I think

I am trying to organize the toy (sample) data set below, so that each year appears only once and each artist appears only once within each year. I've created the XSLT so that the year only appears once, but I don't know how to repeat that within the year for the artist. Can someone provide a clue?

In the example, in 1987 Otis Redding should appear only once with both Titles listed. In 1995, Kenny Rogers should appear only once with both titles listed.

=== Current XSLT: Displays artists sorted by year ====
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <!-- Edited by XMLSpy® -->
  3. <xsl:stylesheet version="1.0"
  4. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  5. <xsl:key name="cdlist" match="cd" use="year" /> 
  6. <xsl:template match="/">
  7.   <html>
  8.   <body>
  9.     <table border="1">
  10.       <tr bgcolor="#9acd32">
  11.         <th>Year</th>
  12.         <th>Artist</th>
  13.         <th>Title</th>
  14.       </tr>
  15.      <!-- Loop through the catalog year by year -->
  16.      <xsl:for-each select="//cd[generate-id(.)=generate-id(key('cdlist', year))]"> 
  17.      <!-- Sort by Year in ascending order -->
  18.      <xsl:sort select="year" order="ascending"/>
  19.      <!-- Display all cd entries for a given year -->
  20.      <xsl:for-each select="key('cdlist', year)">
  21.      <!-- Sort Primary Key by Artist name in ascending order -->
  22.      <xsl:sort select="artist" order="ascending"/>
  23.      <tr>
  24.         <xsl:choose>
  25.              <xsl:when test="position()=1">
  26.                  <td><xsl:value-of select="year"/></td>
  27.              </xsl:when>
  28.              <xsl:otherwise>
  29.                  <td></td>
  30.              </xsl:otherwise>
  31.         </xsl:choose>
  32.         <td><xsl:value-of select="artist"/></td>
  33.         <td><xsl:value-of select="title"/></td>
  34.       </tr>
  35.       </xsl:for-each>
  36.       </xsl:for-each>
  37.     </table>
  38.   </body>
  39.   </html>
  40. </xsl:template>
  41. </xsl:stylesheet>
=== Sample DataSet ===
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <!-- Edited by XMLSpy® -->
  3. <catalog>
  4.     <cd>
  5.         <title>Empire Burlesque</title>
  6.         <artist>Bob Dylan</artist>
  7.         <country>USA</country>
  8.         <company>Columbia</company>
  9.         <price>10.90</price>
  10.         <year>1985</year>
  11.     </cd>
  12.     <cd>
  13.         <title>Hide your heart</title>
  14.         <artist>Bonnie Tyler</artist>
  15.         <country>UK</country>
  16.         <company>CBS Records</company>
  17.         <price>9.90</price>
  18.         <year>1988</year>
  19.     </cd>
  20.     <cd>
  21.         <title>Greatest Hits</title>
  22.         <artist>Dolly Parton</artist>
  23.         <country>USA</country>
  24.         <company>RCA</company>
  25.         <price>9.90</price>
  26.         <year>1982</year>
  27.     </cd>
  28.     <cd>
  29.         <title>Still got the blues</title>
  30.         <artist>Gary Moore</artist>
  31.         <country>UK</country>
  32.         <company>Virgin records</company>
  33.         <price>10.20</price>
  34.         <year>1990</year>
  35.     </cd>
  36.     <cd>
  37.         <title>Eros</title>
  38.         <artist>Eros Ramazzotti</artist>
  39.         <country>EU</country>
  40.         <company>BMG</company>
  41.         <price>9.90</price>
  42.         <year>1997</year>
  43.     </cd>
  44.     <cd>
  45.         <title>One night only</title>
  46.         <artist>Otis Redding</artist>
  47.         <country>UK</country>
  48.         <company>Polydor</company>
  49.         <price>10.90</price>
  50.         <year>1998</year>
  51.     </cd>
  52.     <cd>
  53.         <title>Sylvias Mother</title>
  54.         <artist>Joe Cocker</artist>
  55.         <country>UK</country>
  56.         <company>CBS</company>
  57.         <price>8.10</price>
  58.         <year>1973</year>
  59.     </cd>
  60.     <cd>
  61.         <title>Maggie May</title>
  62.         <artist>Rod Stewart</artist>
  63.         <country>UK</country>
  64.         <company>Pickwick</company>
  65.         <price>8.50</price>
  66.         <year>1990</year>
  67.     </cd>
  68.     <cd>
  69.         <title>Romanza</title>
  70.         <artist>Andrea Bocelli</artist>
  71.         <country>EU</country>
  72.         <company>Polydor</company>
  73.         <price>10.80</price>
  74.         <year>1996</year>
  75.     </cd>
  76.     <cd>
  77.         <title>When a man loves a woman</title>
  78.         <artist>Percy Sledge</artist>
  79.         <country>USA</country>
  80.         <company>Atlantic</company>
  81.         <price>8.70</price>
  82.         <year>1987</year>
  83.     </cd>
  84.     <cd>
  85.         <title>Black angel</title>
  86.         <artist>Kenny Rogers</artist>
  87.         <country>EU</country>
  88.         <company>Mega</company>
  89.         <price>10.90</price>
  90.         <year>1995</year>
  91.     </cd>
  92.     <cd>
  93.         <title>1999 Grammy Nominees</title>
  94.         <artist>Many</artist>
  95.         <country>USA</country>
  96.         <company>Grammy</company>
  97.         <price>10.20</price>
  98.         <year>1999</year>
  99.     </cd>
  100.     <cd>
  101.         <title>For the good times</title>
  102.         <artist>Kenny Rogers</artist>
  103.         <country>UK</country>
  104.         <company>Mucik Master</company>
  105.         <price>8.70</price>
  106.         <year>1995</year>
  107.     </cd>
  108.     <cd>
  109.         <title>Big Willie style</title>
  110.         <artist>Joe Cocker</artist>
  111.         <country>USA</country>
  112.         <company>Columbia</company>
  113.         <price>9.90</price>
  114.         <year>1997</year>
  115.     </cd>
  116.     <cd>
  117.         <title>Tupelo Honey</title>
  118.         <artist>Van Morrison</artist>
  119.         <country>UK</country>
  120.         <company>Polydor</company>
  121.         <price>8.20</price>
  122.         <year>1971</year>
  123.     </cd>
  124.     <cd>
  125.         <title>Soulsville</title>
  126.         <artist>Joe Cocker</artist>
  127.         <country>Norway</country>
  128.         <company>WEA</company>
  129.         <price>7.90</price>
  130.         <year>1996</year>
  131.     </cd>
  132.     <cd>
  133.         <title>The very best of</title>
  134.         <artist>Cat Stevens</artist>
  135.         <country>UK</country>
  136.         <company>Island</company>
  137.         <price>8.90</price>
  138.         <year>1990</year>
  139.     </cd>
  140.     <cd>
  141.         <title>Stop</title>
  142.         <artist>Sam Brown</artist>
  143.         <country>UK</country>
  144.         <company>A and M</company>
  145.         <price>8.90</price>
  146.         <year>1988</year>
  147.     </cd>
  148.     <cd>
  149.         <title>Bridge of Spies</title>
  150.         <artist>T`Pau</artist>
  151.         <country>UK</country>
  152.         <company>Siren</company>
  153.         <price>7.90</price>
  154.         <year>1987</year>
  155.     </cd>
  156.     <cd>
  157.         <title>Private Dancer</title>
  158.         <artist>Tina Turner</artist>
  159.         <country>UK</country>
  160.         <company>Capitol</company>
  161.         <price>8.90</price>
  162.         <year>1983</year>
  163.     </cd>
  164.     <cd>
  165.         <title>Midt om natten</title>
  166.         <artist>Kim Larsen</artist>
  167.         <country>EU</country>
  168.         <company>Medley</company>
  169.         <price>7.80</price>
  170.         <year>1983</year>
  171.     </cd>
  172.     <cd>
  173.         <title>Pavarotti Gala Concert</title>
  174.         <artist>Luciano Pavarotti</artist>
  175.         <country>UK</country>
  176.         <company>DECCA</company>
  177.         <price>9.90</price>
  178.         <year>1991</year>
  179.     </cd>
  180.     <cd>
  181.         <title>The dock of the bay</title>
  182.         <artist>Otis Redding</artist>
  183.         <country>USA</country>
  184.         <company>Atlantic</company>
  185.         <price>7.90</price>
  186.         <year>1987</year>
  187.     </cd>
  188.     <cd>
  189.         <title>Picture book</title>
  190.         <artist>Simply Red</artist>
  191.         <country>EU</country>
  192.         <company>Elektra</company>
  193.         <price>7.20</price>
  194.         <year>1985</year>
  195.     </cd>
  196.     <cd>
  197.         <title>Red</title>
  198.         <artist>The Communards</artist>
  199.         <country>UK</country>
  200.         <company>London</company>
  201.         <price>7.80</price>
  202.         <year>1987</year>
  203.     </cd>
  204.     <cd>
  205.         <title>Unchain my heart</title>
  206.         <artist>Joe Cocker</artist>
  207.         <country>USA</country>
  208.         <company>EMI</company>
  209.         <price>8.20</price>
  210.         <year>1987</year>
  211.     </cd>
  212.     <cd>
  213.         <title>Another dock of the bay</title>
  214.         <artist>Otis Redding</artist>
  215.         <country>USA</country>
  216.         <company>Atlantic</company>
  217.         <price>7.90</price>
  218.         <year>1987</year>
  219.     </cd>
  220. </catalog>
Jun 17 '10 #1

✓ answered by jkmyoung

There's a couple of ways. The easiest way to integrate with your current code is probably to make a second key:
<xsl:key name="cdlist2" match="cd" use="concat(year,artist)"/>

And then in place of the artist cell have:
Expand|Select|Wrap|Line Numbers
  1. <xsl:choose> 
  2.      <xsl:when test="generate-id(.)=generate-id(key('cdlist2',concat(year,artist)))"> 
  3.          <td><xsl:value-of select="artist"/></td> 
  4.      </xsl:when> 
  5.      <xsl:otherwise> 
  6.          <td></td> 
  7.      </xsl:otherwise> 
  8. </xsl:choose> 

2 1689
jkmyoung
2,057 Expert 2GB
There's a couple of ways. The easiest way to integrate with your current code is probably to make a second key:
<xsl:key name="cdlist2" match="cd" use="concat(year,artist)"/>

And then in place of the artist cell have:
Expand|Select|Wrap|Line Numbers
  1. <xsl:choose> 
  2.      <xsl:when test="generate-id(.)=generate-id(key('cdlist2',concat(year,artist)))"> 
  3.          <td><xsl:value-of select="artist"/></td> 
  4.      </xsl:when> 
  5.      <xsl:otherwise> 
  6.          <td></td> 
  7.      </xsl:otherwise> 
  8. </xsl:choose> 
Jun 18 '10 #2
@jkmyoung
Perfect. Does exactly what I was asking for. Thanks.
Jun 18 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Ndeye | last post by:
Hi all I have this xml file (generated from Lotus Domino 6) and would like to transform it to a simpler one by extracting only data between the gif tag (I cut the gif data to reduce the file...
1
by: mountain1228 | last post by:
Hello, I apoligize in advance if this post is off-topic in this group. I have been converting xml to html with an xslt stylesheet using the program xslt-parser that comes with RH 7.3. The...
1
by: sleepyant | last post by:
Hi, I'm using VB .NET 2003. I have a form which only has a CrystalReportViewer to view a report on screen. But every time I run then project, the form is shown but the Report itself is very slow to...
2
by: gene.ellis | last post by:
Hello everyone. I have a pretty straight forward question: I have some data stored in an XMl document in the format of: <comment_info> <comments>These are the comments</comments>...
2
by: ezelasky | last post by:
Very new to dot net & more familiar with COM+, can someone send me a link on how to create a single file assembly as a dll? Do I do this in ..NET visual IDE or from the command line? How does one...
1
by: Toxician | last post by:
why can i use javascrtip to output the file below into html and also able to use xslt to do the same thing ??? xml.xml <root> <node>This is a node </node> <root> ************************...
4
by: Grant Robertson | last post by:
Can an XML Stylesheet Language Transform take data that is spread out in multiple different XML files and not necessarily in proper order, rearrange that data into it's proper order and then...
1
by: Stephen D Cook | last post by:
I am trying to tie a column in a database to a dropdown list, but having very little luck. The table is bound to the .aspx page via oledbadapter1 and oleconnection1, but when I try to create a...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.