Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 基本的DOMXpath,可能是错误的吗?(re:始终检查输入名称空间)_Php_Xpath_Domdocument - Fatal编程技术网

Php 基本的DOMXpath,可能是错误的吗?(re:始终检查输入名称空间)

Php 基本的DOMXpath,可能是错误的吗?(re:始终检查输入名称空间),php,xpath,domdocument,Php,Xpath,Domdocument,我在类中使用了一个内部domDocument,$this->doc->dom,我认为这是可以的,因为$this->doc->dom->saveXML()可以工作,并显示我的XML,比如 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equi

我在类中使用了一个内部domDocument,
$this->doc->dom
,我认为这是可以的,因为
$this->doc->dom->saveXML()可以工作,并显示我的XML,比如

  <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
      <title>04</title>
      <link href="css/04.css" rel="stylesheet" type="text/css"/>
    </head>
   ...
没有报告错误。。。但是,没有元素(!)

显示0(零)这就是问题所在,对我来说是一个DomDocument错误:
元素就在那里


编辑以添加更多线索

当我对
getElementsByTagName()
做类似的事情时,它会工作(!),所以
$this->doc->dom
并没有问题

 $test = $this->doc->dom->getElementsByTagName('link');
 print $test->length; // OK, not zero, returns 1!
它不是一个“DOMDocumentBug”

简单解决方案 合并已发布的评论

注册名称空间 (@PaulT-response) 根(
html
tag)声明了一个命名空间,
xmlns=”http://www.w3.org/1999/xhtml“
。 使用
registerNamespace()
可以使用任意昵称(xx)注册它,然后执行正确的查询

$xpath->registerNamespace('xx', "http://www.w3.org/1999/xhtml"); 
$xpath->query('//xx:link');
从根目录中删除命名空间属性 我需要过滤我的输入,所以它改为

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<html>
 <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>04</title>
    <link href="css/04.css" rel="stylesheet" type="text/css"/>
 </head>
...
</html>

04
...
现在正如我所期望的那样工作,不需要“该死的名称空间”

调试XPath (@RolandoIsidoro回答)
遇到这种情况时,请尝试使用类似的工具。 在您的示例中,它会抛出一个错误,导致您找到解决方案:

XPath查询的默认(无前缀)命名空间URI始终为“” 它不能被重新定义为“

它不是一个“DOMDocumentBug”

简单解决方案 合并已发布的评论

注册名称空间 (@PaulT-response) 根(
html
tag)声明了一个命名空间,
xmlns=”http://www.w3.org/1999/xhtml“
。 使用
registerNamespace()
可以使用任意昵称(xx)注册它,然后执行正确的查询

$xpath->registerNamespace('xx', "http://www.w3.org/1999/xhtml"); 
$xpath->query('//xx:link');
从根目录中删除命名空间属性 我需要过滤我的输入,所以它改为

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<html>
 <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>04</title>
    <link href="css/04.css" rel="stylesheet" type="text/css"/>
 </head>
...
</html>

04
...
现在正如我所期望的那样工作,不需要“该死的名称空间”

调试XPath (@RolandoIsidoro回答)
遇到这种情况时,请尝试使用类似的工具。 在您的示例中,它会抛出一个错误,导致您找到解决方案:

XPath查询的默认(无前缀)命名空间URI始终为“” 它不能被重新定义为“


不确定DOMXPath()如何工作,但您应该尝试注册
“http://www.w3.org/1999/xhtml“
命名空间与
$xpath->registerNamespace('xhtml',”http://www.w3.org/1999/xhtml");$xpath->query('//xhtml:link')是(!),现在血腥的查询正在运行!遇到这种情况时,请尝试使用类似的工具。在您的示例中,它抛出了一个可能导致您找到解决方案的错误:
XPath查询的默认(无前缀)命名空间URI始终为“”,并且不能重新定义为“http://www.w3.org/1999/xhtml“。
。不确定DOMXPath()如何工作,但您应该尝试注册
”http://www.w3.org/1999/xhtml"
名称空间和
$xpath->registerNamespace('xhtml',”http://www.w3.org/1999/xhtml");$xpath->query('//xhtml:link')是(!),现在血腥的查询正在运行!遇到这种情况时,请尝试使用类似的工具。在您的示例中,它抛出了一个可能导致您找到解决方案的错误:
XPath查询的默认(无前缀)命名空间URI始终为“”,并且不能重新定义为“http://www.w3.org/1999/xhtml“。