【发布时间】:2014-09-24 10:29:13
【问题描述】:
我在将 xml 字符串加载到 SimpleXMLElement 时遇到问题(我也尝试使用 DOCDocument,但结果相同)。在 XML 中我有这个:
<definedNames>
<definedName name="name1" Id="1" hidden="1">NAME_TEST</definedName>
<definedName name="name2" Id="4" hidden="1">NAME_TEST_2</definedName>
</definedNames>
现在我需要使用“名称”属性访问特定标签。但总是当我尝试 print_r、var_dump 或 smth 时,我总是看到所有其他属性,但是当我看到只有带有
的数组时[0] = > NAME_TEST,
[1] => NAME_TEST_2
我也尝试过 xpath,但每次当我引用里面的属性时,我都会得到空数组。
所以现在我尝试了:xpath、SimpleXMLDom、DOCDocument,但结果始终相同 - 空数组。有什么线索吗?
@编辑
$xl->LoadTemplate('#xl/workbook.xml');
if (isset($workbook) && is_array($workbook) && count($workbook > 0)) {
$dom = new DOMDocument();
$dom->loadXML($xl->Source);
$xpath = new DOMXpath($dom);
foreach ($xpath->evaluate('//definedName') as $definedName) {
echo $definedName->getAttribute('name');
}
} else {
$TBS->Source = preg_replace('~\<definedNames\>.*\<\/definedNames\>~', '', $TBS->Source);
}
@edit2 - xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x15" xmlns:x15="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main">
<definedNames>
<definedName name="name1" Id="1" hidden="1">NAME_TEST</definedName>
<definedName name="name2" Id="4" hidden="1">NAME_TEST_2</definedName>
</definedNames>
</workbook>
我知道有类似的东西,但我已经尝试过:
$xpath->evaluate('//workbook/definedNames/definedName[@*]')
或
$xpath->evaluate('/workbook/definedNames/definedName[@name="name1"]')
结果仍然是空的。
【问题讨论】: