How To Use XPath within R

xpathSApply is a version of xpathApply which attempts to simplify the result if it can be converted to a vector or matrix rather than left as a list. In this way, it has the same relationship to xpathApply as sapply has to lapply.


XPath. (source: tech.amikelive.com)


My code within R:


R> doc <- htmlParse("http://forums.epicgames.com/archive/index.php/t-672775.html")
R> xpathSApply(doc, "//head/meta[@name=\"description\"]") 

# which returns
[[1]]
<meta name="description" content="[Archive]  Simburgur's Live Stream [Offline] Gears of War 3" />


Obviously, in this example, all I want is what is inside the quotes of content=




Use:

/*/head/meta[@name='description']/@content

This still selects an attribute node, but probably there is an easy way in your PL to get the string value of the attribute.



To get just the string value, use:


string(/*/head/meta[@name='description']/@content)


Do note: Using the // abbreviation may result in very slow evaluation of the XPath expression, because it may cause a linear traversal of a whole (sub)tree.



Result

[Archive] Simburgur's Live Stream [Offline] Gears of War 3

SHARE

About Agung Pambudi

I am a Blogger with passion in business.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment