我是处理 xml 和 xsd 内容的新手。我正在尝试使用 Guzzle 创建一个 Web 服务客户端:
http://guzzlephp.org/index.html
按照本教程,我能够创建一个非常基本的客户端和 1 个命令:
http://guzzlephp.org/tour/building_services.html
我遇到的问题是如何将返回的 XML 转换为更有用的格式。我正在使用的 Web 服务(及其文档和 xsd)可以在这里找到:
http://www.epo.org/searching/free/ops.html
我得到一个 SimpleXMLElement。假设 XML 内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/2.6.2/style/exchange.xsl"?>
<ops:world-patent-data xmlns:ops="http://ops.epo.org" xmlns="http://www.epo.org/exchange" xmlns:ccd="http://www.epo.org/ccd" xmlns:xlink="http://www.w3.org/1999/xlink">
<ops:meta name="elapsed-time" value="30"/>
<exchange-documents>
<exchange-document system="ops.epo.org" family-id="19768124" country="EP" doc-number="1000000" kind="A1">
<bibliographic-data>
<publication-reference>
<document-id document-id-type="docdb">
<country>EP</country>
<doc-number>1000000</doc-number>
<kind>A1</kind>
<date>20000517</date>
</document-id>
<document-id document-id-type="epodoc">
<doc-number>EP1000000</doc-number>
<date>20000517</date>
</document-id>
</publication-reference>
<!-- a lot of content snipped -->
</bibliographic-data>
</exchange-document>
</exchange-documents>
</ops:world-patent-data>
作为一个非常简单的示例,我如何提取文档编号(在上面 XML 的第 11 行)?
我尝试过:
$xpath = '/ops:world-patent-data/exchange-documents/exchange-document/bibliographic-data/publication-reference/document-id/doc-number';
$data = $result->xpath($xpath);
及其变体,但 $data 始终是空数组。
最佳答案
我相信您的问题与命名空间前缀的使用相当相关。我要做的(也是最佳实践)是确保在编写 xpath 之前注册自己的前缀和命名空间。这样,我将使用由我控制的前缀。
PHP 有一种奇怪的(在我看来)处理这个问题的方式,因为它允许“默认”方式,即尝试自行解析前缀。在您的情况下,此路径exchange-documents/exchange-document/bibliography-data/publication-reference/document-id/doc-number
中的标签实际上是合格的,因为 xmlns= “http://www.epo.org/exchange”
。如果您为此命名空间注册一个前缀,并在您的 XPath 中使用它,我相信您的东西会起作用。
看看这个 PHP help topic了解更多信息...
关于PHP Web 服务客户端 : Guzzle and xsd,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10482838/