Retrieving XML values from namespace elements with PHP(使用 PHP 从命名空间元素中检索 XML 值)
问题描述
请有人解释一下如何遍历 <entry> 标记interest-rates/pages/XmlView.aspx?data=yieldyear&year=2015" rel="nofollow">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 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 PHP 从命名空间元素中检索 XML 值
基础教程推荐
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01