明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

使用xslt對(duì)xml進(jìn)行縮進(jìn)格式化處理

[摘要]net_lover [原作] 下面就是簡(jiǎn)單的例子,這里提供2中方法:test.htm<SCRIPT> //裝載數(shù)據(jù) x = "<r><a name='net_lover'>aaaaaaaaaaa</a> &l...

net_lover [原作]

下面就是簡(jiǎn)單的例子,這里提供2中方法:

test.htm

<SCRIPT>
 //裝載數(shù)據(jù)
 x = "<r><a name='net_lover'>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)
 
  // 創(chuàng)建結(jié)果對(duì)象
  var result = new ActiveXObject("Msxml2.DOMDocument");
  result.async = false;

  // 把解析結(jié)果放到結(jié)果對(duì)象中方法1
  source.transformNodeToObject(stylesheet, result);
  alert(result.xml)
 
   // 把解析結(jié)果放到結(jié)果對(duì)象中方法2
  result2 = ""
  result2 = source.transformNode(stylesheet);
  source.loadXML(result2)
  alert(source.xml)
</SCRIPT>


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>