Extracting data from Excel XML files using perl XML::Twig handlers when tags (nodes) contain prefix
OK, I've found a solution. Turns out that XML::Twig has an optional argument
map_xmlns
I can use this to fix my problem. So, my original code
my $t= XML::Twig->new(twig_roots => {'worksheet/sheetData/row/c' =>
sub { Get_Sheet_Data_TEST_1(@_,\@Results);}})->parse($sheetFile);
becomes
my $t= XML::Twig->new(
map_xmlns => {
'http://schemas.openxmlformats.org/spreadsheetml/2006/main' => 's'},
twig_roots => {'s:worksheet/s:sheetData/s:row/s:c' =>
sub { Get_Sheet_Data_TEST_1(@_,\@Results);}})->parse($sheetFile);
Now my handler works for all prefixes (even empty ones!).
As written in the XML::Twig documentation:
map_xmlns
This option is passed a hashref that maps uri's to prefixes. The prefixes in the document will be replaced by the ones in the map. The mapped prefixes can (actually have to) be used to trigger handlers, navigate or query the document.