2010-07-21 60 views
2

生成的HTML頁面包含有時在我的頁面中不存在的引用鏈接。我應該將這個鏈接顯示爲一個簡單的標籤。 目前做到這一點:XSLT:如何在不復制代碼的情況下創建條件鏈接

<xsl:choose> 
<xsl:when test="$nb_action != 0"> 
    <a href="#action">Action (offensive, defensive, reconnaissance)</a> 
</xsl:when> 
<xsl:otherwise> 
    Action (offensive, defensive, reconnaissance) 
</xsl:otherwise> 
</xsl:choose> 

我不知道如何來簡化我的代碼,如何停用節點<a></a>

我的第一個想法是委託一個特殊的CSS類:

<a href="#action"> 
<xsl:if test="$nb_action = 0"> 
    <xsl:attribute name="class">inactive</xsl:attribute> 
</xsl:if>Action (offensive, defensive, reconnaissance) 
</a> 

但它仍然是一個鏈接...

遵循一種解決方法:

<a><xsl:if test="$nb_action != 0"> 
    <xsl:attribute name="href">#action</xsl:attribute> 
</xsl:if>Action (offensive, defensive, reconnaissance)</a> 

這是正確的寫一個html <a>標籤沒有href

回答

2

您可以將該操作文本作爲變量。儘管這個變量仍然出現在3個地方。

<xsl:variable name="textval"> 
Action (offensive, defensive, reconnaissance)</xsl:variable> 
<xsl:choose> 
<xsl:when test="0"> 
    <a href="#action"><xsl:copy-of select="$textval"/></a> 
</xsl:when> 
<xsl:otherwise> 
    <xsl:copy-of select="$textval"/> 
</xsl:otherwise> 
</xsl:choose> 

編輯:另外,如果你不介意額外的,無用的span標籤,你可以使用

<xsl:variable name="condition" select="---condition-here---"/> 

    <xsl:variable name="tagname"> 
    <xsl:choose> 
    <xsl:when test="$condition">a</xsl:when> 
    <xsl:otherwise>span</xsl:otherwise> 
    </xsl:choose> 
    </xsl:variable> 

    <xsl:element name="{$tagname}"> 
    <xsl:if test="$condition"> 
    <xsl:attribute name="href">#action</xsl:attribute> 
    </xsl:if> 
    Action (offensive, defensive, reconnaissance) 
    </xsl:element> 

(在Firefox中,如果我們設置$tagname爲空字符串,那麼該元素根本不會被應用,但處理器也可能是raise an error,所以不要依賴於它。)

+0

我喜歡你的解決方案,謝謝:)) – chepseskaf 2010-07-21 16:08:36

+0

一個小問題:爲了將$ textval聲明爲字符串數據類型,使用'select =''動作(冒犯,防禦,偵察)「」'。否則它將被定義爲結果樹片段。 – 2010-07-21 19:49:47

+0

@Alejandro:其實我相信樹片段是OP想要的,我應該使用'copy-of'而不是'value-of'。更新。 – kennytm 2010-07-21 20:01:29

0

什麼不存儲字符串操作(..)到一個變量,它在代碼中?

+0

我懷疑這個提議,但是因爲可以修改一個屬性,所以我thoug ht有可能修改該項目... – chepseskaf 2010-07-21 15:14:15

相關問題