抓取不是其他节点后代的节点,不包括当前上下文

发布于 2024-10-31 18:00:13 字数 4140 浏览 4 评论 0原文

所以,我不知道如何真正表达这个问题。我想要一个节点列表,并且只选择一个节点,而不选择嵌套节点。例如:

<root>
    <my_element>
        <my_element>
            Some Text
        </my_element>
    </my_element>
</root>

我知道我已经可以通过使用这个 xpath: 来做一些我想做的事情:

Context: /
xPath: descendant::my_element[not(ancestor::my_element)]

这将返回这个结果集:

<root>
    [<my_element>]
        <my_element>
            Some Text
        </my_element>
    [</my_element>]
</root>

这是我想要的预期行为。但我希望能够将上下文更改为:

/my_element

并获得此结果集:

<root>
    <my_element>
        [<my_element>]
            Some Text
        [</my_element>]
    </my_element>
</root>

我已尽力查看 xPath 文档,但还没有想出太多东西。也许这里有人可以提供一些见解?

谢谢!

  • 编辑- 我希望能够选择一个 my_element 后代,它不是 my_element 的祖先(不包括上下文节点)。

  • 再次编辑 - 进一步解释。

我想要一个选择 my_element 节点的 xpath 查询,只要该节点不是 my_element 的子节点。但是,如果 xpath 上下文设置为 my_element 节点,那么我不希望该节点计入表达式中。因此,xpath 将匹配下一个 my_element 节点,即使它实际上是 my_element 的子节点。

  • 再次编辑 -

这里还有一些例子。

<root>
    <a>
        <a>
            <b>
                <a>
                    Hello!
                </a>
            </b>
            <a>
                <b>
                    Hello Again
                    <a>
                        Sub
                    </a>
                </b>
            </a>
        </a>
    </a>
</root>

Context: /root/
Desire: Want to grab all A nodes, so long as they aren't a descendant of A

Result:
<root> == Context
    [<a>]
        <a>
            <b>
                <a>
                    Hello!
                </a>
            </b>
            <a>
                <b>
                    Hello Again
                    <a>
                        Sub
                    </a>
                </b>
            </a>
        </a>
    [</a>]
</root>

Context: /root/a/
Desire: Want to grab all A nodes, so long as they aren't a descendant of A, not including the context /root/a/

Result:
<root>
    <a> == Context
        [<a>]
            <b>
                <a>
                    Hello!
                </a>
            </b>
            <a>
                <b>
                    Hello Again
                    <a>
                        Sub
                    </a>
                </b>
            </a>
        [</a>]
    </a>
</root>

Context: /root/a/a/
Desire: Want to grab all A nodes, so long as they aren't a descendant of A, not including the context /root/a/a/

Result:
<root>
    <a>
        <a> == Context
            <b>
                [<a>]
                    Hello!
                [</a>]
            </b>
            [<a>]
                <b>
                    Hello Again
                    <a>
                        Sub
                    </a>
                </b>
            [</a>]
        </a>
    </a>
</root>

Context: /root/a/a/a/
Desire: Want to grab all A nodes, so long as they aren't a descendant of A, not including the context /root/a/a/a/

Result:
<root>
    <a>
        <a>
            <b>
                <a>
                    Hello!
                </a>
            </b>
            <a> == Context
                <b>
                    Hello Again
                    [<a>]
                        Sub
                    [</a>]
                </b>
            </a>
        </a>
    </a>
</root>

我希望这能让我的愿望更加清晰。感谢所有正在努力的人!

So, I have no idea how to really word this question. I want to have a list of nodes, and only select the one node, but not the nested node. For example:

<root>
    <my_element>
        <my_element>
            Some Text
        </my_element>
    </my_element>
</root>

I know I can already do some of what I want by using this xpath:

Context: /
xPath: descendant::my_element[not(ancestor::my_element)]

Which would return this result set:

<root>
    [<my_element>]
        <my_element>
            Some Text
        </my_element>
    [</my_element>]
</root>

That's the expected behaviour of what I want. But I want to be able to change the context to:

/my_element

And get this result set:

<root>
    <my_element>
        [<my_element>]
            Some Text
        [</my_element>]
    </my_element>
</root>

I've tried my best at looking at xPath documents and I haven't come up with much of anything. Perchance someone on here could provide some insight?

Thanks!

  • edit -
    I want to be able to select a my_element descendant which isn't an ancestor of my_element excluding the context node.

  • edit again -
    To further explain.

I want to have an xpath query that selects nodes of my_element so long as the node isn't a child of my_element. But, if the xpath context is set to a my_element node, then I don't want that node to count in the expression. So the xpath would then match the next my_element node, even though it is actually a child of my_element.

  • edit again -

Here are some more examples.

<root>
    <a>
        <a>
            <b>
                <a>
                    Hello!
                </a>
            </b>
            <a>
                <b>
                    Hello Again
                    <a>
                        Sub
                    </a>
                </b>
            </a>
        </a>
    </a>
</root>

Context: /root/
Desire: Want to grab all A nodes, so long as they aren't a descendant of A

Result:
<root> == Context
    [<a>]
        <a>
            <b>
                <a>
                    Hello!
                </a>
            </b>
            <a>
                <b>
                    Hello Again
                    <a>
                        Sub
                    </a>
                </b>
            </a>
        </a>
    [</a>]
</root>

Context: /root/a/
Desire: Want to grab all A nodes, so long as they aren't a descendant of A, not including the context /root/a/

Result:
<root>
    <a> == Context
        [<a>]
            <b>
                <a>
                    Hello!
                </a>
            </b>
            <a>
                <b>
                    Hello Again
                    <a>
                        Sub
                    </a>
                </b>
            </a>
        [</a>]
    </a>
</root>

Context: /root/a/a/
Desire: Want to grab all A nodes, so long as they aren't a descendant of A, not including the context /root/a/a/

Result:
<root>
    <a>
        <a> == Context
            <b>
                [<a>]
                    Hello!
                [</a>]
            </b>
            [<a>]
                <b>
                    Hello Again
                    <a>
                        Sub
                    </a>
                </b>
            [</a>]
        </a>
    </a>
</root>

Context: /root/a/a/a/
Desire: Want to grab all A nodes, so long as they aren't a descendant of A, not including the context /root/a/a/a/

Result:
<root>
    <a>
        <a>
            <b>
                <a>
                    Hello!
                </a>
            </b>
            <a> == Context
                <b>
                    Hello Again
                    [<a>]
                        Sub
                    [</a>]
                </b>
            </a>
        </a>
    </a>
</root>

I hope this makes my desires clearer. Thank you everyone who is trying!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青衫儰鉨ミ守葔 2024-11-07 18:00:13

使用

//my_element[not(.//my_element)]

这将选择所有名为 my_element 且没有任何 my_element 后代的元素。

Use:

//my_element[not(.//my_element)]

This selects all elements named my_element that don't have any my_element descendents.

踏雪无痕 2024-11-07 18:00:13

我认为您可能陷入了一个常见的陷阱。您的数据上的 XPath 表达式 /root/my_element 将仅选择一个元素 - 最外面的 my_element 节点。但该节点仍与其父节点、兄弟姐妹和子节点相连。当您显示选择的结果时,该节点通常会与其子节点(实际上是所有子节点)一起显示 - 不是因为 XPath 选择了子节点,而是因为这是显示所选单个节点的友好方式。

另一方面,我再次阅读了这个问题,我可能是错的 - 我从您用来显示 XPath 表达式结果的特殊符号中猜测到了这一点。

仅当其父节点是树根处的文档节点时,表达式 /my_element 才会选择 my_element,而无论您的上下文节点如何,您的输入都不会出现这种情况。当然,您可以将以 my_element 为根的子树复制到新文档中,在这种情况下,该表达式将起作用。

I think you may be falling into a common trap. The XPath expression /root/my_element, on your data, will select only one element - the outermost my_element node. But that node is still attached to its parents, siblings, and children. When you display the result of the selection, the node will often be displayed together with its children (indeed, all descendants) - not because the XPath selected the children, but because that's a friendly way to display the single node that was selected.

On the other hand, I've read the question again, and I might be wrong - I guessed that from the peculiar notation you were using to show the results of your XPath expression.

The expression /my_element will select my_element only if its parent is the document node at the root of the tree, which will never be the case with your input, whatever your context node. Of course, you can copy the subtree rooted at my_element into a new document, in which case this expression will work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文