XSL 无效类型错误 - 使用 Value-of 设置变量 [英] XSL Invalid Type Error - Setting Variable using Value-of
问题描述
我正在尝试获取 $deal/total_price 的价值
.在第一个区块中,我能够获得价值并且一切正常.在我使用 value-of
设置名为 deal
的变量的第二个块中,尝试显示 $deal/total_price
时出现错误>.如何使用第二个块中的设置返回 $deal/total_price
?
I am trying to get value-of $deal/total_price
. In the first block, I am able to get value and everything works great. In the second block where I am using value-of
to set the variable named deal
, I get an error when trying to display $deal/total_price
. How can I return $deal/total_price
using setup in second block?
作品:
<xsl:variable name="deal" select="/webpage/results/cars/*[partner_name = $company_name and vehicle_class_description = $vehicle_class_description_full]" />
<xsl:value-of select="$deal/total_price"/>
不起作用:
<xsl:variable name="deal">
<xsl:value-of select="/webpage/results/cars/*[partner_name = $company_name and vehicle_class_description = $vehicle_class_description_full]"/>
</xsl:variable>
<xsl:value-of select="$deal/total_price"/>
我收到以下错误/警告:
Warning: XSLTProcessor::transformToXml(): Invalid type
Warning: XSLTProcessor::transformToXml(): runtime error:
Warning: XSLTProcessor::transformToXml(): XPath evaluation returned no result
推荐答案
With <xsl:variable name="deal" select="/webpage/results/cars/*[partner_name = $company_name and vehicle_class_description =$vehicle_class_description_full]"/>
变量类型是通过评估 select
XPath 表达式来定义的,该表达式返回 XSLT/XPath 1.0 中的节点集.然后您可以在节点集上进行 XPath 导航,例如选择子节点.
With <xsl:variable name="deal" select="/webpage/results/cars/*[partner_name = $company_name and vehicle_class_description = $vehicle_class_description_full]" />
the variable type is defined by evaluating the select
XPath expression which returns a node-set in XSLT/XPath 1.0. You can then do XPath navigation on the node-set, such as selecting child nodes.
与
<xsl:variable name="deal">
<xsl:value-of select="/webpage/results/cars/*[partner_name = $company_name and vehicle_class_description = $vehicle_class_description_full]"/>
</xsl:variable>
变量类型是一个结果树片段,其中包含一个文本节点,该节点具有由内部value-of
选择的第一个节点的字符串值.对于结果树片段类型的变量,您无法进行任何 XPath 导航,您可以使用 value-of
输出其字符串值,或使用 copy-of
输出其树片段.如果您想进行 XPath 导航,那么您首先需要使用 exsl:node-set
或类似方法将结果树片段转换为节点集,但即使您为第二个样本执行此操作将使用 exsl:node-set($deal)
获得一个节点集,其中包含一个包含文本节点的文档节点.因此,如果您想在 XSLT 1.0 中拥有一个包含节点的变量,您需要使用
the variable type is a result tree fragment containing a text node with the string value of the first node selected by the inner value-of
. With a variable of type result tree fragment you can't do any XPath navigation, you can output its string value using value-of
or its tree fragment using copy-of
. If you want to do XPath navigation then you first need to use exsl:node-set
or similar to convert the result tree fragment into a node-set, but even if you do that for your second sample you would get with exsl:node-set($deal)
a node-set with a document node containing a text node. Thus if you want to have a variable containing nodes in XSLT 1.0 you need to use
<xsl:variable name="deal-rtf">
<foo>
<bar>...</bar>
</foo>
</xsl:variable>
<xsl:variable name="deal" select="exsl:node-set($deal-rtf)" xmlns:exsl="http://exslt.org/common"/>
<xsl:value-of select="$deal/foo/bar"/>
某些 XSLT 1.0 处理器(特别是 IE 或 Edge 使用的各种 MSXML 版本以及 .NET 框架中的 XslTransform
)不支持 exsl:node-set
但而是专有命名空间中的类似函数(即 <xsl:variable name="deal" select="ms:node-set($deal-rtf)" xmlns:ms="urn:schemas-microsoft-com:xslt"/>
).
Some XSLT 1.0 processors (notably the various MSXML versions as used by IE or Edge and XslTransform
in the .NET framework) do not support exsl:node-set
but rather a similar function in a proprietary namespace (i.e. <xsl:variable name="deal" select="ms:node-set($deal-rtf)" xmlns:ms="urn:schemas-microsoft-com:xslt"/>
).
在 xsl:variable
内部,你当然可以使用 xsl:choose
,例如
Inside of an xsl:variable
you can of course use xsl:choose
, e.g.
<xsl:variable name="deal-rtf">
<xsl:choose>
<xsl:when test="...">
<xsl:copy-of select="/webpage/results/cars/*[partner_name = $company_name and vehicle_class_description = $vehicle_class_description_full and pay_now = 'Y']"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="webpage/results/cars/*[partner_name = $company_name and vehicle_class_description = $vehicle_class_description_full]"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="deal" select="exsl:node-set($deal-rtf)" xmlns:exsl="http://exslt.org/common"/>
<xsl:value-of select="$deal/total_price"/>
这篇关于XSL 无效类型错误 - 使用 Value-of 设置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!