2010-03-15 47 views
2

對不起極其模糊的問題標題(任何改進建議表示歡迎)XSL模板 - 減少複製

我有一個XSL文檔,目前,有很多複製的,我想減少。

這裏是下面的XML片段,我我目前使用以下XSL基礎上的單位

<xsl:choose> 
    <xsl:when test="@Status = 'alive'"> 
     <img src="/web/resources/graphics/accept.png" /> 
    </xsl:when> 
    <xsl:when test="@Status = 'missingUnit'"> 
     <img src="/web/resources/graphics/error.png" /> 
    </xsl:when> 
    <xsl:when test="@Status = 'missingNode'"> 
     <img src="/web/resources/graphics/exclamation.png" /> 
    </xsl:when> 
    <xsl:when test="@Status = 'unexpectedUnit'"> 
     <img src="/web/resources/graphics/exclamation_blue.png" /> 
    </xsl:when> 
    <xsl:otherwise> 
     <!-- Should never get here --> 
     <img src="/web/resources/graphics/delete.png" /> 
    </xsl:otherwise> 
    </xsl:choose> 

狀態來顯示圖像如何把這個與

<Unit Status="alive"> 

工作代碼放在模板或樣式表中,可以讓我停止複製/粘貼到處?

+0

這個問題是否遺漏了一些xml?目前你的'XML片段'是一行。 – AakashM 2010-03-15 10:52:47

+0

是的,這個XML很大,所以我只粘貼相關的東西,我只是想讓我只寫一次XSL來解析特定的部分,然後在其他地方執行某些操作 – Chris 2010-03-15 10:54:39

+0

使用單獨的查找文檔查看傳統解決方案 - 更清潔高效! – 2010-03-15 13:29:56

回答

5
<xsl:variable name="graphicspath">/web/resources/graphics</xsl:variable> 

    <xsl:template match="/Unit"> 
     <xsl:call-template name="status"> 
     <xsl:with-param name="Status" select="./@Status" /> 
     </xsl:call-template> 
    </xsl:template> 

    <xsl:template name="status"> 
     <xsl:param name="Status" /> 
     <xsl:choose> 
     <xsl:when test="$Status = 'alive'"> 
      <img src="{$graphicspath}/accept.png" /> 
     </xsl:when> 
     <xsl:when test="$Status = 'missingUnit'"> 
      <img src="{$graphicspath}/error.png" /> 
     </xsl:when> 
     <xsl:when test="$Status = 'missingNode'"> 
      <img src="{$graphicspath}/exclamation.png" /> 
     </xsl:when> 
     <xsl:when test="$Status = 'unexpectedUnit'"> 
      <img src="{$graphicspath}/exclamation_blue.png" /> 
     </xsl:when> 
     <xsl:otherwise> 
      <!-- Should never get here --> 
      <img src="{$graphicspath}/delete.png" /> 
     </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 
+0

完美謝謝 – Chris 2010-03-15 11:11:21

2

這是一個 「查找」 的問題的一個典型的例子。一個有效的解決方案是使用一個單獨的查找xml文檔,並使用鍵()/ <xsl:key/>它搜索/索引:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:my="my:way" 
exclude-result-prefixes="my" 
> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 

<xsl:variable name="vStatus" select="*/@Status"/> 

<xsl:key name="kImageForStatus" 
    match="@image" use="../@status"/> 

<my:dict> 
    <when status="alive" image="accept"/> 
    <when status="missingUnit" image="error"/> 
    <when status="missingNode" image="exclamation"/> 
    <when status="unexpectedUnit" image="exclamation_blue"/> 
</my:dict> 

<xsl:variable name="vLookup" 
    select="document('')/*/my:dict[1]"/> 

    <xsl:template match="/"> 
     <xsl:variable name="vImage"> 
      <xsl:for-each select="$vLookup"> 
      <xsl:value-of select="key('kImageForStatus', $vStatus)"/> 
     </xsl:for-each> 
    </xsl:variable> 

     <img src="/web/resources/graphics/{$vImage}.png" /> 
    </xsl:template> 
</xsl:stylesheet> 

當該變換被應用在最初提供的XML文檔:

<Unit Status="alive"/>

有用結果產生

<img src="/web/resources/graphics/accept.png" />