2013-09-30 174 views
1

我知道現在已經有一個關於在XSLT中替換字符串的問題,但我需要一個條件語句來用一個條件語句替換一個字符串中的多個變量。XSLT字符串替換,多個變量

這裏是我的代碼:

<xsl:template name="section-01"> 
    <xsl:call-template name="table-open"/> 
    <xsl:text disable-output-escaping="yes">&lt;table style="text-align=center;"&gt;</xsl:text> 
    <xsl:call-template name="display-gen"> 
    <xsl:with-param name="value" select="./z30-collection"/> 
    <xsl:with-param name="width" select="'30%'"/> 
    </xsl:call-template> 
    <xsl:call-template name="display-gen"> 
    <xsl:with-param name="value" select="./call-no-piece-01"/> 
    <xsl:with-param name="width" select="'30%'"/> 
    </xsl:call-template> 
    <xsl:call-template name="table-close"/> 
</xsl:template> 

我需要一份聲明取代 「./z30-collection」

If ./z30-collection = "Deposit" replace with "DEP" 
if ./z30-collection = "General" replace with "GEN" 
if ./z30-collection = "Storage" replace with "STORE" 

等等

任何幫助將不勝感激!

+0

你可能意味着只是字符串輸出(而不是替換)。或者你想要替換哪一個字符串?從您的描述中不清楚。順便說一句, – DRCB

+0

。檢查''元素。 http://www.w3schools.com/xsl/xsl_choose.asp – DRCB

回答

0

這裏是XSLT的功能,這將工作類似與string.replace()

這個模板有3個參數如下

文本: - 你的主串

替換: - 在要通過替換

字符串: - 字符串將由新的字符串回覆

參考http://exslt.org/str/functions/replace/index.html

1

最「XSLT」方式來處理這樣的事情是爲不同的情況

<xsl:template match="z30-collection[. = 'Deposit']"> 
    <xsl:text>DEP</xsl:text> 
</xsl:template> 
<xsl:template match="z30-collection[. = 'General']"> 
    <xsl:text>GEN</xsl:text> 
</xsl:template> 
<xsl:template match="z30-collection[. = 'Storage']"> 
    <xsl:text>STORE</xsl:text> 
</xsl:template> 
<!-- catch-all for elements that don't have any of the three specific values --> 
<xsl:template match="z30-collection"> 
    <xsl:value-of select="." /> 
</xsl:template> 

,然後當你需要的價值,你做

<xsl:apply-templates select="z30-collection"/> 

和定義不同的模板模板匹配器將自動選出適用於這種特定情況的最具體的模板。沒有必要進行任何明確的有條件檢查,匹配器會爲您處理。