Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Perl和LibXML处理excelxml文件_Xml_Perl_Excel - Fatal编程技术网

用Perl和LibXML处理excelxml文件

用Perl和LibXML处理excelxml文件,xml,perl,excel,Xml,Perl,Excel,我正在尝试处理保存为XML电子表格的Excel文件中的数据。在做了大量的研究(我以前没有做过太多的XML处理)之后,我仍然无法让它工作。以下是我的最小文件的内容: <?xml version="1.0"?> <?mso-application progid="Excel.Sheet"?> <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-micro

我正在尝试处理保存为XML电子表格的Excel文件中的数据。在做了大量的研究(我以前没有做过太多的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:fn="http://www.w3.org/2005/xpath-functions"
 xmlns:sbmextension="http://www.serena.com/SBM/XSLT_Extension">
 <Worksheet ss:Name="index">
 </Worksheet>
</Workbook>

但是,如果我删除(默认名称空间?)xmlns=“urn:schemas-microsoft-com:office:spreadsheet”,它就会开始工作。你能告诉我我错过了什么吗?我想我可以在解析文档之前删除它,但我想了解我做错了什么:)。提前感谢。

如果要使用XPath表达式和名称空间,必须先注册名称空间,然后每次在提到名称空间元素的所有XPath表达式中使用它:

#!/usr/bin/perl
use warnings;
use strict;

use XML::LibXML;
use Data::Dumper;

my $xml = << '__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:fn="http://www.w3.org/2005/xpath-functions"
 xmlns:sbmextension="http://www.serena.com/SBM/XSLT_Extension">
 <Worksheet ss:Name="index">
 </Worksheet>
</Workbook>
__XML__

my $doc = XML::LibXML->load_xml( string => $xml);
my $xc  = XML::LibXML::XPathContext->new( $doc->documentElement );
$xc->registerNs('ss', 'urn:schemas-microsoft-com:office:spreadsheet');
my $xpath = '/ss:Workbook/ss:Worksheet/@ss:Name';

print Dumper $xc->findvalue($xpath);
#/usr/bin/perl
使用警告;
严格使用;
使用XML::LibXML;
使用数据::转储程序;
我的$xml=
__XML__
my$doc=XML::LibXML->load_XML(字符串=>$XML);
my$xc=XML::LibXML::XPathContext->新建($doc->documentElement);
$xc->registerNs('ss','urn:schemas-microsoft-com:office:spreadsheet');
my$xpath='/ss:Workbook/ss:Worksheet/@ss:Name';
打印转储程序$xc->findvalue($xpath);

您为什么不使用其中一个预构建的XLSX解析模块?我不知道它们的存在,我的谷歌搜索技术很差。感谢链接(令人困惑的是,生成文件的工具将其保存为.xls而不是xlxs)啊!我没有意识到我需要将名称空间添加到每个级别,这样做只是解决了问题,因为LibXML从源文档中提取名称空间:)。干杯
#!/usr/bin/perl
use warnings;
use strict;

use XML::LibXML;
use Data::Dumper;

my $xml = << '__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:fn="http://www.w3.org/2005/xpath-functions"
 xmlns:sbmextension="http://www.serena.com/SBM/XSLT_Extension">
 <Worksheet ss:Name="index">
 </Worksheet>
</Workbook>
__XML__

my $doc = XML::LibXML->load_xml( string => $xml);
my $xc  = XML::LibXML::XPathContext->new( $doc->documentElement );
$xc->registerNs('ss', 'urn:schemas-microsoft-com:office:spreadsheet');
my $xpath = '/ss:Workbook/ss:Worksheet/@ss:Name';

print Dumper $xc->findvalue($xpath);