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
R 使用Xpath从类似节点获取值_R_Xpath - Fatal编程技术网

R 使用Xpath从类似节点获取值

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

我似乎无法获得正确的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>
其中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)]]