[英]Xpath union multiple queries
我正在从另一个网站取消工作。 当用户复制粘贴数据和结构更改时,源网站有不同的情况。
情况1:
<h3>Job Description</h3>
<div style="text-align: justify; line-height: 115%"><b>
Receptionist is assigned for ANAFAE-ALC based in Mazar-e-Sharif. This position is supervised by and reports to ALC Educational Program Manager and following are the main duties but are not limited to that.</div>
情况2:
<h3>Job Description</h3>
<p>
Receptionist is assigned for ANAFAE-ALC based in Mazar-e-Sharif. This position is supervised by and reports to ALC Educational Program Manager and following are the main duties but are not limited to that.</p>
在这种情况下,p标签有时会替换其他html标签。
情况3:
<h3>Job Description</h3>
Receptionist is assigned for ANAFAE-ALC based in Mazar-e-Sharif. This position is supervised by and reports to ALC Educational Program Manager and following are the main duties but are not limited to that.
我正在使用此字符串来获取内容。 现在,这适用于情况3,但不适用于其他两种情况。 如何解决这三种情况下的问题。
//text()[preceding::h3[text()="Job Description"]
您的XPath表达式选择在<h3>
之前的文本节点,该文本节点的文本节点等于“作业描述”。 这仅与第三种情况匹配,因为前两种情况在<h3>
之后分别具有<div>
和<p>
<h3>
。
您可以尝试这样的事情:
//node()[preceding-sibling::*[1][self::h3 = "Job Description"]]/string()
一些细节:
//node()
从初始上下文中选择所有元素或文本节点后代。
preceding-sibling::*[1]
选择第一个紧邻的元素。
[self::h3 = "Job Description"]
检查元素是<h3>
,并且其字符串值等于“ Job Description”。
/string()
返回上下文节点的字符串值。 对于您的示例内容,可以使用/descendant-or-self::text()
。 它通过选择上下文节点(如果是文本节点)和所有后代文本节点(如果是元素)来工作。 但是,如果将<div>
或<p>
更改为具有混合内容(即,子元素散布在文本节点中),则该表达式将返回一系列后代文本节点,而/string()
将它们串联在一起。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.