获取javascript参数的XPath查询

我刚开始在php中使用XPath查询,而且很多都是不言自明的我担心如果 可以获取onclick()函数的值。 </ p>

示例</ p>

 &lt; a id =“moreButton”onclick =“showMore('#content','/ page / 2  ');“&gt;显示更多&lt; / a&gt; 
</ code> </ pre>

我提出的最好的是 // a [@ id ='moreButton' ] / @ onclick </ code>返回 showMore('#content','/ page / 2'); </ code> </ p>

我可以进一步优化它吗? 返回 / page / 2 </ code>还是我只需要使用php来解析我的内容?</ p>
</ div>

展开原文

原文

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?

douyou8266
douyou8266 实际上,可以通过评估XPath表达式来生成两个想要的参数。:)
8 年多之前 回复

2个回答



你必须用别的东西解析它,XPath不懂JavaScript </ p>
</ div>

展开原文

原文

You must parse it with something else, XPath doesn't understand JavaScript

duandeng2265
duandeng2265 好的,只是想知道XPath可以遍历多远。 谢谢!
8 年多之前 回复



实际上,通过评估以下XPath表达式</ strong>可以生成 </ em>:< / p>

对于 onclick </ code>属性中指定的函数的第一个参数:</ strong> </ p>

  substring-before(substring-after(/ * / @ onclick,'('),',')
</ code> </ pre>

对于第二个参数 onclick </ code>属性中指定的函数:</ strong> </ p>

  substring-before(
substring-after(substring-after(/ * / @) onclick,'('),','),
')'

</ code> </ pre>

基于XSLT的验证</ strong>:< / p>

 &lt; xsl:stylesheet version =“1.0”
xmlns:xsl =“http://www.w3.org/1999/XSL/Transform”&gt;
&lt; xsl:output omit-xml-declaration =“yes”indent =“yes”/&gt;

&lt; xsl:template match =“/”&gt;
&lt; xsl:copy-of select = \ n“substring-before(substring-after(/ * / @ onclick,'('),',')
”/&gt;
===========

&lt; XSL:复制, select =
“substring-before(
substring-after(substring-after(/ * / @ onclick,'('),','),
')'

”/&gt; \ n
&lt; / xsl:template&gt;
&lt; / xsl:stylesheet&gt;
</ code> </ pre>

在提供的XML文档上应用此转换时:< / strong> </ p>

 &lt; a id =“moreButton”
onclick =“showMore('#content','/ page / 2');”&gt; Show More&lt; ; / a&gt;
</ code> </ pre>

评估两个XPath表达式,并将此评估结果复制到输出中:</ strong> </ p>

 '#content' 
===========

'/ page / 2'
</ code> </ pre>
< / DIV>

展开原文

原文

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'

立即提问
相关内容推荐