您可以使用 JQuery 通过 XSLT 将 XML 转换为 XML [英] Can you use JQuery to transform XML to XML via XSLT

查看:15
本文介绍了您可以使用 JQuery 通过 XSLT 将 XML 转换为 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,其中包含指向根据文档类型动态填充的文档的链接,并且所有数据都位于一个中央 xml 文件中.我想让 JQuery 向样式表传递一个参数,样式表根据传递的参数使用 xpath 隔离节点,然后根据属性对注释进行排序.从我发现的所有文档中,JQuery 本身并不支持 XSLT,并且一旦原始 xml 被转换,任何 3rd 方插件都不能返回新的 XML 对象.我是否遗漏了某些东西,或者我试图做的事情是不可能的?xsl 文件已经在 javascript 之外进行了测试,并且可以完美运行.

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.

这是没有转换的代码示例

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
        });

    }
});

推荐答案

另一个是 http://jquery.glyphix.com/jquery.xslTransform/example/index.html

// 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'
    }
);

或者如一般文档所述:

$.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');

链接页面上有一个用法示例,猜测它可以满足您的需求,尽管它是 sarissa 的 javascript 包装器,它试图为所有浏览器中的 XSL 工具制作一个独立于浏览器的 API.

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.

这篇关于您可以使用 JQuery 通过 XSLT 将 XML 转换为 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆