我正在尝试编写 XPath 来处理一些 XML 节点。
我的 XML 示例如下(元素名称和嵌套插图)。
<Forms>
<Form>
<Control/>
<Control>
<Control></Control>
</Control>
<Control>
<Form><!-- This is the nested one, I don't want the Control children from this-->
<Control/>
</Form>
</Control>
</Form>
有没有办法修改下面的 XPath 语句 - 以包含所有控件(甚至是相互嵌套的控件 - 在“表单”下,但不是来自嵌套表单(请参阅 Form<2>)?当前的 XPath 返回“嵌套“表单”节点下方的“控件”。
string xpath = @"/Forms/Form[@id='" + node.Attributes["id"].Value + "']//Control[@type='GroupItem' or @type='Detail' or @type='GroupHeader']";
我基本上试图取消嵌套所有 Control 标签,并用“Controls”标签包装它们,因此我在 C# 中使用此 Xpath 来重新格式化 - 但我不想将 Control 从嵌套表单中拉出,我想将它们单独包装在 Controls 标签中。
感谢您的帮助!
最佳答案
在 XPath 2.0 中
//Control except //Form//Form//Control
或在 XPath 1.0 中
//Control[not(ancestor::Form/ancestor::Form)]
关于c# - XPath 所有后代,即使是嵌套的 - 但不是父级的后代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5818826/