2011-07-02 23 views
4

我有一個XML代碼,可以ahve兩種形式:如何僅用XSLT去掉回車符?

表1

<?xml version="1.0"> 
<info> 
</info> 

表2

<?xml version="1.0"> 
<info> 
    <a href="http://server.com/foo">bar</a> 
    <a href="http://server.com/foo">bar</a> 
</info> 

從環路I讀xml和通的每個形式它到一個xslt樣式表。

XSLT代碼

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

    <xsl:template match="*|@*|text()"> 
     <xsl:apply-templates select="/info/a"/> 
    </xsl:template> 

    <xsl:template match="a"> 
     <xsl:value-of select="concat(text(), ' ', @href)"/> 
     <xsl:text>&#13;</xsl:text> 
    </xsl:template> 
</xsl:stylesheet> 

我獲得此:

 

bar http://server.com/foo 
bar http://server.com/foo 

如何刪除第一個空行與XSLT只

回答

1

從環路I讀取XML的每種形式並將它傳遞給XSLT樣式表。

可能來自您的應用程序在空表單(表單1)上執行樣式表會導致此問題。嘗試僅通過執行樣式表來處理這種情況,而不管表單是否爲空。

而且你可能想改變你的樣式表到這一個:

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

    <xsl:output method="text"/> 
    <xsl:strip-space elements="*" /> 

    <xsl:template match="info/a"> 
     <xsl:value-of select="concat(normalize-space(.), 
      ' ', 
      normalize-space(@href))"/> 
      <xsl:if test="follwing-sibling::a"> 
      <xsl:text>&#xA;</xsl:text> 
      </xsl:if> 
    </xsl:template> 

</xsl:stylesheet> 

normalize-space()被用來確保輸入數據有沒有多餘的空間。

+0

你是對的;) – Stephan

+0

不客氣 –

1

它可能取決於您使用的XSL處理器,但是您是否嘗試了以下方法?

<xsl:output method="text" indent="no" /> 
+0

我會試一試並告訴你。我已經找到了一個解決方案,現在從這裏的其他答案啓發。 Thx爲您提供幫助。 – Stephan

1

你想用你想要的文本輸出方法,只處理節點和之後的最後不輸出新行(或之前,首先在下面的解決方案)

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

    <xsl:output method="text"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="info"> 
    <xsl:apply-templates select="a"/>  
    </xsl:template> 

    <xsl:template match="a"> 
    <xsl:if test="not(position() = 1)" xml:space="preserve">&#xA;</xsl:if> 
    <xsl:value-of select="concat(text(), ' ', @href)"/> 
    </xsl:template> 

您需要xml:spacexsl:text指令中,以便在讀取樣式表時不會將空格標準化。

+0

thx爲您的anwser。 – Stephan