2013-10-24 47 views
0

我想優化我的XSLT文件以獲得更好的可讀代碼並避免重複。根據條件添加具有不同屬性的節點

在一個部分中,根據條件,我有一個新元素必須添加到DOM(目標是HTML),並具有不同的屬性。

例如:

<xsl:choose> 
    <xsl:when test="status = 0 or type = 2"> 
     <img id="img_{$var1}_check" height="14" width="13" src="{$var1}.png" class="{$var1}" onclick="check({$var1})" alt="{$var1}" title="{$var1}"/> 
    </xsl:when> 
    <xsl:when test="status = 1 or type = 24"> 
     <img id="img_{$var2}_check" height="14" width="13" src="{$var2}.png" class="{$var2}" onclick="check({$var2})" alt="{$var2}" title="{$var2}"/> 
    </xsl:when> 
    <xsl:when test="status = 2 or type = 4"> 
     <img id="img_{$var3}_check" height="14" width="13" src="{$var3}.png" class="{$var3}" onclick="check({$var3})" alt="{$var3}" title="{$var3}"/> 
    </xsl:when> 
    <xsl:when test="status = 4 or type = 22"> 
     <img id="img_{$var4}_check" height="14" width="13" src="{$var4}.png" class="{$var4}" onclick="check({$var4})" alt="{$var4}" title="{$var4}"/> 
    </xsl:when> 
</xsl:choose> 

有沒有辦法不寫在每一個「何時」整個img元素?

問候

+0

你能展示更多的XSLT嗎? '$ var1'到'$ var4'從哪裏來? 「」以什麼方式叫? – Tomalak

+0

您可以使用xsl:call-template或xsl:attribute-set –

回答

0

你可以做的,是把你的xsl:選擇一個變量中,並改變只選擇返回或者$ VAR1,$ VAR2,$ VAR3或$ VAR4。然後,使用這個新變量的值

<xsl:variable name="imgvar"> 
    <xsl:choose> 
     <xsl:when test="status = 0 or type = 2"> 
      <xsl:value-of select="$var1" /> 
     </xsl:when> 
     <xsl:when test="status = 1 or type = 24"> 
      <xsl:value-of select="$var2" /> 
     </xsl:when> 
     <xsl:when test="status = 2 or type = 4"> 
      <xsl:value-of select="$var3" /> 
     </xsl:when> 
     <xsl:when test="status = 4 or type = 22"> 
      <xsl:value-of select="$var4" /> 
     </xsl:when> 
    </xsl:choose> 
</xsl:variable> 
<img id="img_{$imgvar}_check" height="14" width="13" src="{$imgvar}.png" class="{$imgvar}" onclick="check({$imgvar})" alt="{$imgvar}" title="{$imgvar}"/> 

我強烈懷疑這是不是你想要什麼,雖然,因爲它看起來不正確使用相同的變量在所有的寫出來的IMG元素屬性在你的元素中。

另一種方法是使用一個命名模板,只是稱這個從XSL:選擇

<xsl:template name="img"> 
    <xsl:param name="imgvar"> 
    <img id="img_{$imgvar}_check" height="14" width="13" src="{$imgvar}.png" class="{$imgvar}" onclick="check({$imgvar})" alt="{$imgvar}" title="{$imgvar}"/> 
</xsl:template> 

<xsl:variable name="imgvar"> 
    <xsl:choose> 
     <xsl:when test="status = 0 or type = 2"> 
      <xsl:call-template name="img"> 
       <xsl:with-param name="imgvar" select="$var1" /> 
      </xsl:call-template> 
     </xsl:when> 
     <xsl:when test="status = 1 or type = 24"> 
      <xsl:call-template name="img"> 
       <xsl:with-param name="imgvar" select="$var2" /> 
      </xsl:call-template> 
     </xsl:when> 
     <xsl:when test="status = 2 or type = 4"> 
      <xsl:call-template name="img"> 
       <xsl:with-param name="imgvar" select="$var3" /> 
      </xsl:call-template> 
     </xsl:when> 
     <xsl:when test="status = 4 or type = 22"> 
      <xsl:call-template name="img"> 
       <xsl:with-param name="imgvar" select="$var4" /> 
      </xsl:call-template> 
     </xsl:when> 
    </xsl:choose> 
</xsl:variable> 

這可以那麼很明顯延長,如果需要使用額外的參數。

另一種方法,就是把xsl:choose放在元素裏面,併爲每個選項寫出不同的屬性。

<img height="14" width="13"> 
     <xsl:choose> 
      <xsl:when test="status = 0 or type = 2"> 
       <xsl:attribute name="id">img_<xsl:value-of select="$var1" /></xsl:attribute> 
       <xsl:attribute name="src"><xsl:value-of select="$var1" />.png</xsl:attribute> 
      </xsl:when> 
      <xsl:when test="status = 1 or type = 24"> 
       <xsl:attribute name="id">img_<xsl:value-of select="$var2" /></xsl:attribute> 
       <xsl:attribute name="src"><xsl:value-of select="$var2" />.png</xsl:attribute> 
      </xsl:when> 
      <xsl:when test="status = 2 or type = 4"> 
       <xsl:attribute name="id">img_<xsl:value-of select="$var3" /></xsl:attribute> 
       <xsl:attribute name="src"><xsl:value-of select="$var3" />.png</xsl:attribute> 
      </xsl:when> 
      <xsl:when test="status = 4 or type = 22"> 
       <xsl:attribute name="id">img_<xsl:value-of select="$var4" /></xsl:attribute> 
       <xsl:attribute name="src"><xsl:value-of select="$var4" />.png</xsl:attribute> 
      </xsl:when> 
     </xsl:choose> 
    </img> 

我會說使用的xsl:調用模板是最好最好的位置。

+0

我使用模板解決方案解決了這個問題。謝謝你的幫助 – ylerjen

相關問題