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を使用して、2つのコンテンツ要素を組み合わせます。

<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 に答える