XSLT排序日期和编号

时间:2012-12-03 17:36:57

标签: xml xslt

如何使用xslt和my xml对具有数字和日期时间的xml数据进行排序,如下所示。我需要“日期”节点的最大日期和节点“总计”的最大总数我尝试使用下面的代码段

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>

输出:

    <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>

谢谢, 穆拉利

1 个答案:

答案 0 :(得分:0)

试试这个

<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>

如果xml生成是你的,你可以写xml为

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

然后可以排序

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