输出中的XSLT元素对齐

输出中的XSLT元素对齐,xslt,format,Xslt,Format,试图为别人解决一个问题,却遇到了我自己的奇怪问题,我相信这个问题很简单,但答案却在我眼前 我拥有的XML: <xml> <head> <info> <content> <source attribute1="RSC1985s5c1" attribute2="6(17)"> data1 </source> <cite/>

试图为别人解决一个问题,却遇到了我自己的奇怪问题,我相信这个问题很简单,但答案却在我眼前

我拥有的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>

数据1
(
3e/191
)
数据2
(
4e/54
)
使用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>
//我也注意到了!
//为什么这里没有新线?
数据1
(
3e/191
)
数据2
(
4e/54
)
//还有这里?
使用注释强调的问题是我无法理解的,为什么元素之间没有新线


提前感谢。

对提供的转换稍作修改后

<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>

数据1
(
3e/191
)
数据2
(
4e/54
)

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

对提供的转换稍加修改后

<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>

数据1
(
3e/191
)
数据2
(
4e/54
)

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

工作正常,我的处理器对空元素的缩进方式略有不同,但这是Microsoft为您提供的。如往常一样,您可以快速给出完美答案!工作正常,我的处理器对空元素的缩进方式略有不同,但这是Microsoft为您提供的。如往常一样,您可以快速给出完美答案!