Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xpath: extract only class that start with [duplicate]

Tags:

html

r

xml

xpath

I have a html that looks like:

<div class="date_s">May 16, 2018</div>
<div class="date_a">May 17, 2018</div>
<div class="date_g23">May 18, 2018</div>

I can extract a specific div class with:

XML::xpathSApply(XML::htmlParse(myurl), "//div[@class='date_s']", XML::saveXML)

How can I extract all div class that start with 'date' ?

I have tried in vain (returns an empty list):

XML::xpathSApply(XML::htmlParse(myurl), "//div[starts-with(name(), 'date')]", XML::saveXML)
like image 642
RockScience Avatar asked Feb 21 '26 02:02

RockScience


1 Answers

Your code is basically correct, just use @class instead of name():

XML::xpathSApply(XML::htmlParse(myurl), 
                 "//div[starts-with(@class, 'date')]", XML::saveXML)

[1] "<div class=\"date_s\">May 16, 2018</div>"  
[2] "<div class=\"date_a\">May 17, 2018</div>"  
[3] "<div class=\"date_g23\">May 18, 2018</div>"
like image 171
andrew_reece Avatar answered Feb 22 '26 17:02

andrew_reece



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!