0

如何使用 xslt 对具有数字和日期时间的 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>

谢谢, 穆拉利

4

1 回答 1

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" />
于 2012-12-03T18:25:01.677 回答