如何使用php从xml元素获取attribut值类型(ss:Name)

时间:2017-02-17 14:10:24

标签: php xml excel xml-parsing

我想从php获取xml元素的attribut值类型(ss:Name)。 但它没有用。

这是我的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:名字价值吗?

由于

1 个答案:

答案 0 :(得分:0)

属性ss的前缀是命名空间。

您可以通过请求Attributes()的参数来获取该命名空间的属性,如下所示:

  

$节点 - &GT;属性( '瓮:架构 - 微软-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而不是Worksheet->Workbook

相关问题