我是SimpleXML和XPath的新手,虽然我已经成功地使用了几个与此相似的例子,但这个特别的例子似乎让我感到难过。
我尝试访问dmd:surveyId属性(Record节点),但没有成功。我尝试添加'dmd:'前缀,如下所示:
echo 'surveyId is...<h1>'.$xml_intellcont_report_abbrev[0]['dmd:surveyId'].'</h1><br>';
感谢任何线索。
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 $xml_report_abbrev_bb[0]->INTELL->CONTYPE;
echo $xml_report_abbrev_bb[0]['termId'].'<br>';
echo 'surveyId is...<h1>'.$xml_intellcont_report_abbrev[0]['dmd:surveyId'].'</h1><br>';
} else {
echo 'XPath query failed';
}
?>
XML('test_bb.xml'):
<?xml version="1.0" encoding="UTF-8"?>
<Data xmlns="http://www.digitalmeasures.com/schema/data" xmlns:dmd="http://www.digitalmeasures.com/schema/data-metadata" dmd:date="2012-01-03">
<Record userId="148" username="john-smith" termId="4" dmd:surveyId="12">
<dmd:IndexEntry indexKey="D" entryKey="Dylan" text="Dylan"/>
<INTELL id="14" dmd:originalSource="54TEX" dmd:lastModified="2017-04-18T10:54:29" dmd:startDate="2011-01-01" dmd:endDate="2011-12-31">
<CONTYPE>Sales Tools</CONTYPE>
<CONTYPEOTHER>Sales History</CONTYPEOTHER>
</INTELL>
</Record>
<Record userId="149" username="mary-smith" termId="4" dmd:surveyId="16">
<dmd:IndexEntry indexKey="D2" entryKey="Dylan2" text="Dylan2"/>
<INTELL id="14" dmd:originalSource="54TEX" dmd:lastModified="2012-04-18T10:54:29" dmd:startDate="2011-01-01" dmd:endDate="2011-12-31">
<CONTYPE>Other Tools</CONTYPE>
<CONTYPEOTHER>Sales History Part B</CONTYPEOTHER>
</INTELL>
</Record>
</Data>
答案 0 :(得分:1)
您需要查看使用命名空间的代码示例。其中许多都可以在这个网站上找到。
如果它来自另一个命名空间,则无法指出属性。从命名空间'dmd'获取atrributes的正确方法是:
TypeError