gpt4 book ai didi

xslt - XSLT 中的动态变量

转载 作者:行者123 更新时间:2023-12-04 16:23:30 28 4
gpt4 key购买 nike

我将一堆键值对作为参数传递给 XSL(日期 ->“1 月 20 日”,作者 ->“Dominic Rodger”,...)。

我正在解析的一些 XML 中引用了这些 - XML 如下所示:

<element datasource="date" />

目前,除了可怕的 <xsl:choose> 之外,我无法弄清楚如何从这些中获得 1 月 20 日。陈述:
<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 中枚举所有可能的映射键。有任何想法吗?

谢谢,

多姆

最佳答案

这是一种可能的解决方案 ,但是我建议将所有参数分组到一个单独的 XML 文件中,并使用 document() 访问它们。功能:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
exclude-result-prefixes="ext"
>
<xsl:output method="text"/>

<xsl:param name="date" select="'01-15-2009'"/>
<xsl:param name="author" select="'Dominic Rodger'"/>
<xsl:param name="place" select="'Hawaii'"/>
<xsl:param name="time" select="'midnight'"/>

<xsl:variable name="vrtfParams">
<date><xsl:value-of select="$date"/></date>
<author><xsl:value-of select="$author"/></author>
<place><xsl:value-of select="$place"/></place>
<time><xsl:value-of select="$time"/></time>
</xsl:variable>

<xsl:variable name="vParams" select="ext:node-set($vrtfParams)"/>

<xsl:template match="element">
<xsl:value-of select=
"concat('&#xA;', @datasource, ' = ',
$vParams/*[name() = current()/@datasource]
)"
/>
</xsl:template>
</xsl:stylesheet>

当此转换应用于以下 XML 文档时 :
<data>
<element datasource="date" />
<element datasource="place" />
</data>

产生了正确的结果 :

日期 = 01-15-2009

地方 = 夏威夷

请注意使用 xxx:node-set() 函数(此处使用 the EXSLT one)将 RTF( Result Tree Fragment )转换为常规 xml 文档(临时树)。

关于xslt - XSLT 中的动态变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/446354/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com