How to transform an XML file with XSLT, using a Greasemonkey script?

后端 未结 2 702
执笔经年
执笔经年 2021-01-15 02:56

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 frie

相关标签:
2条回答
  • 2021-01-15 03:10

    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.

    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
    );
    
    0 讨论(0)
  • 2021-01-15 03:23

    I would have added this in a comment, but I don't have the relevant reputation. There are two things that I would check. Especially if this is working on a string, and not a file from the server like you said

    1. Make sure that what you are getting from the server is a string, and not an object
    2. Make sure the xml you are getting from the server is correct.

    If you aren't getting a string, you can do an ajax request to get the text from the xml. And then load that as your new xml var.

    And if you really just wanted to make your xml user friendly, I would suggest checking out
    http://code.google.com/p/vkbeautify
    and
    http://google-code-prettify.googlecode.com/svn/trunk/README.html

    Those should maintain the xml format while stylizing it to make it easy to read. Plus, you won't have to mess with greasemonkey, and can instead just use javascript.

    0 讨论(0)
提交回复
热议问题