将参数从URL传递到XSL样式表
在过去的几天里,我一直在深入研究这些消息,寻找将参数从URL传递到XSL样式表的方法 例如,我有一个当前url,比如:将参数从URL传递到XSL样式表,url,xslt,Url,Xslt,在过去的几天里,我一直在深入研究这些消息,寻找将参数从URL传递到XSL样式表的方法 例如,我有一个当前url,比如: http://localhost/blocableau/data/base_bloc/blocableau3x.xml?95.2 我只想选择后面的值?如本例中的95.2,将其放入变量var_massif中 我用javascript尝试了下面的代码来测试值子字符串(1),但用xsl它不起作用 <?xml version="1.0" ?> <xsl:styles
http://localhost/blocableau/data/base_bloc/blocableau3x.xml?95.2
我只想选择后面的值?如本例中的95.2
,将其放入变量var_massif
中
我用javascript尝试了下面的代码来测试值子字符串(1),但用xsl它不起作用
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" />
<!-- recherche des cotations par Massif selon une variable -->
<xsl:key name="byMassif" match="bloc" use="Massif" />
<xsl:template match="BLOCS">
<script>var requete = location.search.substring(1);
mavariable=requete;alert(mavariable);x=mavariable</script>
<xsl:variable name="var_massif"><xsl:value-of select="95.2" /></xsl:variable>
<span>Ma variable xsl est : <xsl:value-of select="$var_massif"/></span>
<div style="position:relative; top:0px;left:10px;font-family: Arial, 'Helvetica Neue', 'Helvetica, sans-serif'; font-size:12px;z-index:1;">
<!-- genere un id pour chaque valeur de cotation et mise en forme -->
<xsl:for-each select="bloc[generate-id() = generate-id(key('byMassif', $var_massif)[1])]" >
<xsl:sort select="Massif" />
var requete=位置.搜索.子字符串(1);
mavariable=重新报价;警惕(可变);x=可变量
Ma变量xsl est:
我假设您正在加载一个使用XSL处理指令转换文件的XML:
<?xml-stylesheet ... ?>
在XSLT样式表中,应该有一个全局参数变量:
<xsl:param name="var_massif" />
此输入(blocableau3x.xml
)位于同一目录中:
<BLOCS>
<bloc>
<Massif></Massif>
</bloc>
</BLOCS>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.0/jquery.js"></script>
<script>
$( document ).ready(function() {
var var_massif = location.search.substring(1); // gets the string and removes the `?`
var body = $("body")[0]; // gets the body element where the result will be inserted
if (var_massif) {
var processor = new XSLTProcessor(); // starts the XSL processor
processor.setParameter(null, "var_massif", var_massif);
var source;
var xslReq = $.get("stylesheet.xsl", function (data) { // loads the stylesheet
processor.importStylesheet(data);
});
var xmlReq = $.get("blocableau3x.xml", function (data) { // loads the xml
source = data;
});
$.when(xslReq, xmlReq).done(function () { // waits both to load
var result = processor.transformToDocument(source); // transforms document
body.appendChild(result.documentElement); // adds result as child of body element
});
} else {
body.html("<h1>Missing query string</h1>"); // in case there is no query string
}
});
</script>
<html>
<body></body>
</html>
现在,在同一目录中创建此HTML文件(blocableau3x.HTML):
<BLOCS>
<bloc>
<Massif></Massif>
</bloc>
</BLOCS>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.0/jquery.js"></script>
<script>
$( document ).ready(function() {
var var_massif = location.search.substring(1); // gets the string and removes the `?`
var body = $("body")[0]; // gets the body element where the result will be inserted
if (var_massif) {
var processor = new XSLTProcessor(); // starts the XSL processor
processor.setParameter(null, "var_massif", var_massif);
var source;
var xslReq = $.get("stylesheet.xsl", function (data) { // loads the stylesheet
processor.importStylesheet(data);
});
var xmlReq = $.get("blocableau3x.xml", function (data) { // loads the xml
source = data;
});
$.when(xslReq, xmlReq).done(function () { // waits both to load
var result = processor.transformToDocument(source); // transforms document
body.appendChild(result.documentElement); // adds result as child of body element
});
} else {
body.html("<h1>Missing query string</h1>"); // in case there is no query string
}
});
</script>
<html>
<body></body>
</html>
$(文档).ready(函数(){
var var_massif=location.search.substring(1);//获取字符串并删除``
var body=$(“body”)[0];//获取将插入结果的body元素
if(var_massif){
var processor=new XSLTProcessor();//启动XSL处理器
processor.setParameter(null,“var_massif”,var_massif);
var源;
var xslReq=$.get(“stylesheet.xsl”),函数(数据){//加载样式表
处理器。导入样式表(数据);
});
var xmlReq=$.get(“blocableau3x.xml”),函数(数据){//加载xml
来源=数据;
});
$.when(xslReq,xmlReq).done(函数(){//等待加载这两个函数
var result=processor.transformToDocument(源);//转换文档
body.appendChild(result.documentElement);//将结果添加为body元素的子级
});
}否则{
body.html(“缺少查询字符串”);//如果没有查询字符串
}
});
与其在URL中调用XML文件,不如调用HTML文件。加载时,它将加载XSL文件和XML文件,并使用XSL处理器处理该文件。它将能够读取查询参数并将其作为XSL全局参数传递,您可以在XSLT文件中读取该参数。您使用什么语言来处理XSLT-PHP、JS等?我尝试了这个方法,但不起作用:-(Ma variable xsl est:如果您使用Web浏览器加载XML文件,它可能不会工作,因为没有XSLT 2.0支持,所以您应该尝试JavaScript解决方案。如果您使用其他应用程序加载文件,您还必须检查您使用的解析器是否支持XSLT 2.0,因为
document-uri()
如果函数不起作用,它将不起作用。是的,我使用浏览器读取xml文件。我是代码中的傻瓜,不知道该怎么做。使用此代码的javascript解决方案不起作用。var_massif=location.search.substring(1);var processor=new XSLTProcessor();processor.setParameter(null,“var_massif”,var_massif);警报(var_massif);如果您将它放在HTML文件中,调用HTML文件,该文件将通过Ajax加载XSL+XSLT。我将向您展示一个示例。