(PHP)如何xpath查询xs:documentation的内部文本?

时间:2014-03-20 03:15:25

标签: php xpath xsd

如何xpath查询xs:documentation的内部文本?

$xsd = simplexml_load_file('my_xsd_file.xsd');

// I have tried:
$xsd->xpath("./xs:complexType[1]/xs:annotation/xs:documentation");

$xsd->xpath("./xs:complexType[1]/xs:annotation/xs:documentation/text()");

$xsd->xpath("./xs:complexType[1]/xs:annotation/xs:documentation[text()]");

以上所有都返回一个空对象。

更新

  • my_xsd_file.xsd 可在gist.github.com/drupalista-br/9666339
  • 找到
  • 打印此来电$xsd->getDocNamespaces();会返回:

    Array ( [ds] => http://www.w3.org/2000/09/xmldsig# [xs] => http://www.w3.org/2001/XMLSchema [] => http://www.portalfiscal.inf.br/nfe )

所以在我看来,命名空间已正确注册。

2 个答案:

答案 0 :(得分:1)

您还必须使用匹配的名称空间URI注册名称空间前缀xs

$xsd->registerXPathNamespace('xs', 'http://www.w3.org/2001/XMLSchema');

此外,您省略了根元素<xs:schema/>的第一个轴步骤。

$xsd->xpath("/xs:schema/xs:complexType[1]/xs:annotation/xs:documentation");

答案 1 :(得分:0)

显然PHP 5.4 xpath https://bugs.php.net/bug.php?id=62203

上存在错误

我在PHP 5.3上测试了我的代码,它运行得很好。