在DOMDocument中使用DOMXPath找到重定向META [英] find redirect META in DOMDocument with DOMXPath
问题描述
我有以下HTML:
< html>
< head>
< meta http-equiv =refreshcontent =0; URL = http://amazingjokes.com/>
< / head>
< / html>
我想用重定向找到META,所以我写了以下XPath查询:
/ html / head / meta [@ http-equiv =refresh]
pre>
但是,'http-equiv'中的' - '会导致错误:
无效的正则表达式:// html / head / meta [@ http-equiv =refresh] /:
中的顺序错误
字符类
我如何正确地重写xpath查询以找到元重定向?
我尝试了这个,当我从HTML代码中删除' - ',并且查询的内容按预期工作,但不幸的是'http-equiv'是一个设置的标准,所以我不能改变。这个实验显示我非常接近...
解决方案
然而,' - '在'http-equiv'导致错误:
正则表达式无效:// html / head / meta [@ http-equiv =refresh] /:
范围不合格的字符类
显然,您使用的XPath引擎是错误的。
问题中使用的XPath表达式是一个有效的XPath 1.0表达式,它会选择想要的
< meta>
元素。
这是一个基于XSLT的验证:
; xsl:stylesheet version =1.0xmlns:xsl =http://www.w3.org/1999/XSL/Transform>
< xsl:output omit-xml-declaration =yesindent =yes/>
< xsl:template match =/>
< xsl:copy-of select =/ html / head / meta [@ http-equiv ='refresh']/>
< / xsl:template>
< / xsl:stylesheet>
上述转换适用于提供的XML文档时: p>
< html>
< head>
< meta http-equiv =refreshcontent =0; URL = http://amazingjokes.com/>
< / head>
< / html>
评估XPath表达式,并在此评估节点中选择输出 :
< meta http-equiv =refreshcontent =0; URL = http://amazingjokes.com />
I have the following HTML:
<html> <head> <meta http-equiv="refresh" content="0;URL=http://amazingjokes.com" /> </head> </html>
I want to find the META with the redirect, so I wrote the following XPath query:
/html/head/meta[@http-equiv="refresh"]
However, the '-' in 'http-equiv' is causing an error:
Invalid regular expression: //html/head/meta[@http-equiv="refresh"]/: Range out of order in character class
How can I properly rewrite the xpath query to be able to find the meta redirect?
I experimented with this, when I remove the '-' from the HTML code and the query things work as expected, but unfortunately the 'http-equiv' is a set standard, so I can not change that. This experiment showed me I am very close...
解决方案However, the '-' in 'http-equiv' is causing an error:
Invalid regular expression: //html/head/meta[@http-equiv="refresh"]/: Range out of order in character class
Obviously, the XPath engine you are using is buggy.
The XPath expression used in the question is a valid XPath 1.0 expression and it selects the wanted
<meta>
element.Here is an 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="/html/head/meta[@http-equiv='refresh']"/> </xsl:template> </xsl:stylesheet>
When the transformation above is applied on the provided XML document:
<html> <head> <meta http-equiv="refresh" content="0;URL=http://amazingjokes.com" /> </head> </html>
the XPath expression is evaluated and the selected in this evaluation node is output:
<meta http-equiv="refresh" content="0;URL=http://amazingjokes.com"/>
这篇关于在DOMDocument中使用DOMXPath找到重定向META的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!