c# - XPath 所有后代,即使是嵌套的 - 但不是父级的后代

标签 c# xpath

我正在尝试编写 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/

相关文章:

c# - 如何将 Microsoft.Extensions.Logging 与 Application Insights 结合使用来记录事件和指标

c# - UWP - 检查表是否存在

c# - 在运行时更改成员身份提供程序 ApplicationName。如何?

r - 快速读取xml文件并转换为数据框

java - 测试表达式中的 XSLT 2.0 析取

c# - Azure 服务总线主题架构

c# - 绑定(bind) MobileServiceCollection 未在 UI (MVVM) 中更新

xml - 如果另一个子值匹配,则获取子值

php - 无法使用 xpath 获取 span 标记

xpath 选择器中的 C# 变量