Skip to content
Advertisement

How to get attribute value type (ss:Name) from xml element with php

I want to get attribut value type (ss:Name) from xml element with php. But it doesn’t work.

This is my xml file :

<?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>

and my php source

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

Can you help my to get ss:Name value ?

Thanks

Advertisement

Answer

The prefix of your attribute ss is a namespace.

You can grab attributes for that namespace by requesting it as the argument for Attributes() like this:

$node->Attributes(‘urn:schemas-microsoft-com:office:spreadsheet’);

$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>';

Example php:

<?php
$xml = simplexml_load_string($xml);
foreach($xml->Worksheet->Attributes('urn:schemas-microsoft-com:office:spreadsheet') as $key=>$val) {
  echo "$key: $valn";
}

outputs

Name: 097-097-024

also note that your top node is Worksheet not Worksheet->Workbook

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement