3

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.

5
  • can you refer to //span... element instead of //*... ? Commented Nov 7, 2015 at 23:30
  • yes Mr. V, I gave that a shot too, no difference! Commented Nov 7, 2015 at 23:35
  • Please provide more context. Also note that the fragment you provided isn't well-formed: itemscope has no value and the span isn't closed. Commented Nov 7, 2015 at 23:37
  • ok I will add more info...also, I agree, itemscope don't have the value, but I didn't write the code and there's nothing I can do to change it... Commented Nov 7, 2015 at 23:39
  • you will not able to use xPathes without having proper xml. You should use external libraries that allow to process invalid xmls. Commented Nov 7, 2015 at 23:44

1 Answer 1

1

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"'
Sign up to request clarification or add additional context in comments.

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.