Example
Template:
--------------------
<xsl:stylesheet version="1.0"
extension-element-prefixes="func"
exclude-result-prefixes="my">
<func:script implements-prefix="my" language="javascript"><![CDATA[
function attrSum(nodeset) {
var sum = 0;
for (i = 0; i < nodeset.length; i++) {
for (j = 0; j < nodeset[i].attributes.length; j++) {
sum += Number(nodeset[i].attributes.item(j));
}
}
return sum;
}
]]>
<xsl:fallback>
<xsl:text>JS extension no supported!</xsl:text>
</xsl:fallback>
</func:script>
<xsl:template match="/root">
<root>
<xsl:choose>
<xsl:when test="function-available('my:attrSum')">
<xsl:text>The sum of attributes: </xsl:text>
<xsl:value-of select="my:attrSum(node)"/>
</xsl:when>
<xsl:otherwise>Function not available!</xsl:otherwise>
</xsl:choose>
</root>
</xsl:template>
</xsl:stylesheet>
Data:
--------------------
<root>
<node a="1" b="2"/>
<node c="10"/>
<node a="5" b="6" c="7"/>
</root>
Result:
--------------------
<root>The sum of attributes: 31</root>
|