简体   繁体   中英

Sorting in xslt with datetime from xml?

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

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM