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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSLT grouping dates 1

Status
Not open for further replies.

Sarky78

Programmer
Oct 19, 2000
878
GB
Hi all,

I'm hoping that someone on here will have come across something like this before and be able to help.

I've got an xml document that is returned to me via a webservice so i can;t change it. the document looks like this:

Code:
<root>
  <news>
    <data alias="date">2008-10-20</data>
  </news>
  <news>
    <data alias="date">2009-11-25</data>
  </news>
  <news>
    <data alias="date">2009-11-20</data>
  </news>
  <news>
    <data alias="date">2009-03-20</data>
  </news>
  <news>
    <data alias="date">2008-01-20</data>
  </news>
</root>

the dates in this are in the yyyy-mm-dd format, and the output that i want is to group the same year and month together:

Code:
2009-11
2009-03
2008-10
2008-01

now i found a really good article that does exactly what i want it to do, with their data set and their node names etc, but when i try and apply that to my xml document it just doesn;t work and i don;t understand it enough to figure out why it is going wrong.

This is my code at the moment:

Code:
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
    <xsl:key name="contacts-by-surname" match="news" use="surname" />
    <xsl:template match="root">
        <ul>
            <xsl:for-each select="news[count(. | key('contacts-by-surname', substring(data, 0,8))[1]) = 1]">
                <xsl:sort select="substring(data, 0, 8)" order="descending"/>
                <li>
                    <xsl:value-of select="substring(data, 0, 8)" />
                </li>
            </xsl:for-each>
        </ul>
    </xsl:template>
</xsl:stylesheet>

this code just outputs all of the dates where as what should happen is the duplicate 2009-11 shouldn't be shown.

Any ideas?

Thanks in advance

Tony
 
[tt]<xsl:stylesheet version="1.0" xmlns:xsl="[ignore][/ignore]">
<xsl:key name="contacts-by-surname" match="news" use="[red]substring(data,1,7)[/red]" />
<xsl:template match="root">
<ul>
<xsl:for-each select="news[count(. | key('contacts-by-surname', substring(data,[red]1,7[/red]))[1]) = 1]">
<xsl:sort select="substring(data,[red]1,7[/red])" order="descending"/>
<li>
<xsl:value-of select="substring(data,[red]1,7[/red])" />
</li>
</xsl:for-each>
</ul>
</xsl:template>
</xsl:stylesheet>
[/tt]
 

Dam it, something that simple. could have sworn that i tried something like that.

Thank you so much

Tony
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top