2

I have a javascript inside the XSL style sheet. In the javascript, i have a function, getName(node). How do i call this in my xsl? I had used xsl:eval=getname(this) earlier. It worked till IE8. Hasnt worked from IE9 onwards. Can i use xsl:value-of-select? or any other tips to make it work ? Thank you in advance.

    <xsl:script language='JavaScript'><![CDATA[

    var sSobeysRegion = "Sobeys West";
    var sReceiverID = "SOBEYSCANADA:ZZ";
    var ATLANTIC = "Sobeys Atlantic";
    var WEST = "Sobeys West";
    var QUEBEC = "Sobeys Quebec";
    var ONTARIO = "Sobeys Ontario";
    var option1 = ATLANTIC ;
    var option2 = WEST ;
    var option3 = QUEBEC ;
    var option4 = ONTARIO ;

    function getRegion(sVar)
    {
        if (sVar == sSobeysRegion) {
            return true;
        }
        else {
            return false;
        }
    }

    function setReceiverID(sTmp)
    {
        if (sTmp == "Sobeys Atlantic") {
            sReceiverID = "SOBNBTATLTST:ZZ";
            option1 = ATLANTIC;
            option2 = WEST;
            option3 = ONTARIO;
            option4 = QUEBEC;
        }
        else if (sTmp == "Sobeys West") {
            sReceiverID = "SOBNBTWSTTST:ZZ";
            option2 = ATLANTIC;
            option1 = WEST;
            option3 = QUEBEC;
            option4 = ONTARIO;
        }
        else if (sTmp == "Sobeys Quebec") {
            sReceiverID = "SOBNBTQUETST:ZZ";
            option2 = ATLANTIC;
            option3 = WEST;
            option4 = ONTARIO;
            option1 = QUEBEC;
        }
        else if (sTmp == "Sobeys Ontario") {
            sReceiverID = "SOBNBTONTTST:ZZ";
            option2 = ATLANTIC;
            option3 = WEST;
            option1 = ONTARIO;
            option4 = QUEBEC;
        }
    }
    function getRegionValue(Node)
    {
        var sRegion = "TESTID";
        var xmldoc = Node.ownerDocument;
        var root = xmldoc.documentElement;

        sRegion = root.getAttribute("Region");

        sSobeysRegion = sRegion;
        setReceiverID(sSobeysRegion)

//      return sRegion;
        return "";
    }
    var iLineItemCount = 0;

    function addLineItemCount()
    {
        ++iLineItemCount;

        return iLineItemCount;
    }
    function getLineItemCount()
    {
        return iLineItemCount;
    }

    function getEditNodeName(Node)
    {
        var sTmp = "";
//      var xmldoc = Node.ownerDocument;
//      var root = xmldoc.documentElement;
//      var attribute = null;

//      sTmp = Node.nodeName;

//      attribute = xmldoc .createAttribute("EditItemCount");
//      Node.setAttributeNode(attribute);
//      Node.setAttribute("EditItemCount", getEditItemCount());

        //attribute = Node.getAttributeNode();
//      sTmp += ", " + Node.getAttribute("EditItemCount");
//      sTmp += ", document.form." + Node.nodeName + Node.getAttribute("EditItemCount") + ".value";
//      sTmp = "this";

        sTmp = "this, " + getIndex(Node);

        return sTmp;
    }
    function getName(Node)
    {
        var sTmp = "";

        sTmp =  Node.nodeName;

        return sTmp;
    }

    function getValue(Node)
    {
        var sTmp = "";

        if (Node.childnodes(0)!= null) {
            sTmp = Node.childnodes(0).nodeValue;
        }
        return sTmp;
    }
    function setValue(Node, sValue)
    {


    }
    function TransformDate(Node)
    {
        var sTmp = 0;
        var sDate = "";

        sTmp = Node.childnodes(0).nodeValue;

        sDate = sTmp.substring(4,6) + "/" + sTmp.substring(6,8) + "/" + sTmp.substring(0,4);

        return sDate;
    }
    var iLineCount = 0;
    function getLineCount(Node)
    {
        ++iLineCount;

        return iLineCount;
    }
    var iEditItemCount = 0;

    function getEditItemCount(Node)
    {
        ++iEditItemCount;

        return iEditItemCount;
    }
                function getIndex(node)
                {
                    var iCount = 0;
                    var previousNode = null;

                    while(node.parentNode != null){
                        previousNode =  node.previousSibling;
                        while(previousNode != null){
                            if ((previousNode.nodeName != "#document") && (previousNode.nodeName != "xml")
                            && (previousNode.nodeName != "xml-stylesheet")) {
                                iCount += countChildren(previousNode);
//                                  sValue += previousNode.nodeName + "%";
                                    ++iCount;
                            }
                            previousNode =  previousNode.previousSibling;
                        }

                        node = node.parentNode;
                        if (node.nodeName != "#document") {
                            ++iCount;
//                          sValue += node.nodeName + "*"
                        }
                    }
                    return(iCount);
                }
                function countChildren(node)
                {
                    var iCount = 0;
                    var iTotal = 0;
                    var intNode = 0;

                    iCount = node.childNodes.length;

                    if (iCount > 0) {
                        for (intNode = 0; intNode < iCount; intNode++) {
                          if (node.childNodes(intNode).nodeName != "#text") {
                              iTotal += countChildren(node.childNodes(intNode));
//                             sValue += node.childNodes(intNode).nodeName + "&";
                               ++iTotal;

                          }
                        }
                    }
                    return(iTotal);
                }
                function showNext()
                {
                }

]]></xsl:script>

This is the javascript present in the xsl:script block. The call to this function is below.

`enter code here`


    <xsl:template match="SAC_SAC_01_248">
<xsl:element name='select'>
    <xsl:attribute name='class'>select</xsl:attribute>
    <xsl:attribute name='name'><xsl:value-of-select = "getName(this)">/></xsl:attribute>
    <xsl:attribute name='defaultvalue'><xsl:value-of-select = "getValue(this)"/></xsl:attribute>
    <xsl:attribute name='style'>color: black; font-size: 10px;font-weight: normal;font-family: Verdana, Arial, Helvetica, sans-serif</xsl:attribute>
    <xsl:attribute name='onchange'>onNewCodeChange(<xsl:value-of-select = "getEditNodeName(this)"/>, 2)</xsl:attribute>
    <xsl:element name='Option'>
                <xsl:attribute name='value'></xsl:attribute>
    </xsl:element>
    <xsl:element name='Option'>
                <xsl:attribute name='value'>A</xsl:attribute>
                A
    </xsl:element>
    <xsl:element name='Option'>
                <xsl:attribute name='value'>C</xsl:attribute>
                C
    </xsl:element>
</xsl:element>
</xsl:template>
3
  • xsl:script is not a standard instruction. So you need to define exactly on what product you want to use it. Now, there might be alternatives like using JavaScript to do what needs to be done in JavaScript, before launching the transform (passing extra info as xsl:param or enriching the input tree with extra attributes or elements). Commented Jun 25, 2015 at 7:47
  • IE8 and below used to support a legacy/draft XSL which allowed xsl:eval name (see msdn.microsoft.com/en-us/library/hh180178(v=vs.85).aspx). However, this support ended with IE9 at it was never a standard. For replacing it, see stackoverflow.com/questions/4551931/…. In particular, if you are only using IE, you could use msxsl:script. See msdn.microsoft.com/en-us/library/ms256042.aspx. Commented Jun 25, 2015 at 7:53
  • 1
    Alternatively, you could simply re-write your XSLT to remove the need to call javascript functions. For example, your getName function could probably be removed simply by doing <xsl:value-of select="name(current())" />. Commented Jun 25, 2015 at 7:56

1 Answer 1

3

This is what I do to call JScript method in XSLT 1.0:

Add the following two namespaces (xmlns) to the top of the stylesheet:

xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:jscript="http://www.transvision.dk"

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:jscript="http://www.url.com"
exclude-result-prefixes="msxsl jscript">

Write the javascript code inside the tags using the jscript namespace:

<msxsl:script language="JScript" implements-prefix="jscript">
function getRegion(sVar){
...
}
</msxsl:script>

When calling the javascript method, then remember to use the jscript namespace:

<xsl:value-of select="jscript:getRegion(string($sVar))"/>
Sign up to request clarification or add additional context in comments.

6 Comments

So, when i have to call the function getName(Node), I will have to call as below <xsl:value-of select="jscript:getName(String($Node))"/> OR <xsl:value-of select="jscript:getName(this)"/>
<xsl:value-of select="jscript:getName(String($Node))"/> looks good to me. What is your "this"? If you want to get hold of the current node you are in, the use current() or ".". <xsl:value-of select="jscript:getName(string(current()))"/> The reason why I use string() or number() when giving the arguments, is to avoid problems with jscript being typeless. If you forward a complex node, then you easily get into problems. you can also store the result in a variable: <xsl:variable name="Name" select="jscript:getName(string(current()))"/>
Thank you, i will try that. I also have to access a CDATA variable present. How do i do that. <msxsl:script language="JScript" implements-prefix="jscript"> var sSobeysRegion = "Sobeys West"; var sReceiverID = "SOBEYSCANADA:ZZ"; var ATLANTIC = "Sobeys Atlantic"; var WEST = "Sobeys West"; var QUEBEC = "Sobeys Quebec"; var ONTARIO = "Sobeys Ontario"; var option1 = ATLANTIC ; var option2 = WEST ; var option3 = QUEBEC ; var option4 = ONTARIO ; can xsl:value-of- select be used?
Make a get function for each variable within jscript you need access to in xslt, similar to the getLineItemCount() function you already have.
What should be the value of "http://www.url.com" ? how to determine what should I put there?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.