2013-04-25 27 views
2

我有許多xsl:值爲的XSLT代碼調用。我需要修整所有值中的空格。
在每次調用時都寫非常繁瑣的normalize-space()。
我用的模板:XSLT - 如何修剪xsl中的所有值:表達式值

<xsl:template match="text()"> 
    <xsl:value-of select="normalize-space(.)"/> 
</xsl:template> 

但它變得沒有影響。
謝謝!
對不起,我的英語。

回答

2

更新:我認爲答案表單@邁克爾凱最有可能是你在找什麼。

  • strip-space elements="*"只刪除節點用空格ONY文本
  • 號樓中間節點集是沒有必要的,如果你只打算也得到了值,而無需空格。
  • 如果您必須測試(<xsl:if test=)物品的物品並且希望避免在測試條件中有normalize-space,那麼或許只有一個原因使用下面的「中間節點集合」解決方案。

下面

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

原來的答覆應該有所幫助。 (就在您的xlst的頂層)

更新(下一次嘗試;-)) 您可以使用exsl:node-set構建沒有空白的中間節點集。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:exsl="http://exslt.org/common" 
      extension-element-prefixes="exsl"> 

    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> 
    <xsl:strip-space elements="*" /> 

    <xsl:template match="/"> 
     <xsl:variable name="intermediate"> 
      <xsl:apply-templates mode="ws_remove"/> 
     </xsl:variable> 
     <xsl:apply-templates select="exsl:node-set($intermediate)/*"/> 

    </xsl:template> 

    <xsl:template match="@*|node()" mode="ws_remove"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()" mode="ws_remove"/> 
     </xsl:copy> 

    </xsl:template> 
     <xsl:template match="text()" mode="ws_remove" > 
      <xsl:value-of select="normalize-space(.)"/> 
     </xsl:template> 

    <xsl:template match ="root"> 
     <test> 
      <xsl:value-of select="test"/> 
     </test> 
    </xsl:template> 
</xsl:stylesheet> 

有了這個輸入

<root> 
    <test> adfd das </test> 
</root> 

生成的輸出:

<test>adfd das</test> 
+0

它不起作用( – 2013-04-25 16:18:32

+0

對不起,錯誤的答案'strip-space elements =「*」'只刪除帶有空格的文本節點 – 2013-04-25 16:32:35

2

把正規化空間在文本節點的模板規則()調用不會因爲XSL工作: value-of不適用模板規則。如果您將<xsl:value-of select="."/>更改爲<xsl:apply-templates/>(隨處可得),那麼它將起作用。