gpt4 book ai didi

php - 如何在不使用 PHP 进行迭代的情况下按属性选择 xml 元素?

转载 作者:可可西里 更新时间:2023-11-01 01:02:58 24 4
gpt4 key购买 nike

我有来自 http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml 的 XML

<?xml version="1.0" encoding="UTF-8"?>
<gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
<gesmes:subject>Reference rates</gesmes:subject>
<gesmes:Sender>
<gesmes:name>European Central Bank</gesmes:name>
</gesmes:Sender>
<Cube>
<Cube time='2013-08-23'>
<Cube currency='USD' rate='1.3355'/>
<Cube currency='GBP' rate='0.85910'/>
<Cube currency='HUF' rate='298.98'/>
</Cube>
</Cube>
</gesmes:Envelope>

(出于演示目的,我删除了一些值)

我想使用 PHP 获取转换率,比方说 GBP。

HI 可以像这样使用 simplexml 加载它:

$XML=simplexml_load_file("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");

foreach($XML->Cube->Cube->Cube as $rate){
...

但我想在不迭代的情况下获取值,而且我真的不想使用正则表达式...

我试过类似的方法,但没有用:

$sxe=simplexml_load_file("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");

$sxe->registerXPathNamespace('c', 'http://www.gesmes.org/xml/2002-08-01');
$result = $sxe->xpath('//c:Cube[@currency="USD"]');

最佳答案

Cube 元素不在 http://www.gesmes.org/xml/2002-08-01 命名空间中,它的前缀是 gesmes,它们在默认命名空间中,即 http://www.ecb.int/vocabulary/2002-08-01/eurofxref

因此而不是:

$sxe->registerXPathNamespace('c', 'http://www.gesmes.org/xml/2002-08-01');
$result = $sxe->xpath('//c:Cube[@currency="USD"]');

您需要注册其他命名空间:

$sxe->registerXPathNamespace('c', 'http://www.ecb.int/vocabulary/2002-08-01/eurofxref');
$result = $sxe->xpath('//c:Cube[@currency="USD"]');

这里是 a live demo showing a result count of 1使用更正后的命名空间。


为了尝试给出一个权威的解释,请考虑以下来自 Namespaces in XML 的摘录。规范:

The Prefix provides the namespace prefix part of the qualified name, and MUST be associated with a namespace URI reference in a namespace declaration [...] Note that the prefix functions only as a placeholder for a namespace name.

The expanded name corresponding to a prefixed element or attribute name has the URI to which the prefix is bound as its namespace name [...]

A default namespace declaration applies to all unprefixed element names within its scope.

If there is a default namespace declaration in scope, the expanded name corresponding to an unprefixed element name has the URI of the default namespace as its namespace name. If there is no default namespace declaration in scope, the namespace name has no value. [...] In all cases, the local name is [...] the same as the unprefixed name itself.

元素 Cube 没有前缀,所以我们寻找范围内的默认命名空间声明;到达最外层元素,我们找到 xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref",因此 Cube 的“命名空间名称” 元素是 URI http://www.ecb.int/vocabulary/2002-08-01/eurofxref,“本地名称”是 Cube .

如果我们改为查看元素 gesmes:Sender,我们会看到它有一个前缀 gesmes,因此我们会查找该前缀的定义。查找 xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01",我们可以得出结论,“扩展名称”具有“命名空间名称” http://www.gesmes.org/xml/2002-08-01,以及“本地名称”Sender

为了在 XPath 中使用这些“命名空间名称”,我们必须指定用于 XPath 表达式的前缀,而不必与实际文档中的前缀相对应。在这种情况下,我们选择为命名空间 http://www.ecb.int/vocabulary/2002-08-01/eurofxref 分配前缀 c,以便表达式 //c:Cube 将匹配“命名空间名称”为 http://www.ecb.int/vocabulary/2002-08-01/eurofxref 的任何元素和 Cube 的“本地名称”。

关于php - 如何在不使用 PHP 进行迭代的情况下按属性选择 xml 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18432430/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com