XPath 获取一级子节点
使用 DOMXPath::query 是否可以只获取一层深度的子节点?
例如,如果我有一个像这样的文档:
<div>
<span>
<cite>
</cite>
</span>
<span>
<cite>
</cite>
</span>
</div>
我希望 NodeList 仅包含跨度而不包含引用。
还应该提到的是,它并不总是相同的元素(div、span 等)。我需要它与任何类型的元素一起使用。
这是我尝试过的,但似乎不起作用:
//*[not(ancestor::div)]
Using DOMXPath::query is it possible to get just one level deep of childNodes?
For example if I had a document like:
<div>
<span>
<cite>
</cite>
</span>
<span>
<cite>
</cite>
</span>
</div>
I would want the NodeList to contain just the spans and not the cites.
Should also mention that it won't always be the same elements (divs, spans, etc). I would need it to work with any type of element.
This is what I tried and it didn't seem to work:
//*[not(ancestor::div)]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果使用,
那么您将获得该元素中所有直接子元素的列表,但这些子元素包含它们的子元素。我认为你不能删除孩子的孩子
有使用默认轴,它被称为
child::
。该轴仅返回当前节点下第 1 层的元素*
匹配所有元素,但属性和 text() 都不匹配。您必须指定节点的路径,并注意
//node 因为它意味着
descendant::node
并且它返回该树中该名称的所有节点If you use
then you get a list of all direct children in this element but these children contain their children. I think that you can't remove children of child
There is used default axis, it is called
child::
. This axis returns only elements in 1 level under the current node*
matches all elements but neither attributes nor text()You have to specify path to your node and be careful about
//node
because it meansdescendant::node
and it returns all nodes of this name in this tree你的问题有点不明确,所以有几种方法可以解释它。如果您想要当前元素的所有直接子元素(及其所有子元素),请使用
对于您的示例,这将为您提供
如果
您想要所有子节点,则使用
node()
而不是*
:对于您的示例,这将为您提供如上所述的两个子元素,以及换行/缩进
text()
节点。但是,如果您希望仅子节点而不是其子节点(即仅
span
元素,但没有其子元素),则必须使用两个表达式:*/*
选择直接子元素text()
仅选择文本节点而不选择孙元素我的 PHP 有点生锈了,但它应该有点像这样工作:
Your question is a bit under-specified, so there are several ways to interpret it. If you want all direct child elements of the current element (with all of their sub-elements), then use
For your example, this gives you
and
If you want all child nodes, then use
node()
instead of*
:For your example, this gives you both sub-elements as above, alongside with newline/indentation
text()
nodes.If, however, you want to have only the child nodes and not their children as well (i.e. only the
span
elements, but without their child elements), you must use two expressions:*/*
text()
My PHP is a bit rusty, but it should work a bit like this: