xml 如何在 XPath 中使用 sum 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7979705/
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
How to use sum function in XPath?
提问by Azeem
I have an xml file with following contents:
我有一个包含以下内容的 xml 文件:
<Xavor>
<Dev>
<Emp>1</Emp>
<Floor>1</Floor>
<Salary>1200.4</Salary>
</Dev>
<Dev>
<Emp>2</Emp>
<Salary>3100.8</Salary>
</Dev>
<Dev>
<Emp>3</Emp>
<Floor>1</Floor>
</Dev>
I want to calculate sum of salaries of first two Employees using sum function. I came to this XPath:
我想使用 sum 函数计算前两个员工的工资总和。我来到这个XPath:
sum(/Xavor/Dev[2]/Salary/text())
But this returns only second salary value ie 3100.8!!!
This XPath was working fine when there were only non-floating point numbers were in salaries. Please help me out.
但这仅返回第二个薪水值,即3100.8!!!当薪水中只有非浮点数时,此 XPath 工作正常。请帮帮我。
回答by Rubens Farias
Try this:
尝试这个:
sum(/Xavor/Dev[position() <= 2]/Salary/text())
回答by Dimitre Novatchev
In addition to the correct answer by @Rubens Farias, if you want to sum the salaries of all Devthat have (numeric) salaries specified, use:
除了@Rubens Farias 的正确答案之外,如果您想对所有Dev指定了(数字)工资的人的工资求和,请使用:
sum(/*/Dev/Salary[number(.) = number(.)])

