【发布时间】:2009-01-15 10:56:05
【问题描述】:
我将一组键值对作为参数传递给 XSL(日期 -> “1 月 20 日”,作者 -> “Dominic Rodger”,...)。
这些在我正在解析的一些 XML 中被引用 - XML 看起来像这样:
<element datasource="date" />
目前,我不知道如何将 1 月 20 日排除在外,除非有一个可怕的 <xsl:choose> 声明:
<xsl:template match="element">
<xsl:choose>
<xsl:when test="@datasource = 'author'">
<xsl:value-of select="$author" />
</xsl:when>
<xsl:when test="@datasource = 'date'">
<xsl:value-of select="$date" />
</xsl:when>
...
</xsl:choose>
</xsl:template>
我想使用类似的东西:
<xsl:template match="element">
<xsl:value-of select="${@datasource}" />
</xsl:template>
但我怀疑这是不可能的。我开始使用外部函数调用,但希望避免在我的 XSL 中枚举所有可能的映射键。有什么想法吗?
谢谢,
Dom
【问题讨论】:
-
请注意,在提供的三个答案中,有两个是不正确的——XPath 中没有字符串的“+”运算符,而且,不能为“指定 AVT” XSLT 中的“选择”属性。我的解决方案已经过测试并且有效。