簡體   English   中英

使用xsl調用用javascript編寫的用戶定義函數

[英]Calling a user defined function written in javascript using xsl

我在XSL樣式表中有一個javascript。 在javascript中,我有一個函數,getName(node)。 如何在xsl中稱呼它? 我之前使用過xsl:eval = getname(this)。 它一直工作到IE8。 Hasnt從IE9開始工作。 我可以使用xsl:value-of-select嗎? 或任何其他技巧使其起作用? 先感謝您。

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

這是xsl:script塊中存在的javascript。 下面是對該函數的調用。

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

這是我在XSLT 1.0中調用JScript方法的操作:

將以下兩個命名空間(xmlns)添加到樣式表的頂部:

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

使用jscript命名空間在標簽內編寫javascript代碼:

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

調用javascript方法時,請記住使用jscript命名空間:

<xsl:value-of select="jscript:getRegion(string($sVar))"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM