Jump to content

Xpath Question: Returning a URL from an onclick node


Recommended Posts

I apologize if this is the wrong forum, just wasn't sure where to put this question. I'm trying to return the url from this onclick node: <a onclick=”javascript:getURLWin(‘http://some.web.url/some/path/index.html’); href=”javascript:void(0);”>; here is the block of text which describes the page </a> So far, I've tried td[2]/a/[@onclick=”javascript:getURLWin(‘”;] , and that hasn't worked. Can anyone offer suggestions? Thanks

Link to comment
Share on other sites

Remove the slash before the predicate, and remove the ";", i.e. instead of


make that


However, it's important to note that, as written, this would not match "javascript:void(0)". It would only match an "a" element that looks like

<a onclick="javascript:getURLWin('">

and that's probably not what you want.I'm guessing you want the attribute that starts with that, which you can do like:

td[2]/a[starts-with(@onclick, "javascript:getURLWin('")]/@onclick

  • Like 1
Link to comment
Share on other sites

Yes. Surround the whole expression with substring-after(). e.g.

substring-after(td[2]/a[starts-with(@onclick, "javascript:getURLWin('")]/@onclick)

I assume you'll also want to get rid of the trailing ")". That's a little difficult to get with just XPath 1.0. The best you can do is just remove the last N characters, assuming that they're always that many. For performance, you might want to store the full result before you cut it. If you're using this XPath in XSLT, you can use xsl:variable for that, e.g.

<xsl:variable name="url" select="substring-after(td[2]/a[starts-with(@onclick, "javascript:getURLWin('")]/@onclick)" /><xsl:value-of select="substring($url, 0, string-length($url)-2)" />

Link to comment
Share on other sites

  • 3 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...