我如何使用 JavaScript 来转换 XML &XSLT? [英] How can I use JavaScript to transform XML & XSLT?

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

问题描述

我想使用 JavaScript 来显示我的 XSLT,但在我的服务器上,浏览器中没有显示任何内容.

I want to use JavaScript to show my XSLT, but on my server nothing is showing up in the browser.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script type="text/javascript" src="main.js"></script>
<meta http-equiv="Content-Language" content="en-us"/>
<title>Contracting, Licensing and Compliance News</title>
</head>
<body>
<script language="javascript">
    function displayMessage() {
        // Load XML 

        var xml = new ActiveXObject("Microsoft.XMLDOM")
        xml.async = false
        xml.load("site-index.xml")

        // Load the XSL
        var xsl = new ActiveXObject("Microsoft.XMLDOM")
        xsl.async = false
        xsl.load("site-index.xsl")

        // Transform
        document.write(xml.transformNode(xsl))
    }
</script>
</body>
</html>

推荐答案

浏览器可以为您执行转换.不需要 javascript,只需像这样从 .xml 链接 .xsl:

The browser can perform the tranform for you. No javascript is needed, just link the .xsl from the .xml like this:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="site-index.xsl" ?>

只提供 xml,没有 html.假设您的 .xsl 包含正确的代码,沿线

and just serve the xml, no html. Assuming your .xsl contains the right code, along the lines

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="4.01" indent="yes"/>
<xsl:output doctype-system="http://www.w3.org/TR/html4/strict.dtd"/>
<xsl:output doctype-public="-//W3C//DTD HTML 4.01//EN"/>

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

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