XPath:如何:选择具有当前上下文的给定属性名称的所有子项和孙项(无论深度如何)?

发布于 2024-11-14 20:57:44 字数 473 浏览 4 评论 0原文

xml:

<root0>
    <elem1>
        <elem21 xlink:href="21"/>
        <elem22>
            <elem31 xlink:href="31"/>
            <elem32>
                <elem41 xlink:href="41"/>
            </elem32>
        </elem22>
    </elem1>
</root0>

深度未知。如何选择具有 xlink:href 属性的所有元素?

我尝试了以下方法:

*[@xlink:href]
self::*[@xlink:href]

感谢任何指导。

xml:

<root0>
    <elem1>
        <elem21 xlink:href="21"/>
        <elem22>
            <elem31 xlink:href="31"/>
            <elem32>
                <elem41 xlink:href="41"/>
            </elem32>
        </elem22>
    </elem1>
</root0>

the depth is unknown. How can I select all elements with xlink:href attribute?

I tried the following:

*[@xlink:href]
self::*[@xlink:href]

Any guidance is appreciated.

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

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

发布评论

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

评论(2

流绪微梦 2024-11-21 20:57:44

对于孙子的孩子使用

descendant-or-self::*[@xlink:href]

对于所有节点,只需在 xpath 前面添加 //

//*[@xlink:href]

另外,您的 xml 示例无效,但我猜它只是一个示例。

For just children of grandchildren use

descendant-or-self::*[@xlink:href]

For all nodes just add // in front of your xpath

//*[@xlink:href]

Also, your xml sample is not valid but I'm guessing it is just a sample.

酷到爆炸 2024-11-21 20:57:44

*[@xlink:href] 应该可以工作。您很可能尚未定义 xlink 命名空间。

如果您发布代码,那么我的回答可以更准确。

*[@xlink:href] should work. It is most likely that you have not defined the xlink namespace.

If you post the code, then I can be more precise in my answer.

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