xml - 捕获不是其他节点的后代的节点,当前上下文除外

标签 xml dom xpath nodes

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

<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>


我希望这可以使我的愿望更加明确。谢谢大家在努力!

最佳答案

用:

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


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

关于xml - 捕获不是其他节点的后代的节点,当前上下文除外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5614834/

相关文章:

android - 如何检查已解析的 XML 标记值是否为空

java - URI 的引用没有使用 Apache Santuario 的 XMLSignatureInput

javascript - 浏览器如何知道哪个元素应该获得点击或悬停事件?

html - 如何检索内部文本

php - 在 PHP 中使用 Xpath 解析 XML

php - 使用 PHP 生成 XML 文件

xml - 对 Node.js 的 xml 到 json 有什么建议吗?

javascript - 当 DOM 元素的计算样式改变时触发事件?

JavaScript 函数和 DOM

java - 使用 XPath 检索有序列表中的所有链接