如何从php的xml元素获取属性值types(ss:Name)

我想从PHP的XML元素获取attribut值types(SS:名称)。 但它不起作用。

这是我的xml文件:

<?xml version="1.0"?> <?mso-application progid="Excel.Sheet"?> <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Worksheet ss:Name="097-097-024"> </Worksheet> </Workbook> 

和我的PHP源码

 <?php $xml = simplexml_load_file($fichier); $attr = $xml->Worksheet->Workbook->attributes(); echo $attr['ss:Name']; ?> 

你能帮我取得ss:名字值吗?

谢谢

属性ss的前缀是一个名称空间。

您可以通过以Attributes()的参数的forms获取该名称空间的Attributes()如下所示:

$节点 – >属性(“瓮:架构 – 微软COM:办公室:电子表格”);

 $xml = '<?xml version="1.0"?> <?mso-application progid="Excel.Sheet"?> <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Worksheet ss:Name="097-097-024"> </Worksheet> </Workbook>'; $xml = simplexml_load_string($xml); foreach($xml->Worksheet->Attributes('urn:schemas-microsoft-com:office:spreadsheet') as $key=>$val) { echo "$key: $val\n"; } 

输出

名称:097-097-024

还要注意你的顶层节点是Worksheet not Worksheet->Workbook