根据条件添加具有不同属性的节点

时间:2013-10-24 08:26:55

标签: xml xslt

我试图优化我的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>

有没有办法不在每个&#34;中写出整个img元素。 ?

此致

1 个答案:

答案 0 :(得分:0)

你能做什么,是把你的xsl:在变量里面选择,然后改变选择只返回$ var1,$ var2,$ var3或$ var4。然后,使用此新变量的值

写出 img 元素
<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:choose

中调用它
<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:select放在 img 元素中,并为每个选择写出不同的属性。

    <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:call-template 是最好的。