From a1b70fa5c9b62578ef82e88764d7f71ddc7b6d33 Mon Sep 17 00:00:00 2001
From: Jens Steidl <Jens.Steidl@slub-dresden.de>
Date: Fri, 9 Apr 2021 13:48:35 +0200
Subject: [PATCH] - add MARCXML schema location for potential automated
 validation

---
 bin/slubsipbuilderbagit.pl | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/bin/slubsipbuilderbagit.pl b/bin/slubsipbuilderbagit.pl
index 63e7f43..d217e3d 100644
--- a/bin/slubsipbuilderbagit.pl
+++ b/bin/slubsipbuilderbagit.pl
@@ -207,6 +207,7 @@ package SLUB::LZA::SIPBuilderBagIt;
             my $recordData = $xp->findnodes_as_string('/*[local-name()="searchRetrieveResponse"]/*[local-name()="records"]/*[local-name()="record"]/*[local-name()="recordData"]/*');
             if (!$recordData) { croak("ERROR: Did not get any <recordData/> for PPN '$ppn' using '$sru'");}
             my $marc_doc = $parser->parse_string($recordData);
+            add_marc21_schema_location($marc_doc);
             if ($with_debug) {
                 say "write DEBUG_${ppn}_extracted_marc_record.xml";
                 SLUB::LZA::SIPBuilderBagIt::write_file("DEBUG_${ppn}_extracted_marc_record.xml", $marc_doc);
@@ -220,6 +221,16 @@ package SLUB::LZA::SIPBuilderBagIt;
         return;
     }
 
+    sub add_marc21_schema_location($) {
+        if(! defined $_[0]) { croak "marc xml document required!"; }
+        my $marc_doc = $_[0];
+        my $root = $marc_doc->findnodes('/*[local-name()="record"]')->[0]; # get root element (XML::LibXML::Element)
+        $root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'); # add xsi namespace (simple)
+        $root->setAttribute('xsi:schemaLocation', 'http://www.loc.gov/MARC21/slim https://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd'); # add MARCXML schema location
+        # HINT: method sets or replaces the node's attribute
+        1;
+    }
+
     sub write_file($$){
         if(! defined $_[0]) { croak "filename not defined!"; }
         if(! defined $_[1]) { croak "value not defined!"; }
-- 
GitLab