如何使用Greasemonkey脚本使用XSLT转换XML文件? [英] How to transform an XML file with XSLT, using a Greasemonkey script?

查看:82
本文介绍了如何使用Greasemonkey脚本使用XSLT转换XML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个搜索服务器,它提供了一个测试页面,我可以在其中输入查询,并以XML格式返回结果。我希望能够以更加用户友好的方式完成结果,所以我开始使用XSLT,现在我有一个简单的样式表,将某种臃肿的XML变成一个简单的表格,只显示一些数据。这在我本地执行时工作正常 - 也就是说,将XSL声明添加到XML然后在Firefox等浏览器中打开XML。

I have a search server that provides a test page where I can input a query and it returns the results in XML. I want to be able to go through the results in a more user friendly way so I started playing around with XSLT and I have a simple style-sheet now that turns a somehow bloated XML into a simple table showing just some of the data. This works fine when I do it locally - that is, adding the XSL declaration to the XML and then opening the XML in a browser like Firefox.

我想做什么虽然只要我通过该测试页面从服务器获取结果,就可以在浏览器中实时应用此转换。我调查了一下,发现可以通过javascript实现这一点

What I want to do though is to apply this transformation live in the browser, as soon as I fetch the results from the server through that test page. I investigated a bit and found that it's possible to do this with javascript.

然后我想到了可以动态地将javascript注入页面的Greasemonkey用户脚本。我只需要一个脚本,当我从测试页面获得XML结果时会启动它。但是,我被困在那里因为Greasemonkey似乎不允许脚本在XML文件上运行(至少在Firefox中)。

Then I thought about Greasemonkey userscripts that can inject javascript into a page dynamically. I would just need a script that would kick in when I get the XML results from the test page. However, I'm stuck there because it seems like Greasemonkey isn't allowing scripts to run on XML files (at least in Firefox).

我发现很少有例子和试图用它们作为灵感但却无法使它们发挥作用。 (这是一个,例如。)

I found very few examples and tried to use them as inspiration but couldn't make them work. (Here's one, for example.)

这是我得到的XML的简化示例:

Here's a simplified example of the XML I'm getting:

<?xml version="1.0" encoding="utf-8"?>
<Results>
    <Result>
        <Listings total="2">
            <Res>
                <Result index="0">
                    <id>123456</id>
                    <name>My Business</name>
                    <category>Restaurants</category>
                    <phone>9872365</phone>
                </Result>
            </Res>
            <Res>
                <Result index="1">
                    <id>876553</id>
                    <name>Some Other Business</name>
                    <category>Restaurants</category>
                    <phone>9834756</phone>
                </Result>
            </Res>
        </Listings>
    </Result>
</Results>






这是我在Greasemonkey中加载的脚本 - 什么也没发生:


Here's the script I'm loading in Greasemonkey - where nothing's happening:

// ==UserScript==
// @name test xml renderer
// @namespace http://sample.com
// @description stylesheet for xml results
// @include *
// ==/UserScript==

(function () {
    var xsl_str = '<?xml version="1.0" encoding="utf-8"?>\n\
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">\n\
    <xsl:output method="html"/>\n\
    <xsl:template match="/">\n\
        <html>\n\
            <head></head>\n\
            <body>\n\
                <table id="results" border="1" cellspacing="0" cellpadding="0">\n\
                    <thead>\n\
                        <tr>\n\
                            <th class="name">id</th>\n\
                            <th class="name">category ID</th>\n\
                            <th class="name">name</th>\n\
                            <th class="name">phone</th>\n\
                        </tr>\n\
                    </thead>\n\
                    <tbody>\n\
                        <xsl:for-each select="Results/Result/Listings/Res">\n\
                            <tr>\n\
                                <td class="small" width="120">\n\
                                    <xsl:value-of select="Result/id"/>\n\
                                </td>\n\
                                <td class="small" width="120">\n\
                                    <xsl:value-of select="Result/category"/>\n\
                                </td>\n\
                                <td class="small" width="120">\n\
                                    <xsl:value-of select="Result/name"/>\n\
                                </td>\n\
                                <td class="small" width="120">\n\
                                    <xsl:value-of select="Result/phone"/>\n\
                                </td>\n\
                            </tr>\n\
                        </xsl:for-each>\n\
                    </tbody>\n\
                </table>\n\
            </body>\n\
        </html>\n\
    </xsl:template>\n\
</xsl:stylesheet>\n\
';

    var processor = new XSLTProcessor();
    var dataXSL = new DOMParser().parseFromString(xsl_str, "text/xml");

    processor.importStylesheet(dataXSL);
    dataXML = document;
    var ownerDocument = document.implementation.createDocument("", "", null);
    var newFragment = processor.transformToFragment(dataXML, ownerDocument);
    dataXML.documentElement.replaceChild(newFragment, dataXML.documentElement.firstChild);
})();

当我在Greasemonkey中启用此脚本时,所有页面都成功替换为XSL模板中的HTML 。但它似乎不适用于本地XML文件或来自我的服务器的任何XML。(我知道要使Greasemonkey与本地文件一起工作,需要在 about:config - extensions.greasemonkey.fileIsGreaseable )。

When I enable this script in Greasemonkey then all pages are successfully replaced with the HTML above in the XSL template. However it doesn't seem to apply to a local XML file or any XML coming from my server.(I know that to make Greasemonkey work with local files a setting needs to be changed in about:config in Firefox - extensions.greasemonkey.fileIsGreaseable).

我没有任何经验使用javascript所以我很可能只是犯了一个非常基本的错误。在这种情况下,非常感谢所有帮助。

I don't have any experience with javascript so most likely I'm just making a very basic mistake. In in case, all the help is really appreciated.

推荐答案

该脚本正在进行核算或添加到文档。头。您希望用已转换的内容替换整个文档。 可以通过将 location.href 更改为适当构造的数据: URL来实现此目的。但更简洁的方法是替换整个 document.documentElement

That script is nuking or adding to document.head. You want to replace the whole document with transformed content. You could do that by changing location.href to an appropriately constructed data: URL. But a neater approach is to replace the whole document.documentElement.

此脚本适用于您的测试/样本XML文件:

This script works on your test/sample XML file:

// ==UserScript==
// @name        _Test XML Renderer
// @description stylesheet for xml results
// @include     http://YOUR_SERVER.COM/YOUR_PATH/*.xml
// @grant       none
// ==/UserScript==

var xsl_str = '<?xml version="1.0" encoding="utf-8"?>\n\
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">\n\
    <xsl:output method="html"/>\n\
    <xsl:template match="/">\n\
        <html>\n\
            <head></head>\n\
            <body>\n\
                <table id="results" border="1" cellspacing="0" cellpadding="0">\n\
                    <thead>\n\
                        <tr>\n\
                            <th class="name">id</th>\n\
                            <th class="name">category ID</th>\n\
                            <th class="name">name</th>\n\
                            <th class="name">phone</th>\n\
                        </tr>\n\
                    </thead>\n\
                    <tbody>\n\
                        <xsl:for-each select="Results/Result/Listings/Res">\n\
                            <tr>\n\
                                <td class="small" width="120">\n\
                                    <xsl:value-of select="Result/id"/>\n\
                                </td>\n\
                                <td class="small" width="120">\n\
                                    <xsl:value-of select="Result/category"/>\n\
                                </td>\n\
                                <td class="small" width="120">\n\
                                    <xsl:value-of select="Result/name"/>\n\
                                </td>\n\
                                <td class="small" width="120">\n\
                                    <xsl:value-of select="Result/phone"/>\n\
                                </td>\n\
                            </tr>\n\
                        </xsl:for-each>\n\
                    </tbody>\n\
                </table>\n\
            </body>\n\
        </html>\n\
    </xsl:template>\n\
</xsl:stylesheet>\n\
';

var processor   = new XSLTProcessor ();
var dataXSL     = new DOMParser ().parseFromString (xsl_str, "text/xml");

processor.importStylesheet (dataXSL);

var newDoc      = processor.transformToDocument (document);

//-- These next lines swap the new, processed doc in for the old one...
window.content  = newDoc;

document.replaceChild (
    document.importNode (newDoc.documentElement, true),
    document.documentElement
);

这篇关于如何使用Greasemonkey脚本使用XSLT转换XML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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