Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 747
user3245543 Avatar asked Sep 09 '26 01:09

user3245543


1 Answers

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: $val\n";
}

outputs

Name: 097-097-024

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

like image 65
WEBjuju Avatar answered Sep 11 '26 15:09

WEBjuju