带命名空间的 xpath [英] xpath with namespace

查看:30
本文介绍了带命名空间的 xpath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在带有 xml 文件的 php SimpleXML 中使用 xpath,其中以下是相关片段:-

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.

在我的 php 代码中,我有以下内容,它注册了命名空间,然后使用 xpath 来获取 CodeLists:-$xml->registerXPathNamespace('test','http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message');

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');

$codelistspath = $xml->xpath('test:CodeLists');

我希望能够使用 xpath 在树中降低一级,即到 CodeList 并认为以下方法可行:-

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');

$codelistpath = $xml->xpath('test:CodeLists/CodeList');

但它只会产生一个空数组.我找不到使用 xpath 访问文档中其他任何内容的方法.我已经花了几个小时试图解决这个问题,所以任何帮助将不胜感激.

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.

推荐答案

CodeList 元素属于从 继承的默认命名空间; 元素 - URI 为 http://www.SDMX.org/resources/SDMXML/schemas/v2_0/structure 的命名空间.

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.

您还需要使用 registerXPathNamespace() 注册它.

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');

这篇关于带命名空间的 xpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆