I am trying to sort my xml by date, but my xml and xsl r are not working, like this

Problem sorting xml by date <xsl: sort select = "/">

I am trying to sort my xml by date, but my xml and xsl r are not working, like this

<xsl:template match="/">
    <xsl:for-each select="news/item">
                        <xsl:sort select="date1" order="descending" />                                     

                          <xsl:value-of select="date1"/>                                

                  </xsl:for-each> 
</xsl:template>

MYXML

<news>
 <item>
<date1>January 1, 2010</date1>
 </item>
 <item>
  <date1>November 29, 2009</date1>
</news>


         Its displaying the result but not in sorted way..
+3
source share
6 answers

xsl-sortDoesn't "know" how to sort dates. It will use the default to sort the text, although you can specify a numerical sort using the attribute data-type.

There are some tips to solve this problem - add an attribute or change the way the date is output in the source XML, so you need a view that you can sort numerically.

+4
source

You can try using something like this:

<xsl:template match="/"> 
  <xsl:for-each select="news/item"> 
    <xsl:sort select="xs:date(date1)" order="descending" />
    <xsl:value-of select="date1"/>                                 
  </xsl:for-each>  
</xsl:template> 

, XML, - :

<date1 isoValue="20100101">January 1, 2010</date1>

<xsl:sort select="xs:date(date1/@isoValue)" order="descending" />
+4

. XSLT XML XML.

+3

xml - . : yyyy-mm-dd, .

, :

<xsl:sort select="newsDate"  order="ascending" />

.

+1

xml:

:

<date1> 2010-10-17+02:00 </date1>

:

<date1 number="{translate(substring-before(date1,'+'),'-','')}"
   <xsl:value-of select="date1"/>
</date1>`

:

<date1 number="20101017">2010-10-17+02:00</date1>

, ,

<xsl:sort order="descending" select="@number" data-type="number"/>

Greetz

0

date1, , year/month/date, . xsl: for-each :

<xsl:sort select="ddwrt:FormatDateTime(string(@date1),1033,'yy/MM/dd')" order="descending" />
0

Source: https://habr.com/ru/post/1729087/


All Articles