【问题标题】:how to supply stylesheet variable value from html or javascript如何从 html 或 javascript 提供样式表变量值
【发布时间】:2012-03-08 09:20:54
【问题描述】:

我的问题是关于在使用 xsl 的客户端上进行渲染。这已经在 IE 中工作了,但我想让它在 firefox 上工作

一、样式表(variablexsl.xsl)这里唯一特别的是存在

<xsl:variable name="module" select="string('RES')"/>

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <xsl:variable name="module" select="string('RES')"/>
        <table cellpadding="0" cellspacing="0" border="0" ID="randomID">
            <tr>
                <xsl:choose>
                    <xsl:when test="$module = 'EDU'">
                        <td>EDU was supplied..</td>
                    </xsl:when>
                </xsl:choose>
            </tr>
            <tr>
                <xsl:choose>
                    <xsl:when test="$module = 'RES'">
                        <td>RES was supplied..</td>
                    </xsl:when>
                </xsl:choose>
            </tr>
        </table>
    </xsl:template>
</xsl:stylesheet>

现在,html 文件 index.html

<html>
<head>
    <script>
        function selectmTab(args) {
            var xml = loadXMLDoc("variabledata.xml"); //ajax call and holds the responseXML. variabledata.xml is empty
            var xsl = loadXMLDoc("variablexsl.xsl");  //ajax call and holds the responseXML
            var ss2 = xsl.selectSingleNode('//xsl:variable/@select');
            ss2.value = "string('" + args + "')";
            document.getElementById("xsltest").innerHTML = xml.transformNode(xsl);
        }
    </script>
</head>
<body onload="displayResult()">
    <div>
    <input type="button" value="EDU" onclick="selectmTab('EDU');" />
    <input type="button" value="RES" onclick="selectmTab('RES');" />
    </div>
    <div id="xsltest"></div>
</body>
</html>

所以最后,当我单击 EDU 和 RES 按钮时,文本在 IE 中正确显示,但在任何其他浏览器中都没有。我尝试使用 document.evaluate() 但一直出错.. 最后求助于 SO!

谢谢!

解决方案: 对样式表进行以下更改,然后使用 xsltprocessor().setParameter 为我工作。

样式表的变化:(在样式表声明之后添加一个新的 xsl:param)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="winnersOnly">RES</xsl:param>

然后,在&lt;xsl:template match="/"&gt; 之后修改&lt;xsl:variable&gt; 声明如下:

<xsl:template match="/">
<xsl:variable  name="module" select="$winnersOnly"/>

Firefox 客户端代码

//function loadXMLDoc() 使用XMLHttpRequest 对象发出ajax 请求,并将responseXML 返回给调用者。

var processor = new XSLTProcessor();
xslholder = loadXMLDoc("styles.xsl");
processor.importStylesheet(xslholder);
processor.setParameter(null, "winnersOnly", "EDU"); //setting the value here
xmlholder = loadXMLDoc("data.xml");
var ownerDocument = document.implementation.createDocument("", "test", null);
var newFragment = processor.transformToFragment(xmlholder, ownerDocument);
document.getElementById("fragment").appendChild(newFragment);

【问题讨论】:

  • 请注意,您不必为transformToFragment 创建临时文档;简单地做例如var targetEl = document.getElementById("fragment"); var newFragment = processor.transformToFragment(xmlholder, targetEl.ownerDocument); targetEl.appendChild(newFragment);.

标签: xslt selectsinglenode xsl-stylesheet xsl-variable


【解决方案1】:

XSLT 有一个定义明确的机制来将外部参数传递给 XSLT 样式表,即通过在样式表中放置顶级 xsl:param 元素来定义外部参数的名称(如果需要,还可以定义默认值),然后通过使用 XSLT 处理器的 API 在运行转换之前设置参数。

对于 Mozilla、Opera、Safari、Chrome,您可以使用相同的 API:https://developer.mozilla.org/en/Using_the_Mozilla_JavaScript_interface_to_XSL_Transformations#Setting_parameters

对于使用 MSXML 的 IE,您需要使用相应的 MSXML API:http://msdn.microsoft.com/en-us/library/ms762312%28v=vs.85%29.aspx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多