【发布时间】:2019-10-28 01:33:21
【问题描述】:
我有以下 XML 文件 sheetX.xml(取自 Excel XML 工作表文件):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet 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"
xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac"
xmlns:xr="http://schemas.microsoft.com/office/spreadsheetml/2014/revision"
xmlns:xr2="http://schemas.microsoft.com/office/spreadsheetml/2015/revision2"
xmlns:xr3="http://schemas.microsoft.com/office/spreadsheetml/2016/revision3"
mc:Ignorable="x14ac xr xr2 xr3"
xr:uid="{109BF357-4A9A-4969-B57D-8A2B0130DC3F}">
<dimension ref="A1"/>
<sheetViews>
<sheetView tabSelected="1" topLeftCell="M1" workbookViewId="0">
<selection activeCell="A1" sqref="A1"/>
</sheetView>
</sheetViews>
<sheetFormatPr defaultRowHeight="15" x14ac:dyDescent="0.25"/>
<sheetData/>
<pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/>
</worksheet>
我正在使用XML::LibXML Perl 模块读取文件
use strict;
use warnings;
use XML::LibXML;
use XML::LibXML::Reader;
my $reader = XML::LibXML::Reader->new( location => sheetX.xml);
$reader->read();
while($NERROR1==0){
my $doc = $reader->copyCurrentNode(1);
if(!defined $doc){
$NERROR1=-1;
} else {
if($reader->attributeCount()>0){
print "tag name:" . $reader->name() . "\n";
my @attributelist = $doc->attributes();
for my $iAtt (0 .. scalar @attributelist-1){
print "Att name:" . $attributelist[$iAtt]->nodeName() . "\n";
print "Att value:" . $attributelist[$iAtt]->value . "\n";
}
}
$reader->nextElement();
}
}
$reader->close();
perl 模块的一些标签的输出是:
tag name:worksheet
Att name:mc:Ignorable
Att value:x14ac xr xr2 xr3
Att name:xr:uid
Att value:{00000000-0001-0000-0400-000000000000}
Att name:xmlns
Att value:http://schemas.openxmlformats.org/spreadsheetml/2006/main
Att name:xmlns:mc
Att value:http://schemas.openxmlformats.org/markup-compatibility/2006
Att name:xmlns:r
Att value:http://schemas.openxmlformats.org/officeDocument/2006/relationships
Att name:xmlns:x14ac
Att value:http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac
Att name:xmlns:xr
Att value:http://schemas.microsoft.com/office/spreadsheetml/2014/revision
Att name:xmlns:xr2
Att value:http://schemas.microsoft.com/office/spreadsheetml/2015/revision2
Att name:xmlns:xr3
Att value:http://schemas.microsoft.com/office/spreadsheetml/2016/revision3
和
tag name:sheetView
Att name:tabSelected
Att value:1
Att name:topLeftCell
Att value:M1
Att name:workbookViewId
Att value:0
Att name:xmlns
Att value:http://schemas.openxmlformats.org/spreadsheetml/2006/main
和
tag name:sheetFormatPr
Att name:defaultRowHeight
Att value:15
Att name:x14ac:dyDescent
Att value:0.25
Att name:xmlns
Att value:http://schemas.openxmlformats.org/spreadsheetml/2006/main
Att name:xmlns:x14ac
Att value:http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac
因此,基本上,代码打印出sheetView 和sheetFormatPr 标记的XML 文件中未显示的xmlns 属性,但worksheet 标记具有显示在文件,没有多余的。
在某个阶段,我需要根据我的 perl 程序生成的数据重建 XML 文件(该程序还打印出标签、值等)。所以我的问题是:有什么方法可以让我的 perl 程序打印出 XML 文件中显示的标签,而不是其他未显示的标签?
【问题讨论】:
-
你指的是
xmlns属性吗? -
是的,它们没有显示在“sheetView”和“sheetFormatPr”标签的xml文件中,而是被LibXML找到。
标签: xml perl xml-parsing namespaces xml-namespaces