Reputation: 137
I am trying to use xpath in php SimpleXML with an xml file, of which the following is a relevant fragment:-
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
- <!-- Created on 21-Mar-2012 10:30:46
-->
- <message:Structure xmlns="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/structure" xmlns:message="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message" xsi:schemaLocation="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/structure http://www.sdmx.org/docs/2_0/SDMXStructure.xsd http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message http://www.sdmx.org/docs/2_0/SDMXMessage.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <Header xmlns="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message">
<ID>none</ID>
<Test>false</Test>
<Truncated>false</Truncated>
<Prepared>2011-11-18T13:56:45</Prepared>
- <Sender id="OECD">
<Name xml:lang="en">Organisation for Economic Co-operation and Development</Name>
<Name xml:lang="fr">Organisation de coopération et de développement économiques</Name>
</Sender>
</Header>
- <message:CodeLists>
- <CodeList id="CL_MEI_OBS_STATUS" agencyID="OECD">
<Name xml:lang="en">Observation Status</Name>
<Name xml:lang="fr">Statut d'observation</Name>
- <Code value="B">
<Description xml:lang="en">Break</Description>
<Description xml:lang="fr">Rupture</Description>
</Code>
etc. etc.
In my php code I have the following, which registers the namespace then uses xpath to obtain CodeLists:- $xml->registerXPathNamespace('test','http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message');
$codelistspath = $xml->xpath('test:CodeLists');
I would like to be able to use xpath to go one level lower in the tree, i.e. to CodeList and thought the following would work:-
$codelistpath = $xml->xpath('test:CodeLists/CodeList');
But it just produces an empty array. I can find no way of accessing anything else in the document with xpath. I have spent hours trying to solve this, so any help would be greatly appreciated.
Upvotes: 2
Views: 919
Reputation: 51970
The CodeList
elements belong to the default namespace inherited from the <message:Structure>
element - the namespace whose URI is http://www.SDMX.org/resources/SDMXML/schemas/v2_0/structure
.
You will need to register that with registerXPathNamespace()
as well.
$xml->registerXPathNamespace('default', 'http://www.SDMX.org/resources/SDMXML/schemas/v2_0/structure');
$codelistpath = $xml->xpath('test:CodeLists/default:CodeList');
Upvotes: 2
Reputation: 13766
It looks like registerXPathNamespace only works for the next xpath query (according to the documentation)... so if you've run $xml->xpath('test:CodeLists')
already, try registering the namespace again before running $xml->xpath('test:CodeLists/CodeList')
.
Upvotes: 0