Jump to content
Sign in to follow this  
shadowplay

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

Share this post


Link to post
Share on other sites

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

td[2]/a/[@onclick="javascript:getURLWin('";]

make that

td[2]/a[@onclick="javascript:getURLWin('"]

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

Share this post


Link to post
Share on other sites

I'm sorry for not getting back sooner. I appreciate your response. When I tried this, we seem to get the url, but we also get the 'javascript:getURLWin(', that precedes this. Is there a way to bascially cut the string to only return what comes after the parenthesis? Thanks

Share this post


Link to post
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)" />

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...