XSLT2, node - - . XSLT1 node -set.
:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:variable name="extendedItems" as="xs:integer*">
<xsl:for-each select="//Item">
<xsl:value-of select="./Price * ./Quantity"/>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="total">
<xsl:value-of select="sum($extendedItems)"/>
</xsl:variable>
<xsl:template match="//QuantityTotal">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:value-of select="$total"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
" " NumberTotal. , QuantityTotal . , . NumberTotal node.
The key to understanding XSL is that it is declarative in nature. The most common conceptual error made by almost all beginners is that the stylesheet is a sequential program that processes the input XML document. This is actually the opposite. The XSL engine reads an XML document. and for each new tag he encounters, he scans the stylesheet for a “better” match, executing this template.
EDIT:
Here is the xslt1.1 version that works with Saxon 6.5
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:ex="http://exslt.org/common"
extension-element-prefixes="ex"
version="1.1">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:variable name="extendedItems">
<xsl:for-each select="//Item">
<extended>
<xsl:value-of select="./Price * ./Quantity"/>
</extended>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="total">
<xsl:value-of select="sum(ex:node-set($extendedItems/extended))"/>
</xsl:variable>
<xsl:template match="//QuantityTotal">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:value-of select="$total"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
source
share