0

I have an XML file with description and date about events like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="timedate.xsl"?>
<catalog>
    <event>
        <name>AAA Festival</name>
        <place>New York</place>
        <country>USA</country>
        <date>19/11/2013</date>
    </event>
    <event>
        <name>BBB Festival</name>
        <place>Paris</place>
        <country>France</country>
        <date>11/10/2013</date>
    </event>
    <event>
        <name>CCC Festival</name>
        <place>London</place>
        <country>UK</country>
        <date>29/09/2013</date>
    </event>
</catalog>

and an XSL file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>

<xsl:template match="/">
<html>
  <body>
    <h2>Upcoming events</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Name</th>
        <th>Place</th>
        <th>Country</th>
        <th>Date</th> 
      </tr>
      <xsl:for-each select="catalog/event">
      <xsl:sort select="date" order="descending"/>
      <tr>
        <td><xsl:value-of select="name"/></td>
        <td><xsl:value-of select="place"/></td>
        <td><xsl:value-of select="country"/></td>
        <td><xsl:value-of select="date"/></td>
</tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>

I would like to make a page that sort and list only upcoming events (including today date). I'm not able to do this, because dates are not well formatted and I can get current date to compare them and print future events. Please let me know the solution with an example that works. Thanks in advance for your replies and help. Regards!

Andrea Otto
  • 21
  • 1
  • 4

1 Answers1

0

Idea to compare/sort dates concerned with converting your raw date to number format. For example by formula year*372+12*month*31+day (372 and 31 is ok since you really don't need precise numbers)

If your xml have fixed date format (e.g you sure that 1 is always 01) you can use XPath function substring

EDITED-with respect to your claim I'm placing full sample of xsl.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>

<xsl:template match="/">
<html>
  <body>
    <h2>Upcoming events</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Name</th>
        <th>Place</th>
        <th>Country</th>
        <th>Date</th> 
      </tr>
      <xsl:for-each select="catalog/event">
        <xsl:sort 
         select="number(substring(date, 7, 4))*372+12*31*number(substring(date, 4, 2))+number(substring(date, 1, 2))" 
         order="descending" data-type="number"/>
        <tr>
            <td><xsl:value-of select="name"/></td>
            <td><xsl:value-of select="place"/></td>
            <td><xsl:value-of select="country"/></td>
            <td><xsl:value-of select="date"/></td>
            <!-- column just for debug-->
            <td><xsl:value-of select="number(substring(date, 7, 4))*372+12*31*number(substring(date, 4, 2))+number(substring(date, 1, 2))"/></td>
        </tr>
      </xsl:for-each>

    </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>
Dewfy
  • 23,277
  • 13
  • 73
  • 121
  • Maybe I didn't write well the code, so doesn't work for me. Sorry but I have no experience with Xslt. Please let me know where I have to include variables and the code between "some". Is allowed in Xslt 1 compare date starting from today? Thanks! – Andrea Otto May 24 '13 at 17:06
  • Many thanks Dewfy for your help. I would like to know if in this example I can get a current date, compare it with events dates in XML file and publish only results after this date (today). I would be grate if you or someone could post the code to help me and other users they will read this page. Thanks in advance! – Andrea Otto May 25 '13 at 12:22
  • @AndreaOtto Most comprehensive answer to get current date you can find there: http://stackoverflow.com/questions/1575111/can-an-xslt-insert-the-current-date , from my point of view better choice to use parameters or write your own function – Dewfy May 25 '13 at 14:27
  • @AndreaOtto XSLT standard itself doesn't contain specification of javascript extensions. But particular implementation of processor of course may support it (btw, I don't know such one). So passing the function as javascript is question to you vendor (type of xslt processor you use) – Dewfy May 27 '13 at 11:33
  • Can I pass a current date (obtained with a javascript code, see below) to an XSL file? How can I do this? – Andrea Otto May 27 '13 at 11:35
  • @AndreaOtto in the link above look at solution of @KevinHakanson `` - any XSLT parser should support input parameters. – Dewfy May 27 '13 at 14:17