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

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

问题描述

请有人解释一下如何遍历 this xml 以便 echo 所有带有 d 的元素: 前缀.

下面的作品,所以我在正确的路线:

$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 ) {回声 $name, '=', $x, "
";}}

但我正在努力处理 <content> 元素.

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

请有人帮我调试一下吗?

解决方案

你看错了.实际的命名空间 (http://schemas.microsoft.com/ado/2007/08/dataservices) 就是您所知道的.前缀特定于元素及其后代 - 直到它被重新定义.它对于元素节点也是可选的.以下树元素都具有相同的命名空间:

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

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

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

$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('原子','http://www.w3.org/2005/Atom');$element->registerXpathNamespace('元','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], "
";echo $properties->xpath('data:NEW_DATE')[0], "

";}

输出:

62572015-01-02T00:00:0062582015-01-05T00:00:0062592015-01-06T00:00:0062602015-01-07T00:00:0062612015-01-08T00:00:00...

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

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

$document = new DOMDocument();$document->loadXml($xml);$xpath = 新 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), "
";echo $xpath->evaluate('string(data:NEW_DATE)', $properties), "

";}

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, "
";
         }
}

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

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

Please could someone help me debug this?

解决方案

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"/>

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.

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], "
";
  echo $properties->xpath('data:NEW_DATE')[0], "

";
}

Output:

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
...

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.

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), "
";
  echo $xpath->evaluate('string(data:NEW_DATE)', $properties), "

";
}

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

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