更正XPath查询以提取div内部文本 [英] Correct XPath query to fetch div inner text

查看:121
本文介绍了更正XPath查询以提取div内部文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用XPath查询HTML文档,其中有下面的div有文本:

 < div class = h1> 2008本田Accord Coupe< small> -  Wuse II< / small>< / div> 

我只想获取的内部文本< div class = h1> 2008本田雅阁Coupe ,不包括< small>内部文字< / small>



我正在做以下XPath查询: // div [@ class ='h1'] 这肯定会返回整个 < div> ...< / div> 节点。



如何获取没有< small> ...< / small>

感谢

解决方案使用 / code> function:



  // div [@ class ='h1' ] / text()

phpFiddle


I am using XPath to query into HTML document where there is following div has text:

<div class="h1">2008 Honda Accord Coupe<small> — Wuse II</small></div>

I wanted to fetch only the inner text of the <div class="h1">2008 Honda Accord Coupe and not include the <small>inner text</small>

I am making following XPath query: //div[@class='h1'] which is definitely returning entire <div>...</div> node.

How can I only fetch portion without <small>...</small>

thanks

解决方案

Use the text() function:

//div[@class='h1']/text()

Tested in phpFiddle

这篇关于更正XPath查询以提取div内部文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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