表达式必须求值为节点集是什么意思?

huangapple go评论98阅读模式
英文:

What does expression must evaluate to a node set mean?

问题

我有以下的代码。

 <!-- 错误函数 -->
 <xsl:template name="myFunc">
    <xsl:param name="person"/>
    <xsl:variable name="name">
       "name": "<xsl:value-of select="$person/name"/>",
    </xsl:variable>
    <xsl:value-of select="$name"/>
 </xsl:template>

 <!-- 调用函数 -->
 <xsl:template match="/">
    <xsl:call-template name="myFunc">
        <xsl:with-param name="person" select="list/person"/>
    </xsl:call-template>
 </xsl:template>

我正在使用这个变量来处理更复杂的内容,但到目前为止,我甚至不能让这一小部分工作。是否有任何原因?我一直收到以下错误消息:

System.Xml.Xsl.XslTransformException
  HResult=0x80131942
  Message=Expression must evaluate to a node-set.
  Source=System.Private.Xml
  StackTrace:
   at System.Xml.Xsl.Runtime.XsltConvert.EnsureNodeSet(IList`1 listItems)
   at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlSequenceWriter results)
   at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter writer)
   at System.Xml.Xsl.XslCompiledTransform.Transform(IXPathNavigable input, XsltArgumentList arguments, XmlWriter results, XmlResolver documentResolver)
   at System.Xml.Xsl.XslCompiledTransform.Transform(IXPathNavigable input, XsltArgumentList arguments, XmlWriter results)
   at WZDxConversion.Controllers.ValuesController.Post() in --------------------------------------------------------
   at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
   at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.SyncObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionMethodAsync>d__12.MoveNext()
英文:

I have the following code.

 <!-- Error func -->
 <xsl:template name="myFunc">
    <xsl:param name="person"/>
    <xsl:variable name="name">
       "name": "<xsl:value-of select="$person/name"/>",
    </xsl:variable>
    <xsl:value-of select="$name"/>
 </xsl:template>

 <!-- Call function -->
 <xsl:template match="/">
    <xsl:call-template name="myFunc">
        <xsl:with-param name="person" select="list/person"/>
    </xsl:call-template>
 </xsl:template>

I'm using the variable for something more complicated, but so far I can't even get this little bit to work. Is there any reason why? I keep getting the following error

System.Xml.Xsl.XslTransformException
  HResult=0x80131942
  Message=Expression must evaluate to a node-set.
  Source=System.Private.Xml
  StackTrace:
   at System.Xml.Xsl.Runtime.XsltConvert.EnsureNodeSet(IList`1 listItems)
   at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlSequenceWriter results)
   at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter writer)
   at System.Xml.Xsl.XslCompiledTransform.Transform(IXPathNavigable input, XsltArgumentList arguments, XmlWriter results, XmlResolver documentResolver)
   at System.Xml.Xsl.XslCompiledTransform.Transform(IXPathNavigable input, XsltArgumentList arguments, XmlWriter results)
   at WZDxConversion.Controllers.ValuesController.Post() in --------------------------------------------------------
   at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
   at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.SyncObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionMethodAsync>d__12.MoveNext()

答案1

得分: 2

我不确定为什么你在这段代码片段中出现错误,但我可以告诉你错误消息的含义。

当你使用类似 $X/Y 的路径表达式时,左侧的表达式必须评估为一个节点集,这是XSLT 1.0和XPath 1.0中的少数基本数据类型之一。(还有一些其他地方的值必须是节点集,但"/"操作符的左侧是最常见的。)

当你使用没有 select 属性的 xsl:variable 时,例如

<xsl:variable name="v">
  <a><b/></a>
</xsl:variable>

那么 v 的值不是一个节点集,而是一个 "结果树片段",这意味着 $v 不能在 "/" 操作符的左侧使用。

这在XSLT 1.0中是一个严重的设计错误。James Clark 在这里解释了其理由:https://markmail.org/message/uafk4pxqawkjuaid

实际上,XSL WG 期望XSLT 1.0会很快被XSLT 1.1取代,解除了这种限制;他们从未预料到会有人在20多年后仍然使用XSLT 1.0。这个限制当然在XSLT 2.0中解除了,但Microsoft从未实现过这个版本。

英文:

I'm not sure why you're getting the error with this code fragment, but I can tell you want the error message means.

When you use a path expression such as $X/Y, the expression on the left-hand-side of the "/" operator must evaluate to a node-set, which is one of the small number of basic data types in XSLT 1.0 and XPath 1.0. (There are a few other places where the value must be a node-set, but the lhs of "/" is the most common.)

When you use xsl:variable with no select attribute, for example

&lt;xsl:variable name=&quot;v&quot;&gt;
  &lt;a&gt;&lt;b/&gt;&lt;/a&gt;
&lt;/xsl:variable&gt;

then the value of v is not a node-set, but rather a "result tree fragment", and this means that $v cannot be used on the lhs of a "/" operator.

This was a serious design error in XSLT 1.0. James Clark explained the rationale here: https://markmail.org/message/uafk4pxqawkjuaid

What it amounts to is that the XSL WG expected that XSLT 1.0 would be quickly followed by an XSLT 1.1 that lifted such restrictions; they never expected that anyone would still be using XSLT 1.0 more than 20 years later. The restriction was of course lifted in XSLT 2.0, but Microsoft never implemented that.

huangapple
  • 本文由 发表于 2020年1月4日 00:48:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582288.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定