R 使用Xpath从类似节点获取值
我似乎无法获得正确的xpath来查询“a”标记包含h3。下面是示例代码R 使用Xpath从类似节点获取值,r,xpath,R,Xpath,我似乎无法获得正确的xpath来查询“a”标记包含h3。下面是示例代码 <div class="ncatstories"> <a href="valuehere"> <div class="img"></div> </a> <h5>valuehere</h5> <a href="valuehere"><h3>titlehere</h3></a> </div&g
<div class="ncatstories">
<a href="valuehere">
<div class="img"></div>
</a>
<h5>valuehere</h5>
<a href="valuehere"><h3>titlehere</h3></a>
</div>
其中data1是整个数据文档
然而,我得到了这个错误
XPath error : Invalid number of arguments
XPath error : Invalid expression
Error in xpathApply.XMLInternalDocument(doc, path, fun, ..., namespaces = namespaces, :
error evaluating xpath expression //div[@class='ncatstories' and ./a[contains(h3)]]
你知道我需要做什么吗?谢谢 正常过滤div,然后适当过滤锚标记,然后获取href
//div[@class='ncatstories']/a[count(h3)>0]/@href
我不是xpath极客,但是xpathsaply(doc,“//div[@class='ncatstories']/a/h3/。”、函数(x)c(xmlValue(x)、xmlAttrs(x)[“href”]])
?或者甚至concat(///124a/h3,“,///a[h3]/@href])
返回String='titleher | valuehere'
谢谢splash和lukeA!两者都解决了这个问题。当我试图在UseMethod(“xmlValue”)中运行代码错误时,我遇到了一个错误:“xmlValue”没有适用于“XMLAttributeValue”类对象的方法。我将只使用上面的@lukeA解决方案。谢谢你的帮助,布伦特!
xpathSApply(data1,"//div[@class='ncatstories' and ./a[contains(h3)]]", function(x) c(xmlValue(x), xmlAttrs(x)[["href"]]))
XPath error : Invalid number of arguments
XPath error : Invalid expression
Error in xpathApply.XMLInternalDocument(doc, path, fun, ..., namespaces = namespaces, :
error evaluating xpath expression //div[@class='ncatstories' and ./a[contains(h3)]]