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

后端 未结 2 722
执笔经年
执笔经年 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 = '\n\
    \n\
        \n\
        \n\
            \n\
                \n\
                \n\
                    \n\
                        \n\
                            \n\
                                \n\
                                \n\
                                \n\
                                \n\
                            \n\
                        \n\
                        \n\
                            \n\
                                \n\
                                    \n\
                                    \n\
                                    \n\
                                    \n\
                                \n\
                            \n\
                        \n\
                    
    idcategory IDnamephone
    \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\
    \n\ \n\ \n\
    \n\
    \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 );

提交回复
热议问题