javascript轻松实现调用xslt解析xml
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
<html>
<head>
<title>student</title>
<script language="javascript">
function doRun()
{
var myXml=new ActiveXObject("Microsoft.XMLDOM");
myXml.async=false;
myXml.load("hello.xml");
var myXsl=new ActiveXObject("Microsoft.XMLDOM");
myXsl.async=false;
myXsl.load("hello.xsl");
//document.write(myXml.transformNode(myXsl));
document.getElementById('divXslt').innerHTML = myXml.transformNode(myXsl);
}
</script>
</head>
<body>
<input type="button" value="执行" onclick="doRun();"></input>
<div id="divXslt"></div>
</body>
</html>
<head>
<title>student</title>
<script language="javascript">
function doRun()
{
var myXml=new ActiveXObject("Microsoft.XMLDOM");
myXml.async=false;
myXml.load("hello.xml");
var myXsl=new ActiveXObject("Microsoft.XMLDOM");
myXsl.async=false;
myXsl.load("hello.xsl");
//document.write(myXml.transformNode(myXsl));
document.getElementById('divXslt').innerHTML = myXml.transformNode(myXsl);
}
</script>
</head>
<body>
<input type="button" value="执行" onclick="doRun();"></input>
<div id="divXslt"></div>
</body>
</html>
hello.xsl
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="GB2312"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<html>
<head>
<title>First XSLT example</title>
</head>
<body>
<table border="1" width="50%">
<tr style="background-color:yellow">
<td>姓名</td>
<td>年龄</td>
</tr>
<xsl:for-each select="PEOPLE/PERSON">
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="age" /></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<html>
<head>
<title>First XSLT example</title>
</head>
<body>
<table border="1" width="50%">
<tr style="background-color:yellow">
<td>姓名</td>
<td>年龄</td>
</tr>
<xsl:for-each select="PEOPLE/PERSON">
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="age" /></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
hello.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="GB2312"?>
<!--<?xml-stylesheet type="text/xsl" href="hello.xsl"?>-->
<PEOPLE>
<PERSON>
<name>ajie</name>
<age>28</age>
</PERSON>
<PERSON>
<name>tom</name>
<age>24</age>
</PERSON>
<PERSON>
<name>miake</name>
<age>30</age>
</PERSON>
</PEOPLE>
<!--<?xml-stylesheet type="text/xsl" href="hello.xsl"?>-->
<PEOPLE>
<PERSON>
<name>ajie</name>
<age>28</age>
</PERSON>
<PERSON>
<name>tom</name>
<age>24</age>
</PERSON>
<PERSON>
<name>miake</name>
<age>30</age>
</PERSON>
</PEOPLE>