簡體   English   中英

如何使用php從xml元素獲取attribut值類型(ss:Name)

[英]How to get attribut value type (ss:Name) from xml element with php

我想從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:名字價值嗎?

謝謝

屬性ss的前綴是名稱空間。

您可以通過請求它作為Attributes()的參數來獲取該命名空間的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而不是Worksheet->Workbook

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM