Hi guys,

I'm struggling with a basic stuff - A call to a javascript function within a XSLT transformation. Could you please help to find out what I'm doing wrong.
Here is the XML input text

<?xml version="1.0" encoding="UTF-8"?>
<message channel-id="987b3452-5e34-4401-8106-ef81939f93e2">
<task-started task-type="task">
<agent-parameter multi-valued="false"><name>EPNIPRangeEndINT</name><value>168428543</value></agent-parameter><agent-parameter multi-valued="false"><name>EPNIPRangeStartINT</name><value>168428288</value></agent-parameter><agent-parameter multi-valued="false"><name>EPNRecordIPName</name><value>epn-2</value></agent-parameter><agent-parameter multi-valued="false"><name>IPPoolName</name><value>AzdinePool-1</value></agent-parameter><agent-parameter multi-valued="false"><name>NetworkType</name><value>AzdinePool-1</value></agent-parameter></task-started>
</message>

The XSLT is here

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:lxslt="http://xml.apache.org/xslt"
xmlns:result="http://www.example.com/results" extension-element-prefixes="result">
<xsl:output method="xml" indent="yes"/>

<lxslt:component prefix="result" functions="num2dot">
    <lxslt:script lang="javascript">
    function num2dot(num) {
        var d = num%256;
        for (var i = 3; i > 0; i--) {
        num = Math.floor(num/256);
        d = num%256 + '.' + d;}
    return d;
    }    
</lxslt:script>
</lxslt:component>

    <xsl:template name="main" match="/message/task-started">
    <CIM CIMVERSION="" DTDVERSION="">
        <DECLARATION>
          <DECLGROUP>
          <xsl:call-template name="incIP">
            <xsl:with-param name="RecordName" select="/message/task-started/agent-parameter[name='EPNRecordIPName']/value"/>
            <xsl:with-param name="currentID" select="/message/task-started/agent-parameter[name='EPNIPRangeStartINT']/value"/>
            <xsl:with-param name="endID" select="/message/task-started/agent-parameter[name='EPNIPRangeEndINT']/value"/>
            <xsl:with-param name="NetType" select="/message/task-started/agent-parameter[name='NetworkType']/value"/>
            <xsl:with-param name="IPPoolName" select="/message/task-started/agent-parameter[name='IPPoolName']/value"/>
           </xsl:call-template>
          </DECLGROUP>
        </DECLARATION>
      </CIM>
    </xsl:template>

    <xsl:template name="incIP">
        <xsl:param name="RecordName" />
        <xsl:param name="currentID" />
        <xsl:param name="endID" />
        <xsl:param name="NetType" />
        <xsl:param name="IPPoolName" />

         <xsl:if test="$currentID <= $endID">

            <VALUE.OBJECT>
                <INSTANCE CLASSNAME="IPAddress">
                    <PROPERTY NAME="Name" TYPE="string">
                        <VALUE><xsl:value-of select="$RecordName" /><xsl:text>.</xsl:text><xsl:value-of select="$currentID" /></VALUE>                  
                    </PROPERTY>
                    <PROPERTY NAME="IPAddressINT" TYPE="char16">
                        <VALUE><xsl:value-of select="$currentID" /></VALUE>
                    </PROPERTY>
                    <PROPERTY NAME="IPAddress" TYPE="char16">
                        <VALUE><xsl:value-of select="result:num2dot(<xsl:value-of select="$currentID">)" /></VALUE>
                    </PROPERTY>
                    <PROPERTY NAME="isReserved" TYPE="char16">
                        <VALUE>N</VALUE>
                    </PROPERTY>
                    <PROPERTY NAME="NetworkType" TYPE="string">
                        <VALUE><xsl:value-of select="$NetType" /></VALUE>
                    </PROPERTY>                       
                    <PROPERTY NAME="Ippool" TYPE="string">
                        <VALUE><xsl:value-of select="$IPPoolName" /></VALUE>
                    </PROPERTY>                       

                </INSTANCE>
            </VALUE.OBJECT>          
            <xsl:call-template name="incIP">
                <xsl:with-param name="RecordName" select="$RecordName" />
                <xsl:with-param name="currentID" select="$currentID + 1" />
                <xsl:with-param name="endID" select="$endID" />
                <xsl:with-param name="NetType" select="$NetType" />
                <xsl:with-param name="IPPoolName" select="$IPPoolName" />
            </xsl:call-template>     
        </xsl:if>  
    </xsl:template>  
</xsl:stylesheet>

You have two syntax errors.

----------------------
Change

<VALUE><xsl:value-of select="$RecordName" /><xsl:text>.</xsl:text><xsl:value-of select="$currentID" />

to:

<VALUE><xsl:value-of select="result:num2dot($currentID)" /></VALUE>

This is because we do not need the <xsl:value-of() /> to retrieve the variable value, simply referencing it is enough.

----------------------

Change

<xsl:if test="$currentID <= $endID">

to:

<xsl:if test="$currentID &lt;= $endID">

This is because < is a reserved character in XSLT.

----------------------

I couldn't fully test it running due to namespace errors on the line xmlns:result="http://www.example.com/results" extension-element-prefixes="result"> however I presume this is a replacement to a real namespace.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.