Here is a short and simple XSLT solution (actually it's just XPath):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="my:my"> <xsl:output method="text"/> <my:MarkValues> <marks> <mark type="HD">10</mark> <mark type="D">8</mark> <mark type="C">5</mark> </marks> </my:MarkValues> <xsl:template match="/*"> <xsl:value-of select= "sum(document('')/* /my:MarkValues/*/* [@type = current()/* [@id='1234']/grade ] )"/> </xsl:template> </xsl:stylesheet>
when this conversion is applied to the provided XML document (named "studentRecord.xml"):
<students> <student id="1234"> <grade>HD</grade> </student> <student id="1234"> <grade>C</grade> </student> <student id="1111"> <grade>D</grade> </student> </students>
Found the desired answer :
15
If you want to save the label values ββin a separate file (not built into the XSLT stylesheet, as indicated above), the XPath expression should be slightly modified (only the argument to the document() function:
sum(document('mark.xml')/*/* [@type = current()/* [@id='1234']/grade ] )
Explanation
source share