• XSLT on the Client and Server


    1.XSLT on the Client

    @RequestMapping(value = "/XSLTClient")
    public String XSLTClient() {
        return "XSLTOnClient";
    }
    
    <!-- XSLTOnClient.html -->
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <script>
            function loadXMLDoc(filename)
            {
                if (window.ActiveXObject)
                {
                    xhttp = new ActiveXObject("Msxml2.XMLHTTP");
                }
                else
                {
                    xhttp = new XMLHttpRequest();
                }
                xhttp.open("GET", filename, false);
                try {xhttp.responseType = "msxml-document"} catch(err) {} // Helping IE11
                xhttp.send("");
                return xhttp.responseXML;
            }
    
            function displayResult()
            {
                // "[[@{cdcatalog_client.xml}]]"这里使用了Thymeleaf模板引擎的语法。
                xml = loadXMLDoc("[[@{cdcatalog_client.xml}]]");
                xsl = loadXMLDoc("[[@{cdcatalog.xsl}]]");
                // code for IE
                if (window.ActiveXObject || xhttp.responseType == "msxml-document")
                {
                    ex = xml.transformNode(xsl);
                    document.getElementById("example").innerHTML = ex;
                }
                // code for Chrome, Firefox, Opera, etc.
                else if (document.implementation && document.implementation.createDocument)
                {
                    xsltProcessor = new XSLTProcessor();
                    xsltProcessor.importStylesheet(xsl);
                    resultDocument = xsltProcessor.transformToFragment(xml, document);
                    document.getElementById("example").appendChild(resultDocument);
                }
            }
        </script>
        <title>XSLT on the Client</title>
    </head>
    <body onload="displayResult()">
    <div id="example"></div>
    </body>
    </html>
    

    2.XSLT on the Server

    @RequestMapping("/XSLTServer")
    public String XSLTServer() throws TransformerException, IOException {
        Source xslt = new StreamSource(new ClassPathResource("static/cdcatalog.xsl").getFile());
        Source text = new StreamSource(new ClassPathResource("static/cdcatalog_client.xml").getFile());
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(xslt);
    
        transformer.transform(text, new StreamResult(new File("cdcatalog.html")));
        return "XSLTOnClient";
    }
    



    注:w3schools官网的示例用的是PHP和ASP,都可以直接返回XML通过XSLT转换后的HTML。

    参考:

  • 相关阅读:
    js 高阶函数之柯里化
    JavaScript 相关的工具代码
    JS 数组、对象的深拷贝
    页面性能优化
    axios(封装使用、拦截特定请求、判断所有请求加载完毕)
    java 实现登录验证码 (kaptcha 验证码组件)
    告别 hash 路由,迎接 history 路由
    解决 Vue 动态生成 el-checkbox 点击无法赋值问题
    分享基于 websocket 网页端聊天室
    vue + element 动态渲染、移除表单并添加验证
  • 原文地址:https://www.cnblogs.com/gzhjj/p/13540197.html
Copyright © 2020-2023  润新知