Xsl sum siblings based on node value

I need to apply an xsl transform that sums up certain node values ​​based on the value of one of its nodes. Here is my xml pseudo code:

<?xml version="1.0" encoding="ISO-8859-1" ?> <Message> <Body> <Order> <Item> <.../> <Type>widget</Type> <Qty>20</Qty> <.../> </Item> <Item> <.../> <Type>gadget</Type> <Qty>10</Qty> <.../> </Item> <Item> <.../> <Type>widget</Type> <Qty>5</Qty> <.../> </Item> <Item/> </Order> </Body> </Message> 

The desired result of summing the quantity for all elements of the widget type is 25

 <xsl:value-of select="sum(/Message/Body/Order/Item/Qty)"/> 

gives 35

My xsl style info

 xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions" version="2.0" 
+4
source share
1 answer

Try adding predicates to find the nodes you are interested in (ie, the "widget"):

 <xsl:value-of select="sum(/Message/Body/Order/Item[Type = 'widget']/Qty)"/> 
+3
source

Source: https://habr.com/ru/post/1493339/


All Articles