Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在使用浏览器转换XML时,是否可以通过URL将参数传递给XSLT?_Xml_Xslt_Browser_Transform_Param - Fatal编程技术网

在使用浏览器转换XML时,是否可以通过URL将参数传递给XSLT?

在使用浏览器转换XML时,是否可以通过URL将参数传递给XSLT?,xml,xslt,browser,transform,param,Xml,Xslt,Browser,Transform,Param,当使用浏览器转换XML(Google Chrome或IE7)时,是否可以通过URL将参数传递给XSLT样式表 例如: data.xml <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="sample.xsl"?> <root> <document type="resume"> <author>John Doe

当使用浏览器转换XML(Google Chrome或IE7)时,是否可以通过URL将参数传递给XSLT样式表

例如:

data.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="sample.xsl"?>
<root>
    <document type="resume">
        <author>John Doe</author>
    </document>
    <document type="novella">
        <author>Jane Doe</author>
    </document>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format">

    <xsl:output method="html" />
    <xsl:template match="/">
    <xsl:param name="doctype" />
    <html>
        <head>
            <title>List of <xsl:value-of select="$doctype" /></title>
        </head>
        <body>
            <xsl:for-each select="//document[@type = $doctype]">
                <p><xsl:value-of select="author" /></p>
            </xsl:for-each>
        </body>
    </html>
</<xsl:stylesheet>

无名氏
无名氏
sample.xsl

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="sample.xsl"?>
<root>
    <document type="resume">
        <author>John Doe</author>
    </document>
    <document type="novella">
        <author>Jane Doe</author>
    </document>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format">

    <xsl:output method="html" />
    <xsl:template match="/">
    <xsl:param name="doctype" />
    <html>
        <head>
            <title>List of <xsl:value-of select="$doctype" /></title>
        </head>
        <body>
            <xsl:for-each select="//document[@type = $doctype]">
                <p><xsl:value-of select="author" /></p>
            </xsl:for-each>
        </body>
    </html>
</<xsl:stylesheet>

名单


您可以在服务器端生成XSLT,即使转换是客户端的

这允许您使用动态脚本来处理参数

例如,您可以指定:

<?xml-stylesheet type="text/xsl"href="/myscript.cfm/sample.xsl?paramter=something" ?>


然后在myscript.cfm中,您将输出XSL文件,但使用动态脚本处理查询字符串参数(这将根据您使用的脚本语言而有所不同)。

不幸的是,否-您不能仅在客户端将参数传递给XSLT。 web浏览器从XML中获取处理指令;并使用XSLT直接转换它



可以通过querystring URL传递值,然后使用JavaScript动态读取它们。但是,这些不能在XSLT(XPath表达式)中使用,因为浏览器已经转换了XML/XSLT。它们只能在呈现的HTML输出中使用。

只需将参数作为属性添加到XML源文件中,并将其用作样式表的属性

xmlDoc.documentElement.setAttribute(“myparam”,getParameter(“myparam”))
JavaScript函数如下所示:

//在javascript中获取querystring请求参数
函数getParameter(parameterName){
var queryString=window.top.location.search.substring(1);
//在参数名称中添加“=”(即参数名称=值)
var parameterName=parameterName+“=”;
如果(queryString.length>0){
//找到字符串的开头
begin=queryString.indexOf(参数名称);
//如果找不到参数名,请跳过它,否则返回值
如果(开始!=-1){
//将长度(整数)添加到开头
begin+=参数name.length;
//多个参数由“&”符号分隔
end=queryString.indexOf(“&”,begin);
如果(结束==-1){
end=queryString.length
}
//返回字符串
返回unescape(queryString.substring(begin,end));
}
//如果未找到任何参数,则返回“null”
返回“null”;
}
}

问题的第二部分是,是否可能仅限于客户端?