Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.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-SimpleXML中子节点的名称空间移位疑难解答_Php_Xml_Xpath_Simplexml_Xml Namespaces - Fatal编程技术网

PHP-SimpleXML中子节点的名称空间移位疑难解答

PHP-SimpleXML中子节点的名称空间移位疑难解答,php,xml,xpath,simplexml,xml-namespaces,Php,Xml,Xpath,Simplexml,Xml Namespaces,我使用SimpleXML和XPath试图获取子节点的('IndexEntry)属性('indexKey')的值。我已经在另一个节点(“记录”)上成功测试了节点的名称空间。 由于某些原因,此节点的属性('indexKey')没有返回。我尝试使用children方法访问节点,同时指定其名称空间 PHP代码 <?php $url = "test_bb.xml"; $xml = simplexml_load_file($url); $xml->registerXPathNamespac

我使用SimpleXML和XPath试图获取子节点的('IndexEntry)属性('indexKey')的值。我已经在另一个节点(“记录”)上成功测试了节点的名称空间。 由于某些原因,此节点的属性('indexKey')没有返回。我尝试使用children方法访问节点,同时指定其名称空间

PHP代码

<?php
$url = "test_bb.xml";


$xml = simplexml_load_file($url);

$xml->registerXPathNamespace('a','http://www.digitalmeasures.com/schema/data');
$xml->registerXPathNamespace('dmd','http://www.digitalmeasures.com/schema/data-metadata');

$xml_report_abbrev_bb = $xml->xpath('//a:Record[@username="john-smith"]');

if($xml_report_abbrev_bb){
    echo '<br>CONTYPE is...'.$xml_report_abbrev_bb[0]->INTELLCONT->CONTYPE;
    echo '<br>termId is...'.$xml_report_abbrev_bb[0]['termId'].'<br>';
    echo '<br>surveyId is...'.$xml_report_abbrev_bb[0]->attributes('dmd',true)['surveyId'].'<br>';

//below - I've tried different methods of accessing the IndexEntry node...
    $dmd_fields = $xml_report_abbrev_bb[0]->children('dmd',true);
    echo '<br>dmd:IndexEntry is...'.$dmd_fields->IndexEntry['indexKey'].'<br>';

    echo '<br>dmd:IndexEntry is...'.$xml_report_abbrev_bb[0]->children('dmd',true)->IndexEntry['indexKey'].'<br>';

    //echo '<br>dmd:IndexEntry is...'.$xml_report_abbrev_bb[0]->IndexEntry('dmd',true)['indexKey'].'<br>';

    //echo '<br>dmd:IndexEntry is...'.$xml_report_abbrev_bb[0]->xpath('/dmd:indexEntry[@indexKey]')[0].'<br>';


} else {
    echo 'XPath query failed b';  
}

?>
registerXPathNamespace('a','http://www.digitalmeasures.com/schema/data');
$xml->registerXPathNamespace('dmd','http://www.digitalmeasures.com/schema/data-metadata');
$xml\u report\u abbrev\u bb=$xml->xpath('//a:Record[@username=“john smith”]');
如果($xml\u report\u abbrev\u bb){
echo“
CONTYPE是…”。$xml\u report\u abbrev\u bb[0]->INTELLCONT->CONTYPE; echo'
termId是…。$xml_report_abbrev_bb[0]['termId']。
'; echo“
surveyId是…”。$xml_report_abbrev_bb[0]->属性('dmd',true)['surveyId'].
; //下面-我尝试了访问IndexEntry节点的不同方法。。。 $dmd_fields=$xml_report_abbrev_bb[0]->子项('dmd',true); echo'
dmd:IndexEntry是…'。$dmd_fields->IndexEntry['indexKey'].
; echo'
dmd:IndexEntry是…。$xml\u report\u abbrev\u bb[0]->children('dmd',true)->IndexEntry['indexKey']。
; //echo'
dmd:IndexEntry是…。$xml\u report\u abbrev\u bb[0]->IndexEntry('dmd',true)['indexKey'].
; //echo'
dmd:IndexEntry是…。$xml_report_abbrev_bb[0]>xpath('/dmd:IndexEntry[@indexKey]')[0].
'; }否则{ echo“XPath查询失败b”; } ?>
XML('test_bb.XML')


销售工具
销售历史
在行中

$dmd_fields = $xml_report_abbrev_bb[0]->children('dmd', true);
这将意味着
$dmd_fields
将是一个节点列表,即使列表中只有一个节点-因此使用
$dmd_fields[0]
引用
元素。由于这是
IndexEntry
元素,只需使用此元素和该元素的列表属性,您就可以

echo '<br>dmd:IndexEntry is...'.$dmd_fields[0]->attributes()['indexKey'].'<br>';
echo'
dmd:IndexEntry是…。$dmd_字段[0]->attributes();
尽管
IndexEntry
元素位于
http://www.digitalmeasures.com/schema/data-metadata
名称空间,由本地前缀
dmd:
表示,其属性没有前缀:


按名称引用节点列表中的项应该可以正常工作;SimpleXML的设计是非常宽容的。谢谢——我只是不喜欢依赖事情“应该”发生的时间。我在使用PHP和一些框架时遇到的最大问题之一是,很多事情都是通过“魔法”发生的。
echo '<br>dmd:IndexEntry is...'.$dmd_fields[0]->attributes()['indexKey'].'<br>';
<dmd:IndexEntry indexKey="D" entryKey="Dylan" text="Dylan"/>
$dmd_fields->IndexEntry->attributes('')->indexKey;