Skip to content

Commit

Permalink
添加解析过程示例
Browse files Browse the repository at this point in the history
  • Loading branch information
zhegexiaohuozi committed Apr 8, 2018
1 parent 8a91bc1 commit e936590
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
11 changes: 6 additions & 5 deletions README.md
Expand Up @@ -67,17 +67,17 @@ for (Object o:rs){

## 语法 ##

支持完备的W3C XPATH 1.0标准语法,W3C规范:http://www.w3.org/TR/1999/REC-xpath-19991116
支持完备的W3C XPATH 1.0标准语法,W3C规范:http://www.w3.org/TR/1999/REC-xpath-19991116 ,这里也提供了两个解析案例,由于图片较大,请移步 ![XpathParseTree](https://github.com/zhegexiaohuozi/JsoupXpath/blob/develop/XpathParseTree.md)

### 关于使用Xpath的一些注意事项 ####

多数情况下是不建议直接粘贴Firefox或chrome里生成的Xpath,这些浏览器在渲染页面会根据标准自动补全一些标签,如table标签会自动加上tbody标签,这样生成的Xpath路径显然不是最通用的,所以很可能就取不到值。所以,要使用Xpath并感受Xpath的强大以及他所带来便捷与优雅最好就是学习下Xpath的标准语法,这样应对各种问题才能游刃有余,享受Xpath的真正威力!

## 函数 ##

- `int position()` 返回当前节点在当前上下文中的位置
- `int last()` 返回同级节点中的最后那个节点位置
- `int first()` 返回同级节点中的第一个节点位置
- `int position()` 返回当前节点在其所在上下文中的位置
- `int last()` 返回所在上下文的最后那个节点位置
- `int first()` 返回所在上下文的的第一个节点位置
- `string concat(string, string, string*)` 连接若干字符串
- `boolean contains(string, string)` 判断第一个字符串是否包含第二个
- `int count(node-set)` 计算给定的节点集合中节点个数
Expand All @@ -86,13 +86,14 @@ for (Object o:rs){
- `string substring(string, number, number?)` 第一个参数指定字符串,第二个指定起始位置(xpath索引都是从1开始),第三指定要截取的长度,这里要注意在xpath的语法里这,不是结束的位置。

substring("12345", 1.5, 2.6) returns "234"

substring("12345", 2, 3) returns "234"

- `string substring-ex(string, number, number)` 第一个参数指定字符串,第二个指定起始位置(java里的习惯从0开始),第三个结束的位置(支持负数),这个是JsoupXpath扩展的函数,方便java习惯的开发者使用。
- `string substring-after(string, string)` 在第一个字符串中截取第二个字符串之后的部分
- `string substring-before(string, string)` 在第一个字符串中截取第二个字符串之前的部分

以上只是Xpath1.0标准中的函数,开发亦可以方便快捷的添加自定义函数,只需实现 `cn.wanghaomiao.xpath.core.Function.java`接口并且包路径为`package cn.wanghaomiao.xpath.core.function;`即可,不需要修改语法范式,JsoupXpath运行时即可自动识别并加载。
以上只是Xpath1.0标准中的函数,开发亦可以方便快捷的添加自定义函数,只需实现 `org.seimicrawler.xpath.core.Function.java`接口并且包路径为`package org.seimicrawler.xpath.core.function;`即可,不需要修改语法范式,JsoupXpath运行时即可自动识别并加载。

### NodeTest ###
- `allText()`提取节点下全部文本,取代类似 `//div/h3//text()`这种递归取文本用法
Expand Down
8 changes: 8 additions & 0 deletions XpathParseTree.md
@@ -0,0 +1,8 @@
这里是JsoupXpath的基于Antlr4的语法解析树示例,方便大家更好的一看JsoupXpath的语法处理能力与语法执行过程
- `//ul[@class='subject-list']/li[./div/div/span[@class='pl']/num()>(1000+90*(2*50))][last()][1]/div/h2/allText()`
这个主要是一些表达式嵌套的解析示例,点击图片可以查看原图
[![muti_expr](http://img.wanghaomiao.cn/jsoupxpath/antlr4_parse_tree_muti_expr.png)](http://img.wanghaomiao.cn/jsoupxpath/antlr4_parse_tree_muti_expr.png)

- `//ul[@class='subject-list']/li[not(contains(self::li/div/div/span[@class='pl']//text(),'14582'))]/div/h2//text()`
这个是对内置函数支持的一个解析示例,点击图片可以查看原图
[![functions](http://img.wanghaomiao.cn/jsoupxpath/antlr4_parse_tree_functions_v2.png)](http://img.wanghaomiao.cn/jsoupxpath/antlr4_parse_tree_functions_v2.png)
2 changes: 1 addition & 1 deletion src/test/java/org/seimicrawler/xpath/JXDocumentTest.java
Expand Up @@ -93,7 +93,7 @@ public static Object[][] dataOfXpathAndexpect() {
return new Object[][] {
{ "//ul[@class='subject-list']/li[position()<3][last()]/div/h2/allText()", "黑客与画家 : 硅谷创业之父Paul Graham文集T2-黑客与画家 : 硅谷创业之父Paul Graham文集" },
{ "//ul[@class='subject-list']/li[first()]/div/h2/allText()", "失控 : 全人类的最终命运和结局T2-失控 : 全人类的最终命运和结局" },
{ "//ul[@class='subject-list']/li[./div/div/span[@class='pl']/num()>10000][last()][1]/div/h2/allText()", "长尾理论长尾理论" },
{ "//ul[@class='subject-list']/li[./div/div/span[@class='pl']/num()>(1000+90*(2*50))][last()][1]/div/h2/allText()", "长尾理论长尾理论" },
{ "//ul[@class='subject-list']/li[self::li/div/div/span[@class='pl']/num()>10000][-1]/div/h2/allText()", "长尾理论长尾理论" },
{ "//ul[@class='subject-list']/li[contains(self::li/div/div/span[@class='pl']//text(),'14582')]/div/h2//text()", "黑客与画家 : 硅谷创业之父Paul Graham文集T2-黑客与画家 : 硅谷创业之父Paul Graham文集" },
{ "//ul[@class='subject-list']/li[contains(./div/div/span[@class='pl']//text(),'14582')]/div/h2//text()", "黑客与画家 : 硅谷创业之父Paul Graham文集T2-黑客与画家 : 硅谷创业之父Paul Graham文集" },
Expand Down

0 comments on commit e936590

Please sign in to comment.