【问题标题】:XSLT sort date and numberXSLT 排序日期和数字
【发布时间】:2012-12-04 02:25:01
【问题描述】:

如何使用 xslt 对具有数字和日期时间的 xml 数据进行排序,我的 xml 如下所示。我需要来自“日期”节点的最大日期和来自节点“总计”的最大总计,我尝试使用低于 sn-p

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>

谢谢, 壁画

【问题讨论】:

  • 问题含糊不清。显示想要从给定输入产生的输出。
  • @JimGarrison 请查看编辑后的帖子。
  • 输出是 XML 和 XSL 的混合...我认为这不是您想要的...?
  • 是的,我正在计算最大日期和值以便在 html 页面上显示它,这就是我像上面这样写的原因。

标签: xml xslt


【解决方案1】:

试试这个

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

【讨论】:

  • 感谢您的解决方案,让我试试这个。有没有办法将这个节点列表数据发送到c#方法中,然后从那里返回计算结果。
猜你喜欢
  • 1970-01-01
  • 2020-11-09
  • 2018-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多