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 中编写成本总和?

4

1 回答 1

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>

应该这样做,在我放置....

于 2013-06-07T15:33:06.957 回答