XSLT sort date and number

2019-09-13 12:01发布

问题:

How to sort xml data having numbers and date times, by using xslt and my xml looks like below. And i need max date from 'Date' node and max Total from node 'Total' i tried with below snippet

XSLT :

<xsl:for-each select="/Report/Total/DailyPeakDmds/DailyPeakDmd">
    <xsl:sort select="number(Demand1/Total)" data-type="number" order="descending"/>
    <xsl:if test="position() = 1">
        <xsl:value-of select="Demand1/Total"/>
    </xsl:if>
</xsl:for-each>

XML:

   <DailyPeakDmds>
        <DailyPeakDmd>
          <Date>03-12-11 (NB)</Date>
          <Demand1>
            <Total>0.037</Total>
            <Time>01:19</Time>
            <RateA>0.000</RateA>
            <RateATime>00:00</RateATime>
            <RateB>0.000</RateB>
            <RateBTime>00:00</RateBTime>
          </Demand1>
          <Demand2>
            <Total>0.002</Total>
            <Time>01:00</Time>
            <RateA>0.000</RateA>
            <RateATime>00:00</RateATime>
            <RateB>0.000</RateB>
            <RateBTime>00:00</RateBTime>
          </Demand2>
        </DailyPeakDmd>
        <DailyPeakDmd>
          <Date>03-13-11 (NB)</Date>
          <Demand1>
            <Total>0.000</Total>
            <Time>00:00</Time>
            <RateA>0.000</RateA>
            <RateATime>00:00</RateATime>
            <RateB>0.036</RateB>
            <RateBTime>03:03</RateBTime>
          </Demand1>
          <Demand2>
            <Total>0.000</Total>
            <Time>00:00</Time>
            <RateA>0.000</RateA>
            <RateATime>00:00</RateATime>
            <RateB>0.002</RateB>
            <RateBTime>03:02</RateBTime>
          </Demand2>
        </DailyPeakDmd>
        <DailyPeakDmd>
          <Date>03-14-11 (NB)</Date>
          <Demand1>
            <Total>0.000</Total>
            <Time>00:00</Time>
            <RateA>0.000</RateA>
            <RateATime>00:00</RateATime>
            <RateB>0.000</RateB>
            <RateBTime>00:00</RateBTime>
          </Demand1>
          <Demand2>
            <Total>0.000</Total>
            <Time>00:00</Time>
            <RateA>0.000</RateA>
            <RateATime>00:00</RateATime>
            <RateB>0.000</RateB>
            <RateBTime>00:00</RateBTime>
          </Demand2>
        </DailyPeakDmd>
        <DailyPeakDmd>
          <Date>03-15-11 (NB)</Date>
          <Demand1>
            <Total>0.000</Total>
            <Time>00:00</Time>
            <RateA>0.000</RateA>
            <RateATime>00:00</RateATime>
            <RateB>0.000</RateB>
            <RateBTime>00:00</RateBTime>
          </Demand1>
          <Demand2>
            <Total>0.000</Total>
            <Time>00:00</Time>
            <RateA>0.000</RateA>
            <RateATime>00:00</RateATime>
            <RateB>0.000</RateB>
            <RateBTime>00:00</RateBTime>
          </Demand2>
        </DailyPeakDmd>
        <DailyPeakDmd>
          <Date>03-16-11 (NB)</Date>
          <Demand1>
            <Total>0.000</Total>
            <Time>00:00</Time>
            <RateA>0.000</RateA>
            <RateATime>00:00</RateATime>
            <RateB>0.000</RateB>
            <RateBTime>00:00</RateBTime>
          </Demand1>
          <Demand2>
            <Total>0.000</Total>
            <Time>00:00</Time>
            <RateA>0.000</RateA>
            <RateATime>00:00</RateATime>
            <RateB>0.000</RateB>
            <RateBTime>00:00</RateBTime>
          </Demand2>
        </DailyPeakDmd>
        <DailyPeakDmd>
          <Date>03-17-11 (NB)</Date>
          <Demand1>
            <Total>0.000</Total>
            <Time>00:00</Time>
            <RateA>0.000</RateA>
            <RateATime>00:00</RateATime>
            <RateB>0.000</RateB>
            <RateBTime>00:00</RateBTime>
          </Demand1>
          <Demand2>
            <Total>0.000</Total>
            <Time>00:00</Time>
            <RateA>0.000</RateA>
            <RateATime>00:00</RateATime>
            <RateB>0.000</RateB>
            <RateBTime>00:00</RateBTime>
          </Demand2>
        </DailyPeakDmd>
        <DailyPeakDmd>
          <Date>03-18-11 (NB)</Date>
          <Demand1>
            <Total>0.000</Total>
            <Time>00:00</Time>
            <RateA>0.000</RateA>
            <RateATime>00:00</RateATime>
            <RateB>0.000</RateB>
            <RateBTime>00:00</RateBTime>
          </Demand1>
          <Demand2>
            <Total>0.000</Total>
            <Time>00:00</Time>
            <RateA>0.000</RateA>
            <RateATime>00:00</RateATime>
            <RateB>0.000</RateB>
            <RateBTime>00:00</RateBTime>
          </Demand2>
        </DailyPeakDmd>
        <DailyPeakDmd>
          <Date>03-19-11 (NB)</Date>
          <Demand1>
            <Total>0.000</Total>
            <Time>00:00</Time>
            <RateA>0.000</RateA>
            <RateATime>00:00</RateATime>
            <RateB>0.000</RateB>
            <RateBTime>00:00</RateBTime>
          </Demand1>
          <Demand2>
            <Total>0.000</Total>
            <Time>00:00</Time>
            <RateA>0.000</RateA>
            <RateATime>00:00</RateATime>
            <RateB>0.000</RateB>
            <RateBTime>00:00</RateBTime>
          </Demand2>
        </DailyPeakDmd>
      </DailyPeakDmds>

Output:

    <table>
<tr>
          <td class="subsection" >
            Max date          </td>
          <td  class="datavalue">
            <xsl:for-each select="/Report/Total/DailyPeakDmds/DailyPeakDmd">
              <xsl:sort select="Date" data-type="number" order="descending"/>
              <xsl:if test="position() = 1">
                <xsl:value-of select="Date"/>
              </xsl:if>
            </xsl:for-each>
          </td>
        </tr>
<tr>
          <td class="subsection" >
            Max value          </td>
          <td  class="datavalue">
            <xsl:for-each select="/Report/Total/DailyPeakDmds/DailyPeakDmd">
              <xsl:sort select="number(Demand1/Total)" data-type="number" order="descending"/>
              <xsl:if test="position() = 1">
                <xsl:value-of select="Demand1/Total"/>
              </xsl:if>
            </xsl:for-each>
          </td>
        </tr>
</table>

Thanks, Murali

回答1:

Try this

<xsl:for-each select="/Report/Total/DailyPeakDmds/DailyPeakDmd">
  <xsl:sort select="normalize-space(substring(Date,7,2))" order="descending" />
  <xsl:sort select="normalize-space(substring(Date,4,2))" order="descending" />
  <xsl:sort select="normalize-space(substring(Date,0,2))" order="descending" />
  <xsl:if test="position() = 1">
   <xsl:value-of select="Date"/>
   </xsl:if>
</xsl:for-each>

If xml generation is yours you could write xml as

  <Date val="20111203">03-12-11 (NB)</Date>

then can sort

 <xsl:sort select="xs:date(Date/@value)" order="descending" />


标签: xml xslt