首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在XSLT中获得前3名的结果

如何在XSLT中获得前3名的结果
EN

Stack Overflow用户
提问于 2019-11-22 10:48:08
回答 1查看 188关注 0票数 0

我只想使用xslt获得前2名的结果。在下面的输入中,它应该得到所有td[3]td[4]在每个tr中的差异。最后,它应该只打印前3值和前2值在底部。

逻辑:

第一: 14-9=5

第二: 12-4=8

第三: 2-9=-7

第四: 3-7=-4

输入:

代码语言:javascript
复制
<table>
  <tr>
    <td>10</td>
    <td>8</td>5
    <td>14</td>
    <td>9</td>
    <td>7</td>
  </tr>
  <tr>
    <td>8</td>
    <td>2</td>
    <td>12</td>
    <td>4</td>
    <td>1</td>
  </tr>
  <tr>
    <td>5</td>
    <td>7</td>
    <td>2</td>
    <td>9</td>
    <td>3</td>
  </tr>
  <tr>
    <td>6</td>
    <td>12</td>
    <td>3</td>
    <td>7</td>
    <td>2</td>
  </tr>
</table>

结果应是:

代码语言:javascript
复制
<result>
  <top>
    <tp>8</tp>
    <tp>5</tp>
  </top>
  <bottom>
    <bt>-7</bt>
    <bt>-4</bt>
  </bottom>
</result>

尝试过的代码:

代码语言:javascript
复制
<xsl:template match="xml">
  <xsl:for-each select="table/tr">
     <xsl:variable name="market-price" select="./td[3]"/>
     <xsl:variable name="open-price" select="./td[4]"/>
     <xsl:variable name="price-difference" select="$market-price - $open-price"/>
  </xsl:for-each>
</xsl:template>

我不知道该怎么做才能解决这个问题。我正在使用XSLT2.0

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-22 11:52:21

你可以用一种方式来看待这个:

XSLT2.0

代码语言:javascript
复制
<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/table">
    <xsl:variable name="tp" as="element(tp)+">
        <xsl:perform-sort>
            <xsl:sort order="descending" data-type="number"/>
            <xsl:for-each select="tr">
                <tp>
                    <xsl:value-of select="td[3] - td[4]"/>
                </tp>
            </xsl:for-each>
        </xsl:perform-sort>
    </xsl:variable>
    <result>
        <top>
            <xsl:copy-of select="$tp[position() le 2]"/>
        </top>
        <bottom>
            <xsl:copy-of select="$tp[position() ge last() - 1]"/>
        </bottom>
    </result>
</xsl:template>

</xsl:stylesheet>

Demohttps://xsltfiddle.liberty-development.net/3NSSEvb/2

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58992625

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档