2012-05-07 60 views
2

我是使用xml和xsd工作的新手。我想用暴食来创建Web服务客户端:PHP Web服务客户端:Guzzle和xsd

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> 

我该如何非常简单地提取doc-number(在上面的XML的第11行)?

我想:它

$xpath = '/ops:world-patent-data/exchange-documents/exchange-document/bibliographic-data/publication-reference/document-id/doc-number'; 
$data = $result->xpath($xpath); 

和变化,但$的数据始终是空数组。

回答

1

我相信你的问题与使用命名空间前缀有关。我会做什么(作为最佳做法)是确保在写入我的xpath之前注册我自己的前缀和名称空间。这样,我会使用由我控制的前缀。

PHP有一个奇怪的(在我看来)处理这种方式,因为它允许“默认”的方式,即试图自行解决前缀。在你的情况下,由于xmlns="http://www.epo.org/exchange",此路径exchange-documents/exchange-document/bibliographic-data/publication-reference/document-id/doc-number中的标签实际上是合格的。如果你要为这个命名空间注册一个前缀,并在你的XPath中使用它,我相信你的东西会起作用。

看看这个PHP help topic更多地了解它...

+0

跟着链接并补充说:$ result-> registerXPathNamespace( '交流', 'http://www.epo.org/exchange' );路径//交换:书目数据/出版物参考/文档编号/文档编号不起作用,但只是使用// exchange:doc-number作为Marc B建议的作品。我很困惑......编辑:好的第一条路径必须是//交换:书目数据/交换:出版物参考/交换:文档编号/交换:文件编号。现在有意义,我想到它,但有点详细... –

+0

你还应该看看[Symfony2的CssSelector组件](http://symfony.com/doc/current/components/css_selector.html)它是用来生成XPath为你。希望这可以帮助。 – renoirb

1

XPath允许这样的:

//document-id 

这将返回所有document-id节点,无论他们在哪里DOM。除非您确实需要确定某物的位置,否则不必指定绝对的/.../.../.../类型路径。