0

i want to sorting by descending order based on date.i dont know how to accomplish this :

Here is my xml :

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<inm:Results productTitle="Inmagic DB/Text WebPublisher PRO" productVersion="13.00" xmlns:inm="http://www.inmagic.com/webpublisher/query" oex="ISO-8859-1">
<inm:Recordset AC="QBE_QUERY" sn="AUTO26264" se="1392" queryCount="139" page="1" pageCount="1" setCount="139">
<inm:Record setEntry="0">
  <inm:Title>BBBBBB</inm:Title>
  <inm:Pub_Date>12-Jun-2012</inm:Pub_Date>
  <inm:Words />
</inm:Record>
<inm:Record setEntry="1">
   <inm:Title>TESTING ESTING</inm:Title>
   <inm:Pub_Date>12-jul-2012</inm:Pub_Date>
  <inm:Words />
</inm:Record>
<inm:Record setEntry="2">    
  <inm:Title>TESFDS SDFASDFASDt</inm:Title>
  <inm:Pub_Date>30-Jun-2012</inm:Pub_Date>
  <inm:Words />
</inm:Record>   
</inm:Recordset>
</inm:Results>

and my xml is :

  <?xml version="1.0" encoding="utf-8"?>
 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:inm="http://www.inmagic.com/webpublisher/query">
 <xsl:output method="html"/>
 <xsl:template match="/">
 <ul class="baseList newsList">
  <xsl:for-each  select="inm:Results/inm:Recordset/inm:Record" >
    <xsl:sort select="inm:Pub_Date" order="descending"/>
      <li>
        <span class="title">
          <a href="#">
            <xsl:value-of select="inm:Title" />
          </a>
        </span>
        <p class="meta">
          <span class="dateTime">
            <xsl:value-of select="inm:Pub_Date"/>
          </span>
        </p>
      </li>
  </xsl:for-each>
</ul>
  </xsl:template>

i try to sorting in xslt script but its not worked perfectly.

output was : - 30-Jun-2012 - 12-jul-2012 - 12-jun-2012

but output should be : -12-jul-2012 -30-jun-2012 -12-Jun-2012

3 Answers 3

3

I agree with the other answer posters that you should convert your dates to ISO date format.

Having said that, if you insist on using the format as supplied in the sample document, you can use the following xsl:sort instructions ...

<xsl:sort select="substring(inm:Pub_Date,8)" data-type="number" order="descending"/>
<xsl:sort select="string-length( substring-before(
  'JanFebMarAprMayJunJulAugSepOctNovDec',
  substring(inm:Pub_Date,4,3)))" data-type="number" order="descending"/>
<xsl:sort select="substring-before(inm:Pub_Date,'-')" data-type="number" order="descending"/>

The first sorts on year, the second on month, and then finally day.

The style-sheet as a whole is ...

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:inm="http://www.inmagic.com/webpublisher/query">
 <xsl:output method="html"/>
 <xsl:template match="/">
 <ul class="baseList newsList">
  <xsl:for-each  select="inm:Results/inm:Recordset/inm:Record" >
    <xsl:sort select="substring(inm:Pub_Date,8)" data-type="number" order="descending"/>
    <xsl:sort select="string-length( substring-before(
      'JanFebMarAprMayJunJulAugSepOctNovDec',
      substring(inm:Pub_Date,4,3)))" data-type="number" order="descending"/>
    <xsl:sort select="substring-before(inm:Pub_Date,'-')" data-type="number" order="descending"/>
      <li>
        <span class="title">
          <a href="#">
            <xsl:value-of select="inm:Title" />
          </a>
        </span>
        <p class="meta">
          <span class="dateTime">
            <xsl:value-of select="inm:Pub_Date"/>
          </span>
        </p>
      </li>
  </xsl:for-each>
</ul>
</xsl:template>
</xsl:stylesheet>

Update

Just as a helpful hint, here is a neat template you can use to convert dates from the 30-Jun-2012 format to ISO/XML format. Let me know if you can use XSLT 2.0 . The conversion gets simpler in XSLT 2.0 .

<xsl:template name="format-date">
 <xsl:param name="InDateValue" select="'01-Jan-2000'" />
 <xsl:value-of select="concat( substring($InDateValue, 8, 4), '-')"/>
 <xsl:variable name="month" select="substring($InDateValue, 4, 3)" />
 <xsl:value-of select="format-number(
          translate( $month, 'nFrylgSONDJaebMApupctov', '00240107666') +
          translate( $month, 'aFpugONDJnebMrAyluSctovc', '12268456'), '00')" />
 <xsl:value-of select="concat( '-', substring($InDateValue, 1, 2))"/>
</xsl:template>

Ok, its quiet obtuse! But I like it.

Sign up to request clarification or add additional context in comments.

1 Comment

Its not working for you because of an error in your input document. You spelt July as lowercase 'jul' instead of 'Jul'. If you make this correction, it will work. I tested the stylesheet on www.xmlper.com . It works as long as there are no errors in the input document.
2

It's always best to use ISO dates in XML: 2012-06-30. One advantage of these is that they sort naturally, another is that it's easy to localize the output for different natural languages etc (esp. in XSLT 2.0 with format-number()).

Converting your dates to ISO format is easy enough even if you are stuck with XSLT 1.0 - the code is tedious but not difficult to write.

Comments

1

You're sorting on the string values of the Pub_Date elements, so the correct order is being returned.

It you want to sort on the actual date type you'll need either XSLT 2.0, or to use the ISO date format (so 30-Jun-2012 is 20120630). This will then put the date strings into a type that can be sorted.

3 Comments

hi..want to perfect sorting.i tried to implement c# in xslt.but when ever i check throwing scripts are usually safe.do you want to allow script to run?
i means i dont know proper way to use c# code in xlst for xml.
The answer doesn't say you need to use C#, so why are you doing it? If you have a problem with C# and XML, that's a different question. In the meantime, since you asked the question, please be gracious enough to try out the answer given, or if you don't understand it, then ask how to apply it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.