I've just started using XPath queries in php and while a lot is very self-explanatory I'm wordering if it's possible to get the values of an onclick() function.
An example
<a id="moreButton" onclick="showMore('#content','/page/2');">Show More</a>
The best I have come up with is //a[@id='moreButton']/@onclick
which returns showMore('#content','/page/2');
Can I further refine it to only return /page/2
or will I just have to use php to parse what I have?
You must parse it with something else, XPath doesn't understand JavaScript
Actually, this can be produced by evaluating the following XPath expressions:
For the first argument of the function specified in the onclick
attribute:
substring-before(substring-after(/*/@onclick, '('), ',')
For the second argument of the function specified in the onclick
attribute:
substring-before(
substring-after(substring-after(/*/@onclick, '('), ','),
')'
)
XSLT - based verification:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:copy-of select=
"substring-before(substring-after(/*/@onclick, '('), ',')
"/>
===========
<xsl:copy-of select=
"substring-before(
substring-after(substring-after(/*/@onclick, '('), ','),
')'
)
"/>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the provided XML document:
<a id="moreButton"
onclick="showMore('#content','/page/2');">Show More</a>
the two XPath expressions are evaluated and the result of this evaluation is copied to the output:
'#content'
===========
'/page/2'