descendant-or-self轴选取当前节点及其所有后代节点,如<bookstore>下使用descendant-or-self::*可选中自身及<book>、<title>、<author>,而descendant轴不包含自身,self轴仅选自身,结合谓语可实现精准查询。

XPath的
descendant-or-self
解决方案:
descendant-or-self
简单来说,
descendant-or-self::node()
descendant-or-self
举个例子,假设我们有以下XML片段:
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
</book>
</bookstore>如果当前节点是
<bookstore>
descendant-or-self::*
<bookstore>
<book>
<title>
<author>
*
descendant-or-self::title
<title>
descendant-or-self
descendant
descendant
<bookstore>
descendant::*
<book>
<title>
<author>
<bookstore>
另外,
self
<bookstore>
self::*
<bookstore>
理解这些轴之间的区别,对于编写精确的XPath表达式至关重要。
在实际应用中,
descendant-or-self
title
//descendant-or-self::title
这个表达式会选取文档中所有名为
title
另一个例子,假设我们想选取所有具有
lang
//descendant-or-self::*[@lang]
这个表达式会选取文档中所有具有
lang
[@lang]
lang
此外, 结合
descendant-or-self
[]
bookstore
author
//bookstore/descendant-or-self::author[text()='Giada De Laurentiis']
总而言之, 掌握
descendant-or-self
以上就是XPath的descendant-or-self轴包含自身吗?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号