2010-03-15 86 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" />