javascript 您可以使用 JQuery 通过 XSLT 将 XML 转换为 XML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6578154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can you use JQuery to transform XML to XML via XSLT
提问by Chris
I have a website that has links to documents that are dynamically populated based on the document type and all the data is located in one central xml file. I wanted have JQuery pass a parameter to the style sheet, the style sheet segregate out the the nodes using xpath based on the passed parameter, and then sort the notes based on an attribute. From all the documentation I found, JQuery doesn't natively support XSLT and none of the 3rd party plugins can return a new XML object once the original xml has been transformed. Am I missing something or is what I'm trying to do not possible? The xsl file has been tested outside of javascript and it works flawlessly.
我有一个网站,其中包含指向根据文档类型动态填充的文档的链接,并且所有数据都位于一个中央 xml 文件中。我想让 JQuery 向样式表传递一个参数,样式表根据传递的参数使用 xpath 隔离节点,然后根据属性对注释进行排序。从我发现的所有文档中,JQuery 本身并不支持 XSLT,并且一旦原始 xml 被转换,任何 3rd 方插件都不能返回新的 XML 对象。我是否遗漏了某些东西,或者我试图做的事情是不可能的?xsl 文件已经在 javascript 之外进行了测试,并且可以完美运行。
Here is a sample of the code without the transform
这是没有转换的代码示例
$.ajax({
type: "GET",
url: "xml/charts.xml",
dataType: "xml",
success: function(xml) {
$(xml).find('chart').each(function(){
// Create link here
});
}
});
回答by Konstantin
Another one is jquery.xslTransform on http://jquery.glyphix.com/jquery.xslTransform/example/index.html
另一个是http://jquery.glyphix.com/jquery.xslTransform/example/index.html 上的jquery.xslTransform
// now load both files into variables for the next 2 transformations
var xsldoc = $.xsl.load('test.xsl');
var xmldoc = $.xsl.load('test.xml');
// with an xpath
$('#with').getTransform(
xsldoc,
xmldoc,
{
xpath: '/test/inside'
}
);
Or as general documentation states:
或如一般文件所述:
$.getTransform(
'path-to-xsl.xsl', // path or xsl document in javascript variable
'path-to-xml.xml', // path or xml document in javascript variable
{
params: { // object for your own xsl parameters
paramName1: 'paramValue1',
paramName2: 'paramValue2'
},
xpath: '/test/inside', // trims your xml file to that defined by this xpath
eval: true, // evaluates any <script> blocks it finds in the transformed result
callback: function(){} // getTransform evaluates this function when transformation is complete
});
// loads an xml file, parses it and stores it in xmlDoc
var xmlDoc = xslTransform.load('path-to-xml.xml');
There's an example of usage on the linked page, guess it can suit your needs although it's a javascript wrapper of sarissa which is trying to make a browser-independent API for XSL tools in all browsers.
链接页面上有一个使用示例,猜测它可以满足您的需求,尽管它是 sarissa 的 javascript 包装器,它试图为所有浏览器中的 XSL 工具制作与浏览器无关的 API。
回答by Dennis Kreminsky
You can do XSLT transformations in Javascript, jQuery is not even involved in this process, however I seriously doubt that you would be able to pass any parameters to the processor.
您可以在 Javascript 中进行 XSLT 转换,jQuery 甚至不参与此过程,但是我严重怀疑您是否能够将任何参数传递给处理器。
There is a tutorialon XSLT processing using javascript on w3schools.
在 w3schools 上有一个关于使用 javascript 处理 XSLT的教程。
回答by cuq
A more portable implementation is ajaxslt ( http://goog-ajaxslt.sourceforge.net/), it is limited but it works fine in many situations. I used it some time ago for a proyect and it worked even in explorer 6.
一个更便携的实现是 ajaxslt ( http://goog-ajaxslt.sourceforge.net/),它是有限的,但它在许多情况下都能正常工作。前段时间我将它用于一个项目,它甚至在资源管理器 6 中也能工作。