.Example

# Sablotron Extensions API Reference

Example | .General
#### Summary An example how to use the DOM in JavaScript extension functions. The function returns a sum of all attributes in a nodeset.
#### Example
    Template:
    --------------------
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0"
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
       xmlns:func="http://www.exslt.org/functions"
       xmlns:my="http://gingerall.org/sablot/myfunc"
       extension-element-prefixes="func"
       exclude-result-prefixes="my">

       <xsl:output method="xml" indent="yes"/>

       <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:
    --------------------
    <?xml version="1.0"?>
    <root>

       <node a="1" b="2"/>
       <node c="10"/>
       <node a="5" b="6" c="7"/>

    </root>

    Result:
    --------------------
    <?xml version="1.0" encoding="UTF-8"?>
    <root>The sum of attributes: 31</root>
#### See Also
Groups | [ .General ]

© 2001-2002 Ginger Alliance
revision 02-08-05
This page was generated by APIDOC