I had tough time dealing with XPath and single quote. Though W3C recommends using ' to escape it but I never got it working, let me know if any of you get through.
Came across this blog and found that “concat” could be used in this situation.
So original XPath expression is –
//meta[@name=’DESCRIPTION’][@content=’Tester’s Test’]
This is some thing which certainly fails as single quote in “Tester’s” marks it at end of string and then XPath blows up, next trial was –
//meta[@name=’DESCRIPTION’][@content=’Tester''s Test’]
This does not work despite w3c recommendation!
And then I used concat function and split the string as –
concat(‘Tester’,”’”,’s Test’)
NOTICE that single quote is kept in double quote while other characters are kept in single quotes.
So XPath looks as –
//meta[@name=’DESCRIPTION’][@content=concat(‘Tester’,”’”,’s Test’)]
And this works charm.
Thanks for the post, it's helping me out.
ReplyDeleteI'm trying to write a similar Xpath, in Google Docs, to pull only the meta descriptions off web pages.
I feel your example is getting me close...but I'm not there yet!
Any ideas on what it would look like?
Thanks!
Glad you liked it.
ReplyDeleteFor meta tags you could use XPath like -
//title[text() = 'title text']
//meta[@name='keywords'][@content='jobs, web, testing']
But I suggest to use CSS insead..