Xpath中text(),string(),data()的区别

摘要: 在XPath中,经常使用text()和string(),而我一般都是想到哪个用哪个,究竟他们之间有什么不同,没有在意过。

本质区别

text()是一个node test,而string()是一个函数,data()是一个函数且可以保留数据类型。此外,还有点号(.)表示当前节点。

使用要点

XML例子: 
<book><author>Tom John</author></book>

用例举例
text()book/author/text()
string()book/author/string()
data()book/author/data()
.book/author/.

特殊用例

XML例子:

  1.  
    <book>
  2.  
    <author>Tom <em>John</em> cat</author>
  3.  
    <pricing>
  4.  
    <price>20</price>
  5.  
    <discount>0.8</discount>
  6.  
    </pricing>
  7.  
    </book>

text()

经常在XPath表达式的最后看到text(),它仅仅返回所指元素的文本内容。

  1.  
    let $x := book/author/text()
  2.  
    return $x

返回的结果是Tom cat,其中的John不属于author直接的节点内容。

string()

string()函数会得到所指元素的所有节点文本内容,这些文本讲会被拼接成一个字符串。

  1.  
    let $x := book/author/string()
  2.  
    return $x

返回的内容是”Tom John cat”

data()

大多数时候,data()函数和string()函数通用,而且不建议经常使用data()函数,有数据表明,该函数会影响XPath的性能。

  1.  
    let $x := book/pricing/string()
  2.  
    return $x

返回的是200.8

  1.  
    let $x := book/pricing/data()
  2.  
    return $x

这样将返回分开的20和0.8,他们的类型并不是字符串而是xs:anyAtomicType,于是就可以使用数学函数做一定操作。

  1.  
    let $x := book/pricing/price/data()
  2.  
    let $y := book/pricing/discount/data()
  3.  
    return $x*$y

比如上面这个例子,就只能使用data(),不能使用text()或 string(),因为XPath不支持字符串做数学运算。

总结

text()不是函数,XML结构的细微变化,可能会使得结果与预期不符,应该尽量少用,data()作为特殊用途的函数,可能会出现性能问题,如无特殊需要尽量不用,string()函数可以满足大部分的需求。

转载于:https://www.cnblogs.com/mabingxue/p/11057471.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
A hands on guide to web scraping and text mining for both beginners and experienced users of R Introduces fundamental concepts of the main architecture of the web and databases and covers HTTP, HTML, XML, JSON, SQL. Provides basic techniques to query web documents and data sets (XPath and regular expressions). An extensive set of exercises are presented to guide the reader through each technique. Explores both supervised and unsupervised techniques as well as advanced techniques such as data scraping and text management. Case studies are featured throughout along with examples for each technique presented. R code and solutions to exercises featured in the book are provided on a supporting website. Table of Contents Chapter 1: Introduction Part One: A Primer on Web and Data Technologies Chapter 2: HTML Chapter 3: XML and JSON Chapter 4: XPath Chapter 5: HTTP Chapter 6: AJAX Chapter 7: SQL and relational databases Chapter 8: Regular expressions and essential string functions Part Two: A Practical Toolbox for Web Scraping and Text Mining Chapter 9: Scraping the Web Chapter 10: Statistical text processing Chapter 11: Managing data projects Part Three: A Bag of Case Studies Chapter 12: Collaboration networks in the US Senate Chapter 13: Parsing information from semistructured documents Chapter 14: Predicting the 2014 Academy Awards using Twitter Chapter 15: Mapping the geographic distribution of names Chapter 16: Gathering data on mobile phones Chapter 17: Analyzing sentiments of product reviews

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值