XPath 获取两个node中间的HTML Nodes

2015-06-01 16:42 972人阅读 评论(0) 收藏 举报
//div[@id="Recipe"]//h5[contains(text(),"Ingredients")]/following-sibling::p[count(.|//div[@id="Recipe"]//h5[contains(text(),"Method")]/preceding-sibling::p) = count(//div[@id="Recipe"]//h5[contains(text(),"Method")]/preceding-sibling::p)]

In XPath 1.0 one way to do this is by using the Kayessian method for node-set intersection:

$ns1[count(.|$ns2) = count($ns2)]

The above expression selects exactly the nodes that are part both of the node-set $ns1 and the node-set $ns2.

To apply this to the specific question -- let's say we need to select all nodes between the 2nd and 3rd h3 element in the following XML document:

<html>
<h3>Title T31</h3>
<a31/>
<b31/>
<h3>Title T32</h3>
<a32/>
<b32/>
<h3>Title T33</h3>
<a33/>
<b33/>
<h3>Title T34</h3>
<a34/>
<b34/>
<h3>Title T35</h3>
</html>

We have to substitute $ns1 with:

/*/h3[2]/following-sibling::node()

and to substitute $ns2 with:

/*/h3[3]/preceding-sibling::node()

Thus, the complete XPath expression is:

/*/h3[2]/following-sibling::node()
[count(.|/*/h3[3]/preceding-sibling::node())
=
count(/*/h3[3]/preceding-sibling::node())
]

We can verify that this is the correct XPath expression:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:template match="/">
<xsl:copy-of select=
"/*/h3[2]/following-sibling::node()
[count(.|/*/h3[3]/preceding-sibling::node())
=
count(/*/h3[3]/preceding-sibling::node())
]
"/>
</xsl:template>
</xsl:stylesheet>

When this transformation is applied on the XML document presented above, the wanted, correct result is produced:

<a32/>

<b32/>

II. XPath 2.0 solution:

Use the intersect operator:

   /*/h3[2]/following-sibling::node()
intersect
/*/h3[3]/preceding-sibling::node()

 

XPath 获取两个node中间的HTML Nodes的更多相关文章

  1. 爬虫 xpath 获取方式

    回顾 bs4 实例化bs对象,将页面源码数据加载到该对象中 定位标签:find('name',class_='xxx') findall() select() 将标签中的文本内容获取 string t ...

  2. Appium根据xpath获取控件

    如文章< Appium基于安卓的各种FindElement的控件定位方法实践>所述,Appium拥有众多获取控件的方法.其中一种就是根据控件所在页面的XPATH来定位控件. 本文就是尝试通 ...

  3. Appium依据xpath获取控件实例随笔

    如文章<Appium基于安卓的各种FindElement的控件定位方法实践>所述,Appium拥有众多获取控件的方法.当中一种就是依据控件所在页面的XPATH来定位控件. 本文就是尝试通过 ...

  4. 【转】Appium根据xpath获取控件实例随笔

    原文地址:http://blog.csdn.net/zhubaitian/article/details/39754233 如文章<Appium基于安卓的各种FindElement的控件定位方法 ...

  5. Appium根据xpath获取控件实例随笔

    如文章<Appium基于安卓的各种FindElement的控件定位方法实践>所述,Appium拥有众多获取控件的方法.其中一种就是根据控件所在页面的XPATH来定位控件. 本文就是尝试通过 ...

  6. 使用python+xpath 获取https://pypi.python.org/pypi/lxml/2.3/的下载链接

    使用python+xpath 获取https://pypi.python.org/pypi/lxml/2.3/的下载链接: 使用requests获取html后,分析html中的标签发现所需要的链接在& ...

  7. Java 获取两个日期之间的日期

    1.前期需求,两个日期,我们叫他startDate和endDate,然后获取到两个日期之间的日期 /** * 获取两个日期之间的日期 * @param start 开始日期 * @param end ...

  8. xpath获取下一页,兄弟结点的妙用

    第一页的情况: 第四页的情况 : 文章的链接:  http://tech.huanqiu.com/science/2018-02/11605853_4.html 从上面我们可以看到,如果仅仅用xpat ...

  9. JavaScript实现获取两个排序数组的中位数算法示例

    本文实例讲述了JavaScript排序代码实现获取两个排序数组的中位数算法.分享给大家供大家参考,具体如下: 题目 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个 ...

随机推荐

  1. 13 个最佳 JavaScript 数据网格库

    13 个最佳 JavaScript 数据网格库   转自:开源中国 www.oschina.net/translate/best-javascript-data-grid-libraries Java ...

  2. 44个 Javascript 变态题解析 (上)

    原题来自: javascript-puzzlers(http://javascript-puzzlers.herokuapp.com/) 读者可以先去做一下感受感受. 当初笔者的成绩是 21/44… ...

  3. 转载:JVM内存分代策略

    Java虚拟机根据对象存活的周期不同,把堆内存划分为几块,一般分为新生代.老年代和永久代(对HotSpot虚拟机而言),这就是JVM的内存分代策略. 为什么要分代? 堆内存是虚拟机管理的内存中最大的一 ...

  4. 2018-8-17-C#-从零开始写-SharpDx-应用-控制台创建-Sharpdx-窗口

    title author date CreateTime categories C# 从零开始写 SharpDx 应用 控制台创建 Sharpdx 窗口 lindexi 2018-8-17 9:3:3 ...

  5. Hadoop 单机安装配置

  6. 设置mysql二进制日志过期时间

    ((none)) > show variables like 'expire_logs_days'; +------------------+-------+ | Variable_name | ...

  7. 彩色图像与二值图像(mask)点乘

    问题描述:给出一幅彩色图像和一张mask二值图像,其中mask和彩色图像大小相同,感兴趣的部分为1,其余部分为0,请用mask与彩色图像点乘,将感兴趣区域显示出来. 点乘的本质是mask中是二值图像, ...

  8. bzoj 1093 [ZJOI2007]最大半连通子图——缩点+拓扑

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1093 缩点+拓扑,更新长度的时候维护方案数. 结果没想到处理缩点后的重边,这样的话方案数会算 ...

  9. bootstrap-fileinput详细说明与使用

    介绍 bootstrap-fileinput是一款非常优秀的HTML5文件上传插件,支持文件预览.多文件上传等一系列特性. 一款非常优秀的HTML5文件上传插件,支持bootstrap 3.x 和4. ...

  10. mybatis深入理解(四)-----MyBatis的架构设计以及实例分析

    MyBatis是目前非常流行的ORM框架,它的功能很强大,然而其实现却比较简单.优雅.本文主要讲述MyBatis的架构设计思路,并且讨论MyBatis的几个核心部件,然后结合一个select查询实例, ...