使用PHP从名称空间元素中检索XML值 [英] Retrieving XML values from namespace elements with PHP

查看:79
本文介绍了使用PHP从名称空间元素中检索XML值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请有人说明如何在<entry>标记之间循环. aspx?data = yieldyear& year = 2015"rel =" nofollow>此 xml,以便echo所有带有d:前缀的元素.

Please could someone explain how to loop through the <entry> tags in this xml in order to echo all the elements with a d: prefix.

以下内容适用,所以我在正确的位置:

The below works, so I am on the right lines:

$url = "http://www.treasury.gov/resource-center/data-chart-center/interest-rates/pages/XmlView.aspx?data=yieldyear&year=2015";
$xml = simplexml_load_file($url);

foreach( $xml->entry as $entry ) {

   $element = $xml->entry->children();
        foreach( $element as $name=>$x ) {
             echo $name, '=', $x, "\n";
         }
}

但是我在<content>元素上苦苦挣扎.

But I'm struggling with the <content> element.

当我将$element更新为= $xml->entry->content->children();时,它不起作用.

When i update $element to = $xml->entry->content->children();, it doesn't work.

请有人帮我调试一下吗?

Please could someone help me debug this?

推荐答案

您正在以错误的方式看待这个问题.实际的名称空间(http://schemas.microsoft.com/ado/2007/08/dataservices)是您所知道的.前缀是特定于元素及其后代的,直到重新定义为止.它对于元素节点也是可选的.以下所有树元素均具有相同的名称空间:

You're looking at this the wrong way. The actual namespace (http://schemas.microsoft.com/ado/2007/08/dataservices) is what you know. The prefix is specific for an element and its descendants - until it is redefined. It is optional for element nodes, too. The following tree elements all have the same namespace:

  • <f:foo xmlns:f="urn:foo"/>
  • <bar:foo xmlns:bar="urn:foo"/>
  • <foo xmlns="urn:foo"/>
  • <f:foo xmlns:f="urn:foo"/>
  • <bar:foo xmlns:bar="urn:foo"/>
  • <foo xmlns="urn:foo"/>

每个元素都解析为名称空间urn:foo中名称为foo的节点.您可以将其读取为{urn:foo}foo.

Each of the elements resolves to a node with the name foo inside the namespace urn:foo. You can read it as {urn:foo}foo.

如果您查看方法的文档,这里有许多将实际名称空间作为参数,因此您不必知道前缀.对于XPath,您需要注册自己的前缀:

If you check the documentation for the methods, here are many that have the actual namespace an argument, so you do not have to know the prefix. For XPath you register your own prefix:

$url = "http://www.treasury.gov/resource-center/data-chart-center/interest-rates/pages/XmlView.aspx?data=yieldyear&year=2015";
$element = simplexml_load_file($url);

$element->registerXPathNamespace(
  'atom', 'http://www.w3.org/2005/Atom'
);
$element->registerXpathNamespace(
  'meta', 'http://schemas.microsoft.com/ado/2007/08/dataservices/metadata'
);

foreach ($element->xpath('//atom:entry/atom:content/meta:properties') as $properties) {
  $properties->registerXpathNamespace('data', 'http://schemas.microsoft.com/ado/2007/08/dataservices');
  echo $properties->xpath('data:Id')[0], "\n";
  echo $properties->xpath('data:NEW_DATE')[0], "\n\n";
}

输出:

6257
2015-01-02T00:00:00

6258
2015-01-05T00:00:00

6259
2015-01-06T00:00:00

6260
2015-01-07T00:00:00

6261
2015-01-08T00:00:00
...

您将必须在要调用xpath()的元素上注册前缀.在示例中,我使用了不同的前缀,因此您可以看到不必知道前缀,而只需知道名称空间本身.

You will have to register your prefixes on the element you like to call xpath() on. I used different prefixes in the example so you can see that you don't have to know the prefix - only the namespace itself.

在DOM中,它是一个单独的对象,并且DOMXpath:evaluate()可以直接返回标量值.

In DOM it an separate object and DOMXpath:evaluate() can return scalar values directly.

$document = new DOMDocument();
$document->loadXml($xml);
$xpath = new DOMXpath($document);
$xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
$xpath->registerNamespace('meta', 'http://schemas.microsoft.com/ado/2007/08/dataservices/metadata');
$xpath->registerNamespace('data', 'http://schemas.microsoft.com/ado/2007/08/dataservices');

foreach ($xpath->evaluate('//atom:entry/atom:content/meta:properties') as $properties) {
  echo $xpath->evaluate('string(data:Id)', $properties), "\n";
  echo $xpath->evaluate('string(data:NEW_DATE)', $properties), "\n\n";
}

这篇关于使用PHP从名称空间元素中检索XML值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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