Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用XPath提取两个标记之间的内容_Xpath - Fatal编程技术网

使用XPath提取两个标记之间的内容

使用XPath提取两个标记之间的内容,xpath,Xpath,我最近刚开始使用XPath,遇到了一个问题。下面是我想从中提取的代码: <h3>Some Company</h3> Mainstreet 1234 <br> 98776, Country <br> 某公司 主干街1234 98776,国家 如何提取结束h3和br标记之间的内容?这可以工作h3/后面的同级::node()[not(前面的同级::br)和not(self::br)](为我返回“Mainstreet 1234

我最近刚开始使用XPath,遇到了一个问题。下面是我想从中提取的代码:

 <h3>Some Company</h3>
    Mainstreet 1234
 <br>
    98776, Country
 <br>
某公司
主干街1234

98776,国家

如何提取结束h3和br标记之间的内容?

这可以工作
h3/后面的同级::node()[not(前面的同级::br)和not(self::br)]
(为我返回“Mainstreet 1234”)


但我很惊讶您真正的xml和实际需求比提供的示例更复杂,因此您可能需要进一步调整它以满足您的需求。

如果您的代码在下面的代码块中:

<par>
    <h3>Some Company</h3>
    Mainstreet 1234
    <br>
    98776, Country
    </br>
</par>

以上将在文档中查找PAR节点中的任意位置。您还可以更具体地了解h3和/或br节点的内容:

//par/text()[preceding::*[name()='h3' and text()='Some Company'] and following::*[name()='br']]

如果上述方法不能解决您的问题,请告诉我。

试试
//h3/以下兄弟姐妹::text()[following::br]

效果很好。非常感谢你!
//par/text()[preceding::*[name()='h3' and text()='Some Company'] and following::*[name()='br']]