I’m new to working with xml and xsd stuff. I’m trying to create a web service client using Guzzle:
http://guzzlephp.org/index.html
I was able to create a very basic client and 1 command following this tutorial:
http://guzzlephp.org/tour/building_services.html
The issue I have is how get the returned XML into a more useful format. The web service I’m using (and its documentation and xsd) can be found here:
http://www.epo.org/searching/free/ops.html
I get a SimpleXMLElement. Assume following content of the 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>
How can I as very simple example extract the doc-number (on Line 11 of the XML above)?
I tried:
$xpath = '/ops:world-patent-data/exchange-documents/exchange-document/bibliographic-data/publication-reference/document-id/doc-number';
$data = $result->xpath($xpath);
and variations of it but $data is always and empty array.
I believe that your problem is rather related to the use of namespace prefixes. What I would do (as a best practice as well) is to make sure I register my own prefixes and namespaces before writing my xpath. This way, I would be using the prefixes as controlled by me.
PHP has a weird (in my opinion) way of dealing with this, since it allows a “default” way i.e. is trying to resolve by itself the prefixes. In your case, the tags in this path
exchange-documents/exchange-document/bibliographic-data/publication-reference/document-id/doc-numberactually are qualified, due toxmlns="http://www.epo.org/exchange". If you would register a prefix for this namespace, and use it in your XPath, I am sure your stuff will work.Take a look at this PHP help topic to understand more about it…