Google

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:
--------------------
<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>

See Also

Groups [ .General ]

© 2001 Ginger Alliance
revision 01-12-14
This page was generated by APIDOC