Consider this simple xml
<books>
<book>
<price ht="100" ttc="120"/>
</book>
<book>
<price ht="150" ttc="180"/>
</book>
</books>
I want to list the substraction of ttc
and ht
for all book/price
node (ttc - ht)
I know how to obtain the ttc: //book[price]/price/@ttc
and the ht same way.
How to obtain the result of ttc - ht ?
CodePudding user response:
Try
//book/price/(./@ttc - ./@ht)
Given your sample xml, output is:
20
30
CodePudding user response:
To retrieve the difference per book
element, you can use this XSL code in combination with the Identity template:
<xsl:template match="book">
<xsl:copy>
<xsl:value-of select="price/@ttc - price/@ht" />
</xsl:copy>
</xsl:template>
It emits the difference for each <book>
element.
To get the total difference of all <book>
element's attributes, you can use the following XPath-1.0 expression:
<xsl:template match="books">
<xsl:copy>
<xsl:value-of select="sum(.//book/price/@ttc) - sum(.//book/price/@ht)" />
</xsl:copy>
</xsl:template>
The core XPath-1.0 expression is, starting from the level of <books>
, this one:
sum(.//book/price/@ttc) - sum(.//book/price/@ht)
This expression subtracts all @ht
attributes from all @ttc
attributes. The .//
operator means, that all descendant elements starting from the current axis are selected. In the above XSLT code, the current axis is <books>
.
So in the above example case, the result is 50
.