无法使用PHP Simplexml在命名空间前缀中添加属性
尝试编辑使用Excels XML命名空间的XML文档:
Trying to edit an XML document that uses Excels XML-Namespaces:
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
我需要得到这个结果(需要 ss:前缀befire Type ):
I need to get to this result (need the ss: prefix befire Type):
<Cell ...><Data ss:Type="String">value</Data></Cell>
我已经查看了问题 无法使用PHP SimpleXML添加名称空间 ,但此方法在这里无济于事.换句话说,按照此处概述的方式运行
I've looked over the question Unable add namespace with PHPs SimpleXML but this method is not helping here. In other words, running this as outlined there
$data = $cells[$i]->addChild('Data','value');
$data->addAttribute("ss:Type","String","urn:schemas-microsoft-com:office:spreadsheet");
给我
<Cell ...><Data Type="String">value</Data></Cell>
,没有 ss:前缀.如果我删除 urn:前缀,那么我会得到这个
without the ss: prefix. And if I remove the urn: prefix, then I'm getting this
<Cell ...><Data xmlns:ss="schemas-microsoft-com:office:spreadsheet" ss:Type="String">value</Data></Cell>
无论哪种情况,当我使用Excel打开文档时,数据都是不可见的.在 urn:的情况下,缺少 ss:且没有 urn:的情况,该定义将成为 元素的一部分,而不是在Excel中工作.
In either case, when I open the document with Excel, the data is invisible. With urn: the ss: is missing and without urn: the definition becomes part of the element which is not working in Excel.
如果要添加具有特定名称空间前缀的属性,而无需在文档中添加名称空间,则不仅要在 name 参数,但在我的示例中,再次以xmlns
为前缀:xmlns:ss:Type
.然后,名称空间URI(第三个参数)也将被忽略,因此也没有必要:
If you want to add an attribute with a specific namespace prefix w/o adding the namespace to the document, you have to specify not only the prefix in the name parameter, but also prefix it again with xmlns
, in my example: xmlns:ss:Type
. The namespace URI (third parameter) then will be ignored as well, so it is not necessary either:
$data->addAttribute("xmlns:ss:Type", "String");
请注意 ss:Type 前面的 xmlns:.然后输出是所需的:
Note the xmlns: in front of ss:Type. The output then is as required:
<Cell><Data ss:Type="String">value</Data></Cell>