tumultous_rooster
tumultous_rooster

Reputation: 12550

Extracting information from HTML using XPath

I have a snippet of html which I extracted from the source of a webpage I'm working on:

<span itemprop="homeLocation" itemscope itemtype="http://schema.org/Place"><meta itemprop="name" content="Kansas"/>

...and I'd like to extract the location, Kansas from it, using Xpath.

Using an Xpath checker, I have been testing this but to no avail.

I tried

//*[@itemprop="homeLocation"]/meta[@itemprop="name"]/@content

and similar attempts, but can't seem to get a match. I don't understand what I'm doing wrong.

Any advice would be greatly appreciated.

Upvotes: 3

Views: 3771

Answers (1)

vvg
vvg

Reputation: 6385

Your xPath is absolutely valid. The problems are with xml.

  1. Close span tag.
  2. Set some value for itemscope attribute.

And the most important. xPath checker your are trying to use seems to have some bugs. Check this one: http://www.freeformatter.com/xpath-tester.html#ad-output

Xml I've used:

    <span 
      itemprop="homeLocation"
      itemscope=""
      itemtype="http://schema.org/Place">
             <meta itemprop="name" content="Kansas"/>
  </span>

Result:

Attribute='content="Kansas"'

Upvotes: 1

Related Questions