2012-06-13 43 views
1

試圖解決別人的問題,而不是碰到我自己奇怪的問題,我相信它很簡單,但答案是逃避我!輸出中的XSLT元素對齊

的XML我:

<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> 
    <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兩個內容元素結合起來

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

隨着提供轉化稍微修改

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

我與撒克遜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建議中沒有嚴格的標準,e應該執行精確的縮進。

+0

按照想要的方式工作,我的處理器對空白元素的縮進略有不同,但那是微軟爲您提供的。按照慣例,快速完美地回答你! – Mike

+0

@Mike:不客氣。 –