使用嵌套的XPath谓词......精炼

Fra*_*ank 8 xpath

所有好的答案!但问题值得改进......

我有以下示例XML ...

<objects>
    <object objectId="1123" ... />
    <properties refObjectId="1123" ... />
    <properties refObjectId="1123" refPropertyId="2311" ... />
    <properties refObjectId="1123" refPropertyId="4611" ... />
    <object objectId="2123" ... />
    <properties refObjectId="2123" refPropertyId="4311" ... />
    <properties refObjectId="2123" refPropertyId="8611" ... />
    ....
</objects>
Run Code Online (Sandbox Code Playgroud)

...以及以下XPath查询...

//object[//properties[@refObjectId=@objectId and not(@refPropertyId)]]
Run Code Online (Sandbox Code Playgroud)

我想这个查询将返回所有object节点那里是一个properties具有节点refObjectId,等于属性objectId的属性object节点,并没有"refPropertyId"属性...即对象1123只,不反对2123 ...但事实并非如此.似乎@objectId嵌套谓词中没有引用节点的objectId属性object.

有任何想法吗?我知道XML结构没有像你期望的那样嵌套,但是有这种结构的原因.

Jon*_*n W 7

一般来说,你应该尽量避免使用//.我会考虑改写:

//object[../properties/@refObjectId=@objectId]
Run Code Online (Sandbox Code Playgroud)

在提供的表达式中,您的嵌套谓词实际上正在检查

//properties/@refObjectId=//properties/@objectId 
Run Code Online (Sandbox Code Playgroud)

其中没有.

我希望这有帮助!

编辑:由于问题已更新,这里是一个更新的响应:您添加"似乎嵌套谓词中的@objectId不引用对象节点的objectId属性." 你是绝对正确的!所以让我们解决它!!

//object[../properties[not(@refPropertyId)]/@refObjectId=@objectId]
Run Code Online (Sandbox Code Playgroud)

这应该更接近你所追求的!