JS中解析xml,并将XML文档用XSL文档进行转换

<html>
<body>
<script type="text/javascript">
//建立一个解析的对象,并将xml文档读入内存.
  var xml = new ActiveXObject("Microsoft.XMLDOM") 
  xml.async = false
  xml.load("test.xml")
  // 建立另一个解析对象,导入xsl文档;
  var xsl = new ActiveXObject("Microsoft.XMLDOM")
  xsl.async = false
  xsl.load("test.xsl")
  // 将XML文档用XSL文档进行转换;
  document.write(xml.transformNode(xsl));
</script>
</body>
</html>
 
<?xml version="1.0" encoding="ISO8859-1" ?>
<CATALOG>
  <CD>
    <TITLE>Empire Burlesque</TITLE>
    <ARTIST>Bob Dylan</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Columbia</COMPANY>
    <PRICE>10.90</PRICE>
    <YEAR>1985</YEAR>
  </CD>
</CATALOG>
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">

<xsl:template match="/">
  <html>
      <body>
        <table border="2" bgcolor="yellow">
           <tr>
               <th>Title</th>
               <th>Artist</th>
           </tr>
           <xsl:for-each select="CATALOG/CD">
           <tr>
               <td><xsl:value-of select="TITLE"/></td>
               <td><xsl:value-of select="ARTIST"/></td>
           </tr>
           </xsl:for-each>
         </table>
     </body>
  </html>

</xsl:template>
</xsl:stylesheet>

JS中解析xml,并将XML文档用XSL文档进行转换.

你可能感兴趣的:(解析xml)