如何获取 SimpleXmlElement 属性的值?
How to get SimpleXmlElement attribute's value?
我有这个 XML 代码:
<w:footerReference w:type="default" r:id="rId6"/>
在 PHP 我有这个代码:
// $footer is a SimpleXMLElement, contain the code above
foreach ($footer->attributes() as $attr_name => $attr_value) {
dd($attr_name." = ".$attr_value);
}
而且 foreach
不是 运行。
我也试过这个:
$type = 'type';
$footer->attributes()->$type; // empty string
$wtype = 'w:type';
$footer->attributes()->$wtype; // empty string
当然我可以将 XML 转换为字符串并执行一些正则表达式魔术,但我认为这不是一个好方法。
更新:
这里是完整的 XML 文档代码:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"
xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"
xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
xmlns:w10="urn:schemas-microsoft-com:office:word"
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml"
xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml"
xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"
xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk"
xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml"
xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 wp14">
<w:body>
<w:sectPr w:rsidR="00654EDA">
<w:footerReference w:type="default" r:id="rId6"/>
</w:sectPr>
</w:body>
</w:document>
如何访问 w:type
和 r:id
属性值?
您必须将属性命名空间作为 attributes
的参数传递
$type = $footer->attributes("http://schemas.openxmlformats.org/wordprocessingml/2006/main")->type;
$id = $footer->attributes("http://schemas.openxmlformats.org/officeDocument/2006/relationships")->id;
与foreach相同
foreach ($footer->attributes("http://schemas.openxmlformats.org/wordprocessingml/2006/main") as $attr_name => $attr_value) {
dd($attr_name." = ".$attr_value);
}
foreach ($footer->attributes("http://schemas.openxmlformats.org/officeDocument/2006/relationships") as $attr_name => $attr_value) {
dd($attr_name." = ".$attr_value);
}
我有这个 XML 代码:
<w:footerReference w:type="default" r:id="rId6"/>
在 PHP 我有这个代码:
// $footer is a SimpleXMLElement, contain the code above
foreach ($footer->attributes() as $attr_name => $attr_value) {
dd($attr_name." = ".$attr_value);
}
而且 foreach
不是 运行。
我也试过这个:
$type = 'type';
$footer->attributes()->$type; // empty string
$wtype = 'w:type';
$footer->attributes()->$wtype; // empty string
当然我可以将 XML 转换为字符串并执行一些正则表达式魔术,但我认为这不是一个好方法。
更新:
这里是完整的 XML 文档代码:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"
xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"
xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
xmlns:w10="urn:schemas-microsoft-com:office:word"
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml"
xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml"
xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"
xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk"
xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml"
xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 wp14">
<w:body>
<w:sectPr w:rsidR="00654EDA">
<w:footerReference w:type="default" r:id="rId6"/>
</w:sectPr>
</w:body>
</w:document>
如何访问 w:type
和 r:id
属性值?
您必须将属性命名空间作为 attributes
$type = $footer->attributes("http://schemas.openxmlformats.org/wordprocessingml/2006/main")->type;
$id = $footer->attributes("http://schemas.openxmlformats.org/officeDocument/2006/relationships")->id;
与foreach相同
foreach ($footer->attributes("http://schemas.openxmlformats.org/wordprocessingml/2006/main") as $attr_name => $attr_value) {
dd($attr_name." = ".$attr_value);
}
foreach ($footer->attributes("http://schemas.openxmlformats.org/officeDocument/2006/relationships") as $attr_name => $attr_value) {
dd($attr_name." = ".$attr_value);
}