2014-02-05 34 views
0

愚蠢,簡單的問題。當我輸出文本時,它仍然基於我的格式化/縮進XSL結構獲取標籤。我如何指示變形器忽略樣式表中的間距,同時仍保持整齊格式化?XSLT去除文本輸出中的所有選項卡

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

     <xsl:template match="/"> 
     <xsl:apply-templates select="Foo/Bar"></xsl:apply-templates> 
     </xsl:template> 
     <xsl:template match="Bar"> 
<xsl:for-each select="AAA"><xsl:for-each select="BBB"><xsl:value-of select="Label"/>|<xsl:value-of select="Value"/><xsl:text>&#10;</xsl:text></xsl:for-each></xsl:for-each> 
</xsl:template> 

     </xsl:stylesheet> 

由生產線生產輸出線無標籤:

SomeLabel|SomeValue 
SomeLabel|SomeValue 
SomeLabel|SomeValue 


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

    <xsl:template match="/"> 
    <xsl:apply-templates select="Foo/Bar"></xsl:apply-templates> 
    </xsl:template> 
    <xsl:template match="Bar"> 
    <xsl:for-each select="AAA"> 
     <xsl:for-each select="BBB"> 
      <xsl:value-of select="Label"/>|<xsl:value-of select="Value"/> 
      <xsl:text>&#10;</xsl:text> 
     </xsl:for-each> 
    </xsl:for-each> 

    </xsl:template> 

    </xsl:stylesheet> 

生成輸出與標籤:

SomeLabel|SomeValue 
    SomeLabel|SomeValue 
    SomeLabel|SomeValue 

更新: 添加此不能解決問題:

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

這是人爲的,但你能想象的XML看起來是這樣的:

<Foo> 
    <Bar> 
    <AAA> 
     <BBB> 
     <Label>SomeLabel1</Label> 
     <Value>SomeValue1</Value> 
     </BBB> 
     <BBB>  
     <Label>SomeLabel2</Label> 
     <Value>SomeValue2</Value> 
     </BBB> 
     <BBB> 
     <Label>SomeLabel3</Label> 
     <Value>SomeValue3</Value> 
     </BBB> 
    </AAA> 
    </Bar> 
</Foo> 
+0

你已經張貼不會產生結果的XSLT代碼你聲稱:http://xsltransform.net/pPgCcop –

+0

@ michael.hor257k已更新。縮進在xsltransform上是正確的。淨,但我在visual studio和記事本++中運行這個,並且當XSLT縮進時,插入到輸出中的標籤。 – kakridge

回答

1

什麼,你可以嘗試是在包裝XSL 當前的所有文本節點:文本。例如,試試這個

<xsl:for-each select="BBB"> 
    <xsl:value-of select="Label"/> 
    <xsl:text>|</xsl:text> 
    <xsl:value-of select="Value"/> 
    <xsl:text>|</xsl:text> 
    </xsl:for-each> 

或者,你可以利用CONCAT功能。

<xsl:for-each select="BBB"> 
    <xsl:value-of select="concat(Label, '|')"/> 
    <xsl:value-of select="concat(Value, '|')"/> 
    </xsl:for-each> 

你可以在兩個語句,甚至合併成一個,如果你想

<xsl:for-each select="BBB"> 
    <xsl:value-of select="concat(Label, '|', Value, '|')"/> 
    </xsl:for-each> 

編輯:如果你不喜歡進入分離器|這麼多次,你利用模板匹配的輸出的Fileds。首先,應用模板更換價值的像這樣

<xsl:for-each select="BBB"> 
     <xsl:apply-templates select="Label"/> 
     <xsl:apply-templates select="Value"/> 
     <xsl:text>&#10;</xsl:text> 
    </xsl:for-each> 

那麼你將有一個特定的模板匹配標籤,在這裏你不會需要輸出的分隔符​​,另一(需要BBB

<xsl:template match="BBB/Label" priority="1"> 
    <xsl:value-of select="." /> 
</xsl:template> 

<xsl:template match="BBB/*"> 
    <xsl:text>|</xsl:text><xsl:value-of select="." /> 
</xsl:template> 

匹配任何孩子更通用的模板優先這裏,以確保標籤與第一個模板匹配,而不是通用模板)。當然,你也可以不這樣做apply-templates on 標籤在這種情況下,只是做xsl:值爲那個。

此外,如果場均是在它們出現在XML的順序輸出,可以簡化的for-each只是這個

<xsl:for-each select="BBB"> 
     <xsl:apply-templates /> 
     <xsl:text>&#10;</xsl:text> 
    </xsl:for-each> 
+0

這有效,我認爲這是一個部分解決方案。如果我有兩個領域,效果很好,但實際上我必須連接大約50個領域。如果沒有將concat應用於所有需要的東西,是不是沒有一種方法可以普遍適用? – kakridge

+0

此外,每個標籤之前仍包含一個標籤。 – kakridge

+0

@kakridge「*在現實中,我將不得不連接大約50個字段。*」我們只能通過您向我們展示的內容去完成。在你的代碼中,你明確地調用每個「字段」,所以爲每個這樣的調用添加一個文本元素不是很大的努力。或者,您可以構建for-each「字段」循環,並將文本元素插入到其中,一次。 –

相關問題