我有以下代码:
<xsl:variable name="attribute-name">
<xsl:choose>
<xsl:when test="$var1 = 1">attr1</xsl:when>
<xsl:when test="$var2 = 1">attr2</xsl:when>
<xsl:when test="$var3 = 1">attr3</xsl:when>
<xsl:otherwise>attr</xsl:otherwise>
</xsl:choose>
</xsl:variable>
将用于在后面的代码中设置属性:
<tag>
<xsl:attribute name="{$attribute-name}">Something</xsl:attribute>
</tag>
我必须优化代码,因为它确实会减慢我的转换过程。我看过像select="if ()..."
这样的结构,但这只能在简单的if-then-else情况下使用。上面的代码可以被其他xsl:when
扩展,因此可维护的解决方案会很好。