find redirect META in DOMDocument with DOMXPath

和自甴很熟 提交于 2019-12-14 03:34:16

问题


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...


回答1:


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"/>


来源:https://stackoverflow.com/questions/40413746/find-redirect-meta-in-domdocument-with-domxpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!