我有以下XML:
我想对“数量”中的所有值求和,尽管其中有些可能会完全丢失值。我怎样才能做到这一点。我尝试的一切都会导致NaN
/ M
<Top>
<Lines>
<Line ID="1" Quantity="1,00" />
<Line ID="2" Quantity="11,00" />
<Line ID="3" Quantity="" />
<Line ID="4" Quantity="" />
<Line ID="5" Quantity="10,00" />
</Lines>
</Top>
答案 0 :(得分:0)
如评论中所述,您在这里有两个问题:
在XSLT 2.0中,您可以使用以下表达式解决这两个问题:
sum(/Top/Lines/Line[string(@Quantity)]/number(translate(@Quantity, ',', '.')))
[string(@Quantity)]
谓词排除Line
属性为空的Quantity
,而number(translate(@Quantity, ',', '.'))
表达式将其他转换为有效数字。