[英]Usage for xsl param in xpath
I defined a param array $gen
as:我将参数数组$gen
定义为:
<xsl:variable name="inline-array">
<item>western</item>
<item>Romance</item>
<item>Adventure</item>
<item>Drama</item>
<item>Comedy</item>
<item>Horror</item>
<item>Action</item>
</xsl:variable>
<xsl:param name="gen" select="document('')/*/xsl:variable[@name='inline-array']/*"/>
I want to transfer a XML to VXML by XSLT.我想通过 XSLT 将 XML 传输到 VXML。 Param $gen
and $gen2
failed to select the value when I used it in Xpath当我在 Xpath 中使用它时,参数$gen
和$gen2
未能选择该值
<xsl:value-of select>:
<filled namelist="MovieSummary">
<if cond="MovieSummary == '{$gen[1]}'">
<prompt>
<xsl:value-of select ="//genre[.='western']/../title"/>. <xsl:value-of select ="//genre[.='western']/../summary"/>
</prompt>
<xsl:for-each select="$gen[position()>1]">
<xsl:variable name="gen2"><xsl:value-of select="."/></xsl:variable>
<elseif cond="MovieSummary == '{$gen2}'"/>
<prompt>
<xsl:value-of select ="//genre[.='$gen2']/../title"/>. <xsl:value-of select ="//genre[.='$gen2']/../summary"/>
</prompt>
</xsl:for-each>
<else/>
</if>
</filled>
It failed to select the value when I use $gen2
instead of "Romance" or other string.当我使用$gen2
而不是“Romance”或其他字符串时,它无法选择该值。 But cond="MovieSummary == '{$gen[1]}'"
works well.但是cond="MovieSummary == '{$gen[1]}'"
效果很好。 It can only generate something like:它只能生成如下内容:
<if cond="MovieSummary == 'western'">`enter code here`
<prompt>Range Feud.
Clint Turner is arrested for the murder of his girlfriend Judy's
father, a rival rancher who was an enemy of his own father.
</prompt>
<elseif cond="MovieSummary == 'Romance'"/>
<prompt>. </prompt>
<else/>
</if>
I've tried:我试过了:
select ="//genre[.=$gen2]/../summary";
select ="//genre[.={$gen2}]/../summary";
select ="//genre[.='{$gen2}']/../summary"
All failed.都失败了。
The instruction:指令:
<xsl:for-each select="$gen[position()>1]">
puts you in the context of $gen
.将您置于$gen
的上下文中。 From this context, the expression:从这个上下文,表达式:
<xsl:value-of select ="//genre[.='$gen2']/../title"/>
selects nothing, because //
starts from the root of the current document - but genre
is in another document altogeher.什么都不选择,因为//
从当前文档的根开始 - 但genre
完全在另一个文档中。
You need to change the context back to the processed XML document in order to address the nodes within it.您需要将上下文更改回已处理的 XML 文档,以便对其中的节点进行寻址。
Note: in XSLT 2.0, the key()
function can select nodes in another document directly, without changing the overall context.注意:在 XSLT 2.0 中, key()
函数可以直接选择另一个文档中的节点,而无需更改整体上下文。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.