Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML to HTML using XSL, How do you Group By an attribute?

Status
Not open for further replies.

din112

Programmer
Oct 25, 2007
2
GB
Hi,

I am trying to transform an xml document:
Code:
<offers>
  <row dates="2008-01-17T00:00:00" Price="22.7900"  Name="1" />
  <row dates="2008-01-19T00:00:00" Price="64.2900"  Name="2" />
  <row dates="2008-01-19T00:00:00" Price="74.2900"  Name="3" />
</offers>
I want to transform it so that if a row node has the same date as another row node then group it together. Here is the result I need:
Code:
<table>
 <tr>
   <td>17-Jan-08</td>
   <td>1</td><td>22.79</td>
 </tr>
  
 <tr>
   <td>19-Jan-08</td>
   <td>2</td><td>74.29</td>
   <td/>
   <td>3</td><td>74.29</td>
 </tr>
</table>

Does anyone know how to do this using XSL?

Thanks,

Din112
 
This is the kind of questions recurrently asked. This is the article contributed in publicizing the Meunchian method.

Let me use the count() method to identifying the first node having certain key characteristic. The method using generate-id() is more used and is illustrated in a faq forum's member k5tm had generously posted.

Here I suppose "offers" be root. Also I suppose the essential characteristic is the date only whereas the time is immaterial in the indexing. This illustration provides you specific twists which may blow some life for people to understand the essence of the method.
[tt]
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="<xsl:eek:utput method="html" indent="yes" encoding="utf-8" />
<xsl:key name="dategroup" match="row" use="substring(@dates,1,10)" />
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="offers" />
</body>
</html>
</xsl:template>
<xsl:template match="offers">
<table>
<xsl:for-each select="row[count(.|key('dategroup',substring(@dates,1,10))[1]) = 1]">
<tr>
<td><xsl:value-of select="substring(@dates,1,10)" /></td>
<xsl:for-each select="key('dategroup',substring(@dates,1,10))">
<td><xsl:value-of select="@Name" /></td>
<td><xsl:value-of select="@Price" /></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
[/tt]
ps: If you have further development needed for some further concrete conditions and/or making the display prettier, you have to do the task yourself and I won't do that for you, otherwise it would counter the shared understanding of the good-use of the forum.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top