Use xslt to indent and format xml

黄舟
Release: 2017-03-02 17:34:17
Original
2182 people have browsed it

The following is a simple example, here are 2 methods:

test.htm

<SCRipT>
 //装载数据
 x = "<r><a name=&#39;net_lover&#39;>aaaaaaaaaaa</a>         <b>bbbbbbb</b><a>aaaaaaaaaaa</a><b>bbbbbbb</b></r>"
  var source = new ActiveXObject("Msxml2.DOMDocument");
  source.async = false;
  source.loadXML(x)
  alert(source.xml)
  // 装载样式单
  var stylesheet = new ActiveXObject("Msxml2.DOMDocument");
  stylesheet.async = false;
  stylesheet.resolveExternals = false;
  stylesheet.load("style.xsl");
 alert(stylesheet.xml)
 
  // 创建结果对象
  var result = new ActiveXObject("Msxml2.DOMDocument");
  result.async = false;
  // 把解析结果放到结果对象中方法1
  source.transformNodeToObject(stylesheet, result);
  alert(result.xml)
  
   // 把解析结果放到结果对象中方法2
  result2 = ""
  result2 = source.transformNode(stylesheet);
  source.loadXML(result2)
  alert(source.xml)
</SCRIPT>
Copy after login

style.xsl

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method = "xml"  omit-xml-declaration = "yes" indent = "yes"/>
 <xsl:template match="/ | @* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
Copy after login

The above is to use xslt to indent xml Formatting content, please pay attention to the PHP Chinese website (m.sbmmt.com) for more related content!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!