gpt4 book ai didi

xslt - 使用xsl :variable in a xsl:foreach select statement

转载 作者:行者123 更新时间:2023-12-01 17:11:25 25 4
gpt4 key购买 nike

我正在尝试使用 xsl:foreach 迭代 xml 文档,但我需要 select=""是动态的,因此我使用变量作为源。这是我尝试过的:

...

<xsl:template name="SetDataPath">
<xsl:param name="Type" />

<xsl:variable name="Path_1">/Rating/Path1/*</xsl:variable>
<xsl:variable name="Path_2">/Rating/Path2/*</xsl:variable>

<xsl:if test="$Type='1'">
<xsl:value-of select="$Path_1"/>
</xsl:if>

<xsl:if test="$Type='2'">
<xsl:value-of select="$Path_2"/>
</xsl:if>
<xsl:template>

...

    <!-- Set Data Path according to Type -->
<xsl:variable name="DataPath">
<xsl:call-template name="SetDataPath">
<xsl:with-param name="Type" select="/Rating/Type" />
</xsl:call-template>
</xsl:variable>

...

<xsl:for-each select="$DataPath">

...

foreach 抛出错误,指出:“XslTransformException - 要在路径表达式中使用结果树片段,请首先使用 msxsl:node-set() 函数将其转换为节点集。”

当我使用 msxsl:node-set() 函数时,我的结果是空白的。

我知道我将 $DataPath 设置为字符串,但 node-set() 函数不应该从中创建节点集吗?我错过了什么吗?当我不使用变量时:

<xsl:for-each select="/Rating/Path1/*">

我得到了正确的结果。

这是我正在使用的 XML 数据文件:

<Rating>
<Type>1</Type>
<Path1>
<sarah>
<dob>1-3-86</dob>
<user>Sarah</user>
</sarah>
<joe>
<dob>11-12-85</dob>
<user>Joe</user>
</joe>
</Path1>
<Path2>
<jeff>
<dob>11-3-84</dob>
<user>Jeff</user>
</jeff>
<shawn>
<dob>3-5-81</dob>
<user>Shawn</user>
</shawn>
</Path2>
</Rating>

我的问题很简单,如何在 2 个不同的路径上运行 foreach?

最佳答案

试试这个:

   <xsl:for-each select="/Rating[Type='1']/Path1/*
|
/Rating[Type='2']/Path2/*">

关于xslt - 使用xsl :variable in a xsl:foreach select statement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2449011/

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