Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在不使用PHP迭代的情况下按属性选择xml元素?_Php_Xml_Xpath_Simplexml - Fatal编程技术网

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

如何在不使用PHP迭代的情况下按属性选择xml元素?,php,xml,xpath,simplexml,Php,Xml,Xpath,Simplexml,我从 但是我想不迭代地得到值,我真的不想使用正则表达式 我试过这样的方法,但没用: $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[@cur

我从

但是我想不迭代地得到值,我真的不想使用正则表达式

我试过这样的方法,但没用:

$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
namespace,其前缀为
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"]');
下面是更正后的名称空间


试图给出一个权威的解释,考虑下面的摘录:

前缀提供限定名称的名称空间前缀部分,并且必须与名称空间声明中的名称空间URI引用关联[…]请注意,前缀仅用作名称空间名称的占位符

与带前缀的元素或属性名称对应的扩展名称将前缀绑定到的URI作为其命名空间名称[…]

默认名称空间声明适用于其范围内所有未固定的元素名称

如果作用域中存在默认命名空间声明,则对应于未固定元素名称的扩展名称将默认命名空间的URI作为其命名空间名称。如果作用域中没有默认命名空间声明,则命名空间名称没有值。[…]在所有情况下,本地名称与未固定名称本身[…]相同

元素
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
,以及“本地名称”
发送者


为了在XPath中使用这些“名称空间名称”,我们必须为XPath表达式中使用的前缀分配前缀,这不需要与实际文档中的前缀对应。在本例中,我们选择分配名称空间
http://www.ecb.int/vocabulary/2002-08-01/eurofxref
前缀
c
,这样表达式
//c:Cube
将匹配“名称空间名称”为
http://www.ecb.int/vocabulary/2002-08-01/eurofxref
和“本地名称”多维数据集的

以哪种方式工作,确切地说?我只是得到了一个空数组,可能是因为您的XML中没有“摩擦”?抱歉,有。我删掉了一些行,使它变短,并删除了一个完全与擦,我把它改为美元…它的作品,真棒!我不明白gesmes是如何不相关的,因为它甚至在根节点中。。。这两个名称空间让我感到困惑,我需要仔细阅读背后的理论!谢谢每个元素正好位于一个名称空间中,而不管它的祖先在哪个名称空间中。在本文档中,前缀为
gesmes:
的节点位于一个名称空间中,没有前缀的节点位于另一个名称空间(“默认”名称空间)。使用XPath,您只需要指定要查找的元素的名称空间,而不管文档中还包含什么。太棒了,非常感谢,现在我终于明白了@伊姆索普
$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"]');