我需要使用相同的带值从多个费率中获取特定金额
输入我有的xml:
<Rates>
<Rate>
<Code>2000</Code>
<Type>Life</Type>
<GRates>
<GRate>
<band><25</band>
<amount>0.04</amount>
</GRate>
<GRate>
<band>25-29</band>
<amount>0.05</amount>
</GRate>
<GRate>
<band>30-34</band>
<amount>0.06</amount>
</GRate>
</Rates>
</Rate>
<Rate>
<Code>2000</Code>
<Type>Add</Type>
<GRates>
<GRate>
<band><25</band>
<amount>0.08</amount>
</GRate>
<GRate>
<band>25-29</band>
<amount>0.10</amount>
</GRate>
<GRate>
<band>30-34</band>
<amount>0.12</amount>
</GRate>
</GRates>
</Rate>
<Rate>
<Code>2000</Code>
<Type>Sub</Type>
<GRates>
<GRate>
<band>40-45</band>
<amount>0.02</amount>
</GRate>
<GRate>
<band>46-50</band>
<amount>0.03</amount>
</GRate>
<GRate>
<band>51-55</band>
<amount>0.01</amount>
</GRate>
</GRates>
</Rate>
</Rates>
XSL 我已经尝试过以下方法:
<xsl:template match="Rates">
<xsl:param name="Group" as="element()" tunnel="yes"/>
<xsl:for-each select="($Group//Rates/Rate[descendant::GRate][Type='Life'])[1]//GRate">
<row>
<entry align="center" colsep="1">
<xsl:value-of select="band"/>
</entry>
<xsl:for-each select="$Group//Rates/Rate[Type='Life']">
<entry>
<p>
$<xsl:value-of select="amount"/>
</p>
</entry>
<entry>
<p>
$<xsl:value-of select="$Group//Rates/Rate[Type='Add']/amount"/>
</p>
</entry>
</xsl:for-each>
</row>
</xsl:for-each>
</xsl:template>
我得到两倍的输出并从“生命”率中获取
<row>
<entry align="center" colsep="1"><25</entry>
<entry>
<p>$0.04</p>
</entry>
<entry>
<p>$0.04</p>
</entry>
</row>
<row>
<entry align="center" colsep="1">25-29</entry>
<entry>
<p>$0.05</p>
</entry>
<entry>
<p>$0.05</p>
</entry>
</row>
<row>
<entry align="center" colsep="1">30-34</entry>
<entry>
<p>$0.06</p>
</entry>
<entry>
<p>$0.06</p>
</entry>
</row>
预期输出:
<row>
<entry align="center" colsep="1"><25</entry>
<entry>
<p>$0.04</p>
</entry>
<entry>
<p>$0.08</p>
</entry>
</row>
<row>
<entry align="center" colsep="1">25-29</entry>
<entry>
<p>$0.05</p>
</entry>
<entry>
<p>$0.10</p>
</entry>
</row>
<row>
<entry align="center" colsep="1">30-34</entry>
<entry>
<p>$0.06</p>
</entry>
<entry>
<p>$0.12</p>
</entry>
</row>
“Life”和“Add”费率的带值是相同的,但金额不同。所以我使用了 xsl:for-each。由于“添加”费率不会被挑选,而“生命”费率会来两次。
这个问题很令人困惑,因为您的代码似乎只包含 Life 类型的费率,但预期输出也显示了 Add 类型的费率。
获得此类输出的正确方法是:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/Rates">
<xsl:for-each-group select="Rate[Type=('Life', 'Add')]/GRates/GRate" group-by="band">
<row>
<entry align="center" colsep="1">
<xsl:value-of select="band"/>
</entry>
<xsl:for-each select="current-group()">
<entry>
<p>
<xsl:value-of select="format-number(amount, '$0.00')"/>
</p>
</entry>
</xsl:for-each>
</row>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>