Bug 9415: XML catalog export is missing root node
authorFridolyn SOMERS <fridolyn.somers@biblibre.com>
Thu, 17 Jan 2013 10:42:53 +0000 (11:42 +0100)
committerJared Camins-Esakov <jcamins@cpbibliography.com>
Wed, 20 Mar 2013 02:07:02 +0000 (22:07 -0400)
When exporting some biblio or authorities records with tools/export.pl (via web client or command-line) :
If choosing XML format, you get a concatenation of full XML records.

This patch uses MARC::File::XML to create a well formated file.
See http://search.cpan.org/~gmcharlt/MARC-XML-0.93/lib/MARC/File/XML.pm#close%28%29

Test plan:
- Go to Tools/Export data
- Enter numbers in from and to biblio number
  (make sure that at least two records will
   be exported).
- Select xml in file format
- Click "Export bibliographic records"
- Save file somewhere
- Look at downloaded file
=> File should look like :
   <?xml version="1.0" encoding="UTF-8"?>
   <collection
   ...

   <record>
   ...
   </record>

   <record>
   ...
   </record>

   </collection>
=> "collection" is the root node and XML declaration exists only once
- Do the same for authorities export and command-line use of tools/export.pl

Second test plan:

- From the command line, run tools/export.pl, e.g.,

  tools/export.pl  --format=xml --filename=bibs.xml

- Verify that the output is valid XML, e.g.,

  xmllint --noout bibs.xml # if the file is valid, no error messages will be displayed

Signed-off-by: Galen Charlton <gmc@esilibrary.com>
Signed-off-by: Paul Poulain <paul.poulain@biblibre.com>
Signed-off-by: Jared Camins-Esakov <jcamins@cpbibliography.com>
tools/export.pl

index 1b5a766..d634419 100755 (executable)
@@ -17,6 +17,7 @@
 # Suite 330, Boston, MA  02111-1307 USA
 
 use Modern::Perl;
+use MARC::File::XML;
 use List::MoreUtils qw(uniq);
 use Getopt::Long;
 use CGI;
@@ -309,6 +310,7 @@ if ( $op eq "export" ) {
             } @{ $sth->fetchall_arrayref };
         }
 
+        my $xml_header_written = 0;
         for my $recordid ( uniq @recordids ) {
             if ($deleted_barcodes) {
                 my $q = "
@@ -374,19 +376,30 @@ if ( $op eq "export" ) {
                 }
                 RemoveAllNsb($record) if ($clean);
                 if ( $output_format eq "xml" ) {
-                    if ( $marcflavour eq 'UNIMARC' && $record_type eq 'auths' )
-                    {
-                        print $record->as_xml_record('UNIMARCAUTH');
-                    }
-                    else {
-                        print $record->as_xml_record($marcflavour);
+                    unless ($xml_header_written) {
+                        MARC::File::XML->default_record_format(
+                            (
+                                     $marcflavour eq 'UNIMARC'
+                                  && $record_type eq 'auths'
+                            ) ? 'UNIMARCAUTH' : $marcflavour
+                        );
+                        print MARC::File::XML::header();
+                        print "\n";
+                        $xml_header_written = 1;
                     }
+                    print MARC::File::XML::record($record);
+                    print "\n";
                 }
                 else {
                     print $record->as_usmarc();
                 }
             }
         }
+        if ($xml_header_written) {
+            print MARC::File::XML::footer();
+            print "\n";
+        }
+
         exit;
     }
     elsif ( $format eq "csv" ) {