XSL / XPATH基于条件的两个节点的总和

时间:2013-06-07 14:16:16

标签: xpath xslt-2.0

这是我的示例XML看起来像

<Header>
<id> 1234 <id>
<type> abcd <type>
<account>
    <code1>a11 </code1>
    <code2>a12 </code2>
    <expense>
        <cost> 10 </cost>
        <description> Travel </description>
    </expense>
        <expense>
        <cost> 30 </cost>
        <description> Travel </description>
    </expense>
    <expense>
        <cost> 20 </cost>
        <description> Hotel </description>
    </expense>
        <expense>
        <cost> 10 </cost>
        <description> Hotel </description>
    </expense>
</account>
</Header>

我需要以XML格式显示结果,如下所示

<Header>
<id> 1234 <id>
<type> abcd <type>
<code1>a11 </code1>
<code2>a12 </code2>
<description> Travel </description>
<cost> 40 </cost> <!-- sum of cost nodes, if description is Travel -->
<description> Hotel </description>
<cost> 30 </cost> <!-- sum of cost nodes, if description is Hotel -->
</Header>

如何根据条件在XSLT 2.0中编写成本总和?

1 个答案:

答案 0 :(得分:0)

<xsl:template match="Header/account">
 ...
  <cost><xsl:value-of select="sum(expense[contains(description, 'Travel')]/cost)"/></cost>
...
  <cost><xsl:value-of select="sum(expense[contains(description, 'Hotel')]/cost)"/></cost>
</xsl:template>

应该这样做,添加我放置...的其他元素。