SUM() 一个 XML 的不同节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12200325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:39:56  来源:igfitidea点击:

SUM() different nodes of an XML

xmlxsltxpathsumbi-publisher

提问by AnBisw

I have an XML like below in BI Publisher-

我在 BI Publisher 中有一个如下所示的 XML-

<ROW1>
  <TOTAL_RETAIL>10.95</TOTAL_RETAIL>
  <TOTAL_TAX> 1.8</TOTAL_TAX>
  <TOTAL_SHIPPING>7.95</TOTAL_SHIPPING>
</ROW1>

<ROW1>does not repeat. Now as I understand to do a SUMof a node I can use XPath function sum()like <?sum(.//TOTAL_RETAIL)?>. This will sum the values of the node TOTAL_RETAILonly, I want the sum of TOTAL_RETAIL, TOTAL_TAX, and TOTAL_SHIPPING. Is there a way I can write the sumfunction to achieve this.

<ROW1>不重复。现在,据我所知,要执行一个SUM节点,我可以使用 XPath 函数,sum()例如<?sum(.//TOTAL_RETAIL)?>. 这将总结该节点的值TOTAL_RETAIL而已,我想的总和TOTAL_RETAILTOTAL_TAXTOTAL_SHIPPING。有没有办法可以编写sum函数来实现这一点。

Note- It cannot be handled programmatically i.e. using variables etc. since its inside a Report template and has to be defined as <?sum(...)?>this values will be mapped to a specific cell in an excel report template.

注意 - 它不能以编程方式处理,即使用变量等,因为它位于报告模板内,并且必须定义<?sum(...)?>为该值将映射到 Excel 报告模板中的特定单元格。

回答by LarsH

Try

尝试

<?sum(.//TOTAL_RETAIL | .//TOTAL_TAX | .//TOTAL_SHIPPING)?>

|is the union operator, so the argument you're passing to sum()is the union of the three sub-expressions.

|是联合运算符,因此您传递给的参数sum()是三个子表达式的联合。

回答by JohnB

sum(.//TOTAL_RETAIL | .//TOTAL_TAX | .//TOTAL_SHIPPING)

回答by simsim

You can also try splitting the sum on each node, it works like this:

您还可以尝试在每个节点上拆分总和,其工作方式如下:

<?sum(.//TOTAL_RETAIL) + sum(.//TOTAL_TAX) + sum(.//TOTAL_SHIPPING)?>

And in case you have NULL values or empty nodes, use this filter to make sure that the sum function will always work: sum(.//TOTAL_RETAIL[.!='']) + ...etc

如果您有 NULL 值或空节点,请使用此过滤器来确保 sum 函数始终有效:sum(.//TOTAL_RETAIL[.!='']) + ...etc