【发布时间】:2018-08-14 01:00:46
【问题描述】:
我正在尝试通过 XPath 评估检查 XML 文档中的两条规则。
规则是:
- /root/path1/text()='TABLE1'
- /root/path2/text()='TABLE2'
我的代码如下:
XPathDocument document = new XPathDocument(myDocument);
XPathNavigator navigator = document.CreateNavigator();
XmlNamespaceManager xpathNsMgr = new XmlNamespaceManager(navigator.NameTable);
xpathNsMgr.AddNamespace("xsl", "http://www.w3.org/1999/XSL/Transform");
var result = (bool)navigator.Evaluate("((/root/path1/text()='TABLE1') and (/root/path2/text()='TABLE2'))", xpathNsMgr);
如果我通过评估自己的 XPath 检查两个 XPath,那么一切正常。但是如果我检查它们的组合,如上面的代码所示,我会收到以下错误:
xsltcontext is needed for this query because of an unknown function
为什么不能将 XPath 结合起来并一起评估它们?自 XPath 1.0 以来,我认为“and”、“or”等都是有效的运算符...
【问题讨论】:
-
我认为和或用于评估每个element的属性? /root 可能需要在外括号之外。
-
@lloyd 这个 XPath 1.0 是特定的吗?因为上面显示的表达式完美地工作在:freeformatter.com/xpath-tester.html#ad-output
-
您确定您收到发布的 XPath 表达式的错误吗?我在那里看不到任何未知的功能。您对 Tomalak 的回答的评论显示了函数
exists的使用,该函数确实未在 XPath 1 中定义。 -
Oh...exists() 确实是 XPath 版本,但是 XPath 2.0...w3schools.com/xml/xsl_functions.asp 所以,我必须搜索另一个库来评估这些表达式,因为它们是通过外部系统/合作伙伴。