我有这样的结构:
<root>
<properties>
<property name="test">
<value>X</value>
</property>
</properties>
<things>
<thing>
<properties>
<property name="test">
<value>Y</value>
</property>
</properties>
</thing>
</things>
</root>
如果使用 <root>
运行,是否有一个 XPath 表达式仅选择值为 X 的测试属性作为 root,如果以 thing
运行,则只有值为 Y 的那个作为根?
我认为 /properties/property[@name='test']
会要求它是一个直接的 child ,但这似乎没有任何返回。如果我删除斜杠,我会同时得到 property
元素(我使用的是 C#,带有 XElement root = ...; root.XPathSelectElements(xpathexpression);
)。
最佳答案
我认为你的意思是 Property
而不是 Properties
。试试 ./properties/property[@name='test']
关于c# - 仅选择直系后代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9278091/