返回XSLT中的顶部和底部值

时间:2019-11-25 18:43:17

标签: xml xslt

我想返回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
  • 第三个数字mut是(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]中,值为12td[3]-td[4]+8

1 个答案:

答案 0 :(得分:1)

尝试更改:

<xsl:sort order="descending" data-type="number"/>

收件人:

<xsl:sort select="text()[3]" order="descending" data-type="number"/>