Xml 与current()等价的XPath

Xml 与current()等价的XPath,xml,xpath,Xml,Xpath,如何引用从XPath中计算XPath的节点 例如: 不幸的是,条件中的引用了Bar元素,而不是我执行XPath的原始Ref节点。如何从XPath中引用原始上下文节点?您至少需要XPath 2.0才能使用单个纯XPath表达式来解决它:for$current in。return../Bar[Ref=$current]/Elem Microsoft不支持XPath 2.0,但有第三方XPath 2实现可插入现有的.NET体系结构,并提供扩展方法,例如XmlNode(需要使用Wmhelp.XPath2

如何引用从XPath中计算XPath的节点

例如:


不幸的是,条件中的
引用了
Bar
元素,而不是我执行XPath的原始
Ref
节点。如何从XPath中引用原始上下文节点?

您至少需要XPath 2.0才能使用单个纯XPath表达式来解决它:
for$current in。return../Bar[Ref=$current]/Elem

Microsoft不支持XPath 2.0,但有第三方XPath 2实现可插入现有的.NET体系结构,并提供扩展方法,例如
XmlNode
(需要
使用Wmhelp.XPath2;
和NuGet软件包):

输出
Y

如果希望在.NET framework XPath API中具有变量解析,则
SelectSingleNode/SelectNodes
的第二个参数是
XmlNamespaceManager
,它是
XsltContext
的基类,具有方法
ResolveVariable
。对于
公共类DynamicContext:XsltContext
中的变量解析,有一种方法实现了
XsltContext
,因此您可以使用它:

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(@"<Root>
    <Foo>
        <Bar><Elem>X</Elem><Ref>1</Ref></Bar>
        <Bar><Elem>Y</Elem><Ref>2</Ref></Bar>
        <Ref>2</Ref>
    </Foo>
</Root>");

            XmlNode refEl = doc.SelectSingleNode("Root/Foo/Ref");

            DynamicContext context = new DynamicContext();
            context.AddVariable("current", refEl);

            XmlNode elem = refEl.SelectSingleNode("../Bar[Ref = $current]/Elem", context);

            Console.WriteLine(elem.OuterXml);
XmlDocument doc=新的XmlDocument();
doc.LoadXml(@)
X1
Y2
2.
");
XmlNode refEl=doc.SelectSingleNode(“Root/Foo/Ref”);
DynamicContext上下文=新的DynamicContext();
AddVariable(“当前”,refEl);
XmlNode elem=refEl.SelectSingleNode(“../Bar[Ref=$current]/elem”,上下文);
Console.WriteLine(elem.OuterXml);

请参阅中的文档。

您至少需要XPath 2.0才能使用单个纯XPath表达式来解决此问题:
for$current in。return../Bar[Ref=$current]/Elem

Microsoft不支持XPath 2.0,但有第三方XPath 2实现可插入现有的.NET体系结构,并提供扩展方法,例如
XmlNode
(需要
使用Wmhelp.XPath2;
和NuGet软件包):

输出
Y

如果希望在.NET framework XPath API中具有变量解析,则
SelectSingleNode/SelectNodes
的第二个参数是
XmlNamespaceManager
,它是
XsltContext
的基类,具有方法
ResolveVariable
。对于
公共类DynamicContext:XsltContext
中的变量解析,有一种方法实现了
XsltContext
,因此您可以使用它:

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(@"<Root>
    <Foo>
        <Bar><Elem>X</Elem><Ref>1</Ref></Bar>
        <Bar><Elem>Y</Elem><Ref>2</Ref></Bar>
        <Ref>2</Ref>
    </Foo>
</Root>");

            XmlNode refEl = doc.SelectSingleNode("Root/Foo/Ref");

            DynamicContext context = new DynamicContext();
            context.AddVariable("current", refEl);

            XmlNode elem = refEl.SelectSingleNode("../Bar[Ref = $current]/Elem", context);

            Console.WriteLine(elem.OuterXml);
XmlDocument doc=新的XmlDocument();
doc.LoadXml(@)
X1
Y2
2.
");
XmlNode refEl=doc.SelectSingleNode(“Root/Foo/Ref”);
DynamicContext上下文=新的DynamicContext();
AddVariable(“当前”,refEl);
XmlNode elem=refEl.SelectSingleNode(“../Bar[Ref=$current]/elem”,上下文);
Console.WriteLine(elem.OuterXml);
请参阅中的文档

更改为原始xpath


更改为原始xpath

如果您使用的是xpath 1.0,那么最好的选择可能是绑定一个变量,如下所示,具体取决于您的API:

var myNode = GetNodeRootFooRef();
var xpath = "../Bar[Ref=$current]/Elem";
XPath x = new XPath():
x.bindVariable("current", myNode);
x.evaluate(xpath, myNode);

但是您已经达到了XPath 1.0的极限,因此是时候向前迈进了。.NET上提供了各种非常好的XPath 2.0和3.0引擎。

如果您使用的是XPath 1.0,那么最好的选择可能是绑定一个变量,如下所示,具体取决于您的API:

var myNode = GetNodeRootFooRef();
var xpath = "../Bar[Ref=$current]/Elem";
XPath x = new XPath():
x.bindVariable("current", myNode);
x.evaluate(xpath, myNode);

但是您已经达到了XPath 1.0的极限,因此是时候向前迈进了。在.NET上有各种非常好的XPath 2.0和3.0引擎可用。

如果Foo元素的结构始终相同,那么以下查询就足够了:

../Bar[Ref=(../Ref)]/Elem
(已试用-此xpath在线测试仪支持xpath 2.0)


我希望它也能在XPath 1.0中工作。

如果Foo元素的结构总是相同的,那么下面的查询就足够了:

../Bar[Ref=(../Ref)]/Elem
(已试用-此xpath在线测试仪支持xpath 2.0)


我希望它也能在XPath 1.0中工作。

我使用的是.NET 4.6.2 framework的
System.Xml
组件-是否有其他与该库支持的XPath标准兼容的替代方案?谢谢你的指点,支持你的答案!但无法接受,因为它对我的(开源)项目没有帮助。。。我买不起这样的图书馆。无论如何,谢谢你的支持!至于使用
SelectSingleNode
,它有一个使用XmlNamespaceManager的重载,这是一个可以扩展和实现的基类,以提供on函数和/或变量解析,从而使类似
current()
$current
的功能正常工作。不过,这是相当大的努力。谢谢你的指点,如果我们想在这个问题上花费更多的精力,我必须和我的同龄人谈谈。。。如果我们要实现您的想法,我们会在这里报告我们的解决方案!谢谢@D.R.,我最初的答案并不完全正确,因为XPath2.0而不是3.0就足够了。对于XPath 2.0,有一个NuGet包可能更适合您的需要。我正在使用.NET 4.6.2 framework的
System.Xml
组件-是否有其他与此库支持的XPath标准兼容的替代方案?感谢您的提示,我对您的答案投了赞成票!但无法接受,因为它对我的(开源)项目没有帮助。。。我买不起这样的图书馆。无论如何,谢谢你的支持!至于使用
SelectSingleNode
,它有一个使用XmlNamespaceManager的重载,这是一个可以扩展和实现的基类,以提供on函数和/或变量解析,从而使类似
current()
$current
的功能正常工作。然而,这是相当大的努力
var myNode = GetNodeRootFooRef();
var xpath = "../Bar[Ref=$current]/Elem";
XPath x = new XPath():
x.bindVariable("current", myNode);
x.evaluate(xpath, myNode);
../Bar[Ref=(../Ref)]/Elem