1

试图为其他人解决问题,却遇到了我自己的奇怪问题,我确信这很简单,但答案却让我难以捉摸!

我拥有的 XML:

<xml>
  <head>
    <info>
      <content>
        <source attribute1="RSC1985s5c1" attribute2="6(17)">
          data1
        </source>
        <cite/>
        <case/>
        (
        <target attribute1="LRC1985s5c1" attribute2="6(17)1">
          3e/191
        </target>
        )
      </content>
      <content>
        <source attribute1="RSC1985s5c1" attribute2="6(17)">
          data2
        </source>
        <cite/>
        <case/>
        (
        <target attribute1="LRC1985s5c4" attribute2="6(17)1">
          4e/54
        </target>
        )
      </content>
    </info>
  </head>
</xml>

用 XSLT 来组合两个内容元素:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates select="node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="info">
    <xsl:copy>
      <xsl:element name="content">
        <xsl:for-each select="content">
          <xsl:apply-templates select="@*"/>
          <xsl:apply-templates select="node()"/>
        </xsl:for-each>
      </xsl:element>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

这将创建:

<?xml version="1.0" encoding="utf-8"?><xml> //Just noticed here too!
  <head>
    <info><content> //Why no new line here?
        <source attribute1="RSC1985s5c1" attribute2="6(17)">
          data1
        </source>
        <cite />
        <case />
        (
        <target attribute1="LRC1985s5c1" attribute2="6(17)1">
          3e/191
        </target>
        )

        <source attribute1="RSC1985s5c1" attribute2="6(17)">
          data2
        </source>
        <cite />
        <case />
        (
        <target attribute1="LRC1985s5c4" attribute2="6(17)1">
          4e/54
        </target>
        )
      </content></info> //And again here?
  </head>
</xml>

使用评论突出显示的问题是我无法弄清楚的,为什么元素之间没有新线。

提前致谢。

4

1 回答 1

1

对提供的转换稍作修改

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

  <xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates select="node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="info">
    <xsl:copy>
      <xsl:element name="content">
        <xsl:for-each select="content">
          <xsl:apply-templates select="@*"/>
          <xsl:apply-templates select="node()"/>
        </xsl:for-each>
      </xsl:element>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

我得到了 Saxon 6.5.5 和许多其他 XSLT 1.0 处理器的良好缩进输出:

<xml>
   <head>
      <info>
         <content>
            <source attribute1="RSC1985s5c1" attribute2="6(17)">
          data1
        </source>
            <cite/>
            <case/>
        (
        <target attribute1="LRC1985s5c1" attribute2="6(17)1">
          3e/191
        </target>
        )
      <source attribute1="RSC1985s5c1" attribute2="6(17)">
          data2
        </source>
            <cite/>
            <case/>
        (
        <target attribute1="LRC1985s5c4" attribute2="6(17)1">
          4e/54
        </target>
        )
      </content>
      </info>
   </head>
</xml>

一些 XSLT 处理器可能仍然会产生不需要的缩进——在 W3C XSLT 1.0 建议中没有严格的标准应该如何执行缩进。

于 2012-06-13T12:48:21.870 回答