10

I am new to XPath, but I can see how powerful it is. I am looking at the source code of this link and simply want to extract the contents and username from the following two pieces of the page, which for simplicity sake are located near the top of the source code.

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

<div class="username">Simburgur</div>

Here is my code within R:

doc <- htmlParse("http://forums.epicgames.com/archive/index.php/t-672775.html")
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= but am stuck and can not seem to get my expression to return the string I want.

I repeat. I am new to XPath. :)

1
  • Good question, +1. See my answer for two short and efficient solutions. :) Commented Feb 2, 2011 at 4:19

2 Answers 2

10

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.

Always avoid using // if the structure of the XML document is statically known .

Sign up to request clarification or add additional context in comments.

Comments

3

You're close. This should do it.

//head/meta[@name=\"description\"]/@content

The brackets are constraining the choice of meta tags, but you still have to specify the attribute you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.