我想返回XSLT中的顶部和底部值。这是根据(td[3] - td[4]) div td[4] * 100
进行排序的。我在下面提到了应该如何实施的逻辑。
输入:
<table>
<tr>
<td>10</td>
<td>8</td>
<td>14</td>
<td>9</td>
<td>7</td>
</tr>
<tr>
<td>5</td>
<td>7</td>
<td>2</td>
<td>9</td>
<td>3</td>
</tr>
<tr>
<td>8</td>
<td>2</td>
<td>12</td>
<td>4</td>
<td>1</td>
</tr>
<tr>
<td>6</td>
<td>12</td>
<td>3</td>
<td>7</td>
<td>2</td>
</tr>
</table>
输出应为:
<result>
<top>
<tp>12<t/>+8<t/>+200</tp>
<tp>14<t/>+5<t/>+55.55555</tp>
</top>
<bottom>
<tp>3<t/>-4<t/>-57.142857</tp>
<tp>2<t/>-7<t/>-77.7777</tp>
</bottom>
</result>
尝试的代码:
<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]"/>
<t/>
<xsl:variable name="cdt" select="td[3]-td[4]"/>
<xsl:value-of select="if ($cdt > 0) then concat('+',$cdt) else $cdt"/>
<t/>
<xsl:variable name="xat" select="(td[3] - td[4]) div td[4] * 100"/>
<xsl:value-of select="if ($xat > 0) then concat('+',$xat) else $xat"/>
</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>
我的输出:
<result>
<top>
<tp>14<t/>+5<t/>+55.56</tp>
<tp>2<t/>-7<t/>-77.78</tp>
</top>
<bottom>
<tp>12<t/>+8<t/>+200.00</tp>
<tp>3<t/>-4<t/>-57.14</tp>
</bottom>
</result>
逻辑:
(td[3] - td[4]) div td[4] * 100
td[3]
,例如:12,14 td[3] - td[4]
,例如:+ 8,+ 5 (td[3] - td[4]) div td[4] * 100
,例如:+ 200,+ 55.5555 (td[3] - td[4]) div td[4] * 100
对tp进行排序。并应显示每个已排序tp中的tp td[3]
和td[3] - td[4]
中的其他值。我的意思是最高的tp是+200
。这是第三tr。在第三tr
td[3]
中,值为12
,td[3]-td[4]
为+8
答案 0 :(得分:1)
尝试更改:
<xsl:sort order="descending" data-type="number"/>
收件人:
<xsl:sort select="text()[3]" order="descending" data-type="number"/>