选择nodeValue,但不包括子元素 [英] Select nodeValue but exclude child elements

查看:127
本文介绍了选择nodeValue,但不包括子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这个代码:

 < p dataname =description> 
您好,这是一个描述。 < a href =#>点击此处查看更多。< / a>
< / p>

如何选择 p 的nodeValue但是不包括 a ,它的内容?



我目前的代码:

  $ result = $ xpath-> query(// p [@ dataname ='description'] [not(self :: a)]); 

我通过 $ result-> item(0) - > ; nodeValue;

解决方案

只需将你的查询附加/ text() p>

  $ result = $ xpath-> query(// p [@ dataname ='description'] [not(self一)] /文本()); 


Let's say I have this code:

<p dataname="description">
Hello this is a description. <a href="#">Click here for more.</a>
</p>

How do I select the nodeValue of p but exclude a and it's content?

My current code:

$result = $xpath->query("//p[@dataname='description'][not(self::a)]");

I select it by $result->item(0)->nodeValue;

解决方案

Simply appending /text() to your query should do the trick

$result = $xpath->query("//p[@dataname='description'][not(self::a)]/text()");

这篇关于选择nodeValue,但不包括子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆