我正在尝试在 php SimpleXML 中使用 xpath 和 xml 文件,其中以下是相关片段:-
<?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.
在我的 php 代码中,我有以下内容,它注册名称空间,然后使用 xpath 获取 CodeLists:-
$xml->registerXPathNamespace('测试','http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message');
$codelistspath = $xml->xpath('test:CodeLists');
我希望能够使用 xpath 在树中进入下一级,即 CodeList 并认为以下内容可行:-
$codelistpath = $xml->xpath('测试:CodeLists/CodeList');
但它只是产生一个空数组。我找不到使用 xpath 访问文档中其他任何内容的方法。我花了几个小时试图解决这个问题,所以任何帮助将不胜感激。
The CodeList
元素属于默认命名空间继承自<message:Structure>
element - URI 为的命名空间http://www.SDMX.org/resources/SDMXML/schemas/v2_0/structure
.
您需要将其注册为registerXPathNamespace()
以及。
$xml->registerXPathNamespace('default', 'http://www.SDMX.org/resources/SDMXML/schemas/v2_0/structure');
$codelistpath = $xml->xpath('test:CodeLists/default:CodeList');
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)