我只想使用xslt获得前2个结果。在下面的输入中,它将获得每个td[3]
中所有td[4]
和tr
的差。最后,它应该只打印顶部的3个值和底部的2个值。
逻辑:
第一:14-9 = 5
秒:12-4 = 8
第三:2-9 = -7
第四:3-7 = -4
输入:
<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>
结果应为:
<result>
<top>
<tp>8</tp>
<tp>5</tp>
</top>
<bottom>
<bt>-7</bt>
<bt>-4</bt>
</bottom>
</result>
尝试的代码:
<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>
我不知道如何进一步解决这个问题。我正在使用XSLT 2.0
一种看待这个问题的方式:
XSLT 2.0
<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:strip-space elements="*"/>
<xsl:template match="/table">
<xsl:variable name="tp" as="element()*">
<xsl:for-each select="tr">
<tp>
<xsl:value-of select="td[3] - td[4]"/>
</tp>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="tp" as="element()*">
<xsl:perform-sort select="$tp">
<xsl:sort select="." order="descending"/>
</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>