以文本方式查看主题

-  W3CHINA.ORG讨论区 - 语义网·描述逻辑·本体·RDF·OWL  (http://bbs.xml.org.cn/index.asp)
--  『 XSL/XSLT/XSL-FO/CSS 』  (http://bbs.xml.org.cn/list.asp?boardid=8)
----  使用XSLT转化xml格式问题, 急!!![求助]  (http://bbs.xml.org.cn/dispbbs.asp?boardid=8&rootid=&id=39413)


--  作者:xiaoyupan
--  发布时间:10/27/2006 6:36:00 AM

--  使用XSLT转化xml格式问题, 急!!![求助]
使用XSLT转化xml格式问题, 急!!!
源xml格式
<?xml version = '1.0' encoding = 'UTF-8'?>
<Book name="XML">
 <Chapter no="1">
  <Section no="1">
  </Section>
  <Section no="2">
  </Section>
  <Section no="3">
  </Section>
 </Chapter>
 <Chapter no="2">
  <Section no="1">
  </Section>
  <Section no="2">
  </Section>
  <Section no="3">
  </Section>
 </Chapter>
 <Chapter no="3">
 </Chapter>
</Book>

目标格式
<?xml version = '1.0' encoding = 'UTF-8'?>
<Book name="XML">
 <Chapter no="1">
  <Section no="1"/>
  <Section no="2"/>
  <Section no="3"/>
 </Chapter>
 <Chapter no="2">
  <Section no="1"/>
  <Section no="2"/>
  <Section no="3"/>
 </Chapter>
 <Chapter no="3"/>
</Book>

也就是说, 将<Section no="1">   
  </Section> 等元素
和  <Chapter no="3">
  </Chapter>
转换成<Section no="1"/> 和 <Chapter no="3"/>

由于xml文件很大, 所以想给转换一下便于阅读. 我用下面的XSLT转换没有效果, 请高人指点谜津!
<?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:strip-space elements="*"/>
  <xsl:template match="/ | @* | node()" priority="0.5">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
手册上说, 用 strip-space 可以去掉空白, 但是对此毫无效果.


--  作者:W3
--  发布时间:10/30/2006 9:19:00 AM

--  
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <xsl:element name="BOOK">
      <xsl:attribute name="name">XML</xsl:attribute>
      <xsl:for-each select="//Chapter">
        <xsl:element name="Chapter">
          <xsl:attribute name="no">
            <xsl:value-of select="@no"/>
          </xsl:attribute>
          <xsl:for-each select="Section">
            <xsl:element name="Section">
              <xsl:attribute name="no">
                <xsl:value-of select="@no"/>
              </xsl:attribute>
            </xsl:element>
          </xsl:for-each>
        </xsl:element>
      </xsl:for-each>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>
--  作者:xiaoyupan
--  发布时间:10/30/2006 9:52:00 PM

--  
感激不尽! 马上就试一试!
--  作者:xiaoyupan
--  发布时间:11/4/2006 12:23:00 AM

--  
发现了个简单的方法!! 由于文件大而复杂, 下面的方法较为合适和通用, 贴出与大家共享:

<?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()" priority="0.5">
    <xsl:copy>
      <xsl:choose>
        <xsl:when test="count(node())>0">
          <xsl:apply-templates select="@* | node()"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates select="@*"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
109.375ms