【发布时间】:2012-03-22 17:45:14
【问题描述】:
我正在尝试将 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('test','http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message');
$codelistspath = $xml->xpath('test:CodeLists');
我希望能够使用 xpath 在树中降低一层,即到 CodeList,并认为以下方法可行:-
$codelistpath = $xml->xpath('test:CodeLists/CodeList');
但它只会产生一个空数组。我找不到使用 xpath 访问文档中其他任何内容的方法。我花了几个小时试图解决这个问题,所以任何帮助将不胜感激。
【问题讨论】: