简体   繁体   English

XPath:选择文本之后

[英]XPath : Select Text After

I am unable to construct an XPath expression to scrape the text between the <br> tags.我无法构建 XPath 表达式来抓取<br>标记之间的文本。 Any help would be greatly appreciated:任何帮助将不胜感激:

Webpage:网页:

<hr>
<center>
<a href="http://www.somewebsite.html" target="_blank">Description</a>
</center>
<br>
SEC 10 TWP 20 RGE 30
<br>
PLAT BOOK A PAGE 200
<br>
BLK 10 LOTS 1.4.5.6.7.8. EX
<br>
POSSIBLY MORE TEXT...
<br>
<hr>

Copy XPath Result for <href> :复制<href> XPath 结果:

//*[@id="prc"]/table/tbody/tr/td/center[6]/a

Javascript: Javascript:

var Xpath = 'substring-after(//*[@id="prc"]/table/tbody/tr/td/center[6]/a, "Description")';

var parser = new DOMParser();
var doc = parser.parseFromString(html, 'text/html');

//Property Description
var result = doc.evaluate(Xpath, doc, null, XPathResult.STRING_TYPE, null);
Description = result.stringValue;

//Display Message
alert("Description Search Results: " + Description);

Desired Result for Display Message:显示消息的期望结果:

SEC 10 TWP 20 RGE 30
PLAT BOOK A PAGE 200
BLK 10 LOTS 1.4.5.6.7.8. EX
POSSIBLY MORE TEXT...

As there are various text nodes XPath 1.0 is not powerful enough to give you a single string result with one expression.由于有各种文本节点,XPath 1.0 不够强大,无法通过一个表达式为您提供单个字符串结果。 Using SaxonJS.XPath you could however use the string-join XPath 2.0 and later function:但是,使用 SaxonJS.XPath 您可以使用string-join XPath 2.0 和更高版本的函数:

 const html = `<hr> <center> <a href="http://www.somewebsite.html" target="_blank">Description</a> </center> <br> SEC 10 TWP 20 RGE 30 <br> PLAT BOOK A PAGE 200 <br> BLK 10 LOTS 1.4.5.6.7.8. EX <br> POSSIBLY MORE TEXT... <br> <hr>`; const doc = new DOMParser().parseFromString(html, 'text/html'); alert(SaxonJS.XPath.evaluate("string-join(//center[a[. = 'Description']]/following-sibling::text()/normalize-space(), '\\n')", doc, { xpathDefaultNamespace : 'http://www.w3.org/1999/xhtml' }))
 <script src="https://www.saxonica.com/saxon-js/documentation/SaxonJS/SaxonJS2.rt.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM