5. C3 Plus-Marc21-XML
add chapter

5.1 Marc21-XML-Datei erstellen
add section

<code>
// ---------------------------------------------------------------------------------
// Date:   2017/11/08
// Author: RLO/KPA
// ---------------------------------------------------------------------------------
// History:
//
// 2017-08-22 RLO: changes in Field 100 (added role and rolename)
// 2017-08-22 RLO: added constant fields 336, 337, 338
// 2017-08-22 RLO: callname (Signatur): moved from Field 850 to Field 852
// 2017-08-23 RLO: added constant field 040
// 2017-08-30 RLO: field 936: changes for volume/month subfields
// 2017-11-08 RLO/KPA: setting GND fields only if GND value is not empty
//
// ---------------------------------------------------------------------------------
// Test creation of a MARC21 XML file using scripting
// Important: At the end the generated XML string must be returned as a result
//
// Impotant: Generate datafield tags always IMMER in ascending order!
//
// ---------------------------------------------------------------------------------
// -----------------------------------------------------------
// -----------------------------------------------------------
// Indexnames as script variables:
// use them instead numerics...
// -----------------------------------------------------------
// -----------------------------------------------------------

ind_Order_ID                              =  1;   // Index Order-ID  Type: String [no associated meta data type]: myBib eDoc Order-Number ind_Titel_der_Zeitung_Zeitschrift         =  2;   // Index Title of the newspaper/magazin  Type: String [title]:
ind_Verlag                                =  3;   // Index Publisher  Type: String [Verlag]:
ind_Erscheinungsort                       =  4;   // Index place of publication  Type: String [place of publication]:
ind_ZDB_ID                                =  5;   // Index ZDB-ID  Type: Number [ZDB ID]:
ind_Titel_des_Artikels                    =  6;   // Index Title of the article  Type: String [title of the article]:
ind_Untertitel_des_Artikels               =  7;   // Index Subtitle of the article  Type: String [Subtitle of the article]:
ind_Sprache_des_Artikels                  =  8;   // Index Language of the article  Type: Collection (option list with multiple selection) [Language]:
ind_Erscheinungsdatum                     =  9;   // Index Publication date  Type: String [Publication date]:
ind_Band                                  = 10;   // Index Band  Type: String [Volume]:
ind_Jahr                                  = 11;   // Index Year  Type: String [Date of publication]: Year of the volume or magazin
ind_Seitenangabe                          = 12;   // Index Page reference  Type: String [Page reference]: von - bis
ind_Fotos                                 = 13;   // Index Fotos  Type: option list [no associated meta data type]: Photos

ind_Autoren_des_Artikels                  = 14;   // Index authors of the article  Type: Table [Article author]: Article authors
     col_Autor_Autorname                  =  1;   // Column Name of the author  Type: String [Article author]: Author's name in preferred form
     col_Autor_Lebensdaten                =  2;   // Column life dates  Type: String [life dates]: Dates of Birth and Death
     col_Autor_GND_ID_des_Autors          =  3;   // Column GND ID of the author  Type: String [GND ID]:

ind_Sonstige_Personen                     = 15;   // Index Other people  Type: Table [Other people]:
     col_Sonst_Name                       =  1;   // Column Name  Type: String [Other people]:
     col_Sonst_Rolle                      =  2;   // Column Role  Type: Option list [Role of the person]: Role of the person
     col_Sonst_Lebensdaten                =  3;   // Column Life dates  Type: String [Life dates]:
     col_Sonst_GND_ID                     =  4;   // Column GND ID  Type: Number [GND ID]:

ind_Schlagworte                           = 16;   // Index Keywords  Type: Table [Keyword]: Keywords
     col_Schla_Schlagwort                 =  1;   // Column Keyword  Type: String [Keyword]: Keyword
     col_Schla_GND_ID                     =  2;   // Column GND ID  Type: String [GND ID Keyword]: GND-ID

ind_Organisation_Koerperschaft            = 17;   // Index organization/corporation Type: Tabelle [Körperschaft]: Index organization/corporation
     col_Organ_Organisation_Koerperschaft =  1;   // Column Organisation/Körperschaft  Type: String [Körperschaft]: Index organization/corporation
     col_Organ_GND_ID                     =  2;   // Column GND-ID  Type: String [GND ID]: GND-ID

ind_Veranstaltungen                       = 18;   // Index events  Type: Table [Veranstaltung]: Veranstaltungen
     col_Veran_Veranstaltung              =  1;   // Column Veranstaltung  Type: String [Veranstaltung]: Veranstaltung
     col_Veran_GND_ID                     =  2;   // Column GND-ID  Type: String [GND ID]: GND-ID
     col_Veran_Ort                        =  3;   // Column Location  Type: String [no associated meta data type]: location
     col_Veran_Zeitraum                   =  4;   // Column Period  Type: String [life dates]: Period or day of the event

ind_Kuenstler                             = 19;   // Index Artist  Type: Table [Artist]: Artist
     col_Kuens_Kuenstler                  =  1;   // Column Artist  Type: String [Artist]: Author's name in preferred form
     col_Kuens_Rolle                      =  2;   // Column Role  Type: Option list [Rolle der Person]: Rolle der Person
     col_Kuens_Lebensdaten                =  3;   // Column Lebensdaten  Type: String [Lebensdaten]: Dates of birth and death
     col_Kuens_GND_ID_Kuenstler           =  4;   // Column GND ID Künstler  Type: String [GND ID]:

ind_Textgattung                           = 20;   // Index text genre  Type: Option list [no associated meta data type]: text genre
ind_Kategorie                             = 21;   // Index Category  Type: Option list [Typ (Monographie, Periodikum)]:
ind_Vorlagenart                           = 22;   // Index template type  Type: Option list [no associated meta data type]: Template type, indication of origin
ind_Medientyp                             = 23;   // Index Media type  Type: Option list [Typ (Text, Map, Video, Audio...)]:
ind_Genre                                 = 24;   // Index Genre  Type: Option list[Genre]: The genre defines the type of medium: (Monograph (book), article, ...)
ind_Nutzungsrecht                         = 25;   // Index Copyright  Type: Option list [Copyright]: Copyright

// -----------------------------------------------------------
// -----------------------------------------------------------
//
// initialisieren...
//
xr.initMarc21Doc();

//
// the "Leader"...
//
xr.setLeader();

//
// controlfield 001 contains the MyBib order number as a unique identifier
//
xr.addControlField ('001', job.keyMap['ME_Order_ID']);

//
// controlfield 003 contains as the respective ISIL (taken from ME field //'or_responder_id')
//
xr.addControlField ('003', job.myBibOrderRec['or_responder_id']);

//
// controlField 007: Typ of the resource: here the index value must be mapped to //'cr' or 'tu'...
//
if (job.index(18) == 'Online-Ressource')
    ressource_type = 'cr';
else
    ressource_type = 'tu';

xr.addControlField ('007', ressource_type);

//
// datafield 040: Constant field (SWB, Katz)
//
xr.addDataField ("", "", "040");
xr.addSubField ("e", "rda");


//
// datafield 041: Language of the article
//
xr.addDataField ("0", "", "041");
// subfield a: Language
xr.addSubField ("a", job.index(ind_Sprache_des_Artikels));


//
// Authors:
//
// first author in field 100, two more in field 700
// NOTA: 700s below because of order...
// maximum three...
//
if (job.indexNumRows(ind_Autoren_des_Artikels) >= 1) {

   //
   // erster Autor:
   //
   xr.addDataField ("1", "", "100");
   xr.addSubField  ("a", job.indexVal(ind_Autoren_des_Artikels, 1, col_Autor_Autorname));

   if (job.indexVal(ind_Autoren_des_Artikels, 1, col_Autor_GND_ID_des_Autors).length)
      xr.addSubField  ("0", "(DE-588)" + job.indexVal(ind_Autoren_des_Artikels, 1, col_Autor_GND_ID_des_Autors));

   xr.addSubField  ("e", "VerfasserIn");

   if (job.indexVal(ind_Autoren_des_Artikels, 1, col_Autor_GND_ID_des_Autors).length)
      xr.addSubField  ("2", "gnd");

   xr.addSubField  ("4", "aut");

} // job.indexNumRows(ind_Autoren_des_Artikels) >= 1

//
// datafield 245: Title of the article
//
xr.addDataField ("1", "0", "245");
// subfield a: Title
// subfield b: Subtitle
xr.addSubField ("a", job.index(ind_Titel_des_Artikels));
xr.addSubField ("b", job.index(ind_Untertitel_des_Artikels));


//
// datafield 264: Publisher, Location, Year...
//
xr.addDataField ("", "1", "264");
// subfield a: Place of Publication
// subfield b: Publisher
// subfield c: Date
xr.addSubField ("a", job.index(ind_Erscheinungsort));
xr.addSubField ("b", job.index(ind_Verlag));
xr.addSubField ("c", job.index(ind_Erscheinungsdatum));

//
// datafield 336: Constant field (SWB, Katz)
//
xr.addDataField ("", "", "336");
xr.addSubField ("a", "Text");
xr.addSubField ("b", "txt");
xr.addSubField ("2", "rdacontent");

//
// datafield 337: Constant field (SWB, Katz)
//
xr.addDataField ("", "", "337");
xr.addSubField ("a", "without using tools");
xr.addSubField ("b", "n");
xr.addSubField ("2", "rdamedia");

//
// datafield 338: Constant field (SWB, Katz)
//
xr.addDataField ("", "", "338");
xr.addSubField ("a", "Band");
xr.addSubField ("b", "nc");
xr.addSubField ("2", "rdacarrier");


//
// datafield 600: Artists as keywords
//
if (job.indexNumRows(ind_Kuenstler) >= 1) {

   //
   // iterate over all artists
   //
   for (i = 1; i <= job.indexNumRows(ind_Kuenstler); i++) {

      xr.addDataField ("1", "7", "600");
      xr.addSubField  ("a", job.indexVal(ind_Kuenstler, i, col_Kuens_Kuenstler));

      if (job.indexVal(ind_Kuenstler, i, col_Kuens_GND_ID_Kuenstler).length) {
         xr.addSubField  ("0", "(DE-588)" + job.indexVal(ind_Kuenstler, i, col_Kuens_GND_ID_Kuenstler));
         xr.addSubField  ("2", "gnd");
      }

   } // for i...

} // job.indexNumRows(ind_Kuenstler) >= 1



//
// datafield 610: Koerperschaften als Schlagwoerter
//
if (job.indexNumRows(ind_Organisation_Koerperschaft) >= 1) {

   //
   // iteriere ueber alle Koerperschaften
   //
   for (i = 1; i <= job.indexNumRows(ind_Organisation_Koerperschaft); i++) {

      xr.addDataField ("", "7", "610");
      xr.addSubField  ("a", job.indexVal(ind_Organisation_Koerperschaft, i, col_Organ_Organisation_Koerperschaft));

      if (job.indexVal(ind_Organisation_Koerperschaft, i, col_Organ_GND_ID).length) {
         xr.addSubField  ("0", "(DE-588)" + job.indexVal(ind_Organisation_Koerperschaft, i, col_Organ_GND_ID));
         xr.addSubField  ("2", "gnd");
      }

   } // for i...

} // job.indexNumRows(ind_Organisation_Koerperschaft) >= 1

//
// datafield 610: Events as keywords
//
if (job.indexNumRows(ind_Veranstaltungen) >= 1) {

   //
   // iterate over all corporations
   //
   for (i = 1; i <= job.indexNumRows(ind_Veranstaltungen); i++) {

      xr.addDataField ("", "7", "610");
      xr.addSubField  ("a", job.indexVal(ind_Veranstaltungen, i, col_Veran_Veranstaltung));

      if (job.indexVal(ind_Veranstaltungen, i, col_Veran_GND_ID).length) {
         xr.addSubField  ("0", "(DE-588)" + job.indexVal(ind_Veranstaltungen, i, col_Veran_GND_ID));
         xr.addSubField  ("2", "gnd");
      }

   } // for i...

} // job.indexNumRows(ind_Veranstaltungen) >= 1


//
// datafield 650: title of the dossier
// 650: Additional entry under a keyword
//
xr.addDataField ("", "7", "650");
// subfield a: Text
// subfield 2: Source (here again ISIL)
xr.addSubField ("a", job.myBibOrderRec['or_item_series_title']);
xr.addSubField ("2", job.myBibOrderRec['or_responder_id']);


//
// datafield 650: Schlagwoerter
//
if (job.indexNumRows(ind_Schlagworte) >= 1) {

   //
   // iteriere ueber alle Schlagwoeter
   //
   for (i = 1; i <= job.indexNumRows(ind_Schlagworte); i++) {

      xr.addDataField ("", "7", "650");
      xr.addSubField  ("a", job.indexVal(ind_Schlagworte, i, col_Schla_Schlagwort));

      if (job.indexVal(ind_Schlagworte, i, col_Schla_GND_ID).length) {
         xr.addSubField  ("0", "(DE-588)" + job.indexVal(ind_Schlagworte, i, col_Schla_GND_ID));
         xr.addSubField  ("2", "gnd");
      }

   } // for i...

} // job.indexNumRows(ind_Schlagworte) >= 1

//
// Tag 700
//
// other authors:
//
// first author in field 100, two more in field 700
// maximum three...
//

if (job.indexNumRows(ind_Autoren_des_Artikels) >= 2) {

   //
   // zweiter Autor:
   //
   xr.addDataField ("1", "", "700");
   xr.addSubField  ("a", job.indexVal(ind_Autoren_des_Artikels, 2, col_Autor_Autorname));

   if (job.indexVal(ind_Autoren_des_Artikels, 2, col_Autor_GND_ID_des_Autors).length)
      xr.addSubField  ("0", "(DE-588)" + job.indexVal(ind_Autoren_des_Artikels, 2, col_Autor_GND_ID_des_Autors));

   xr.addSubField  ("e", "VerfasserIn");

   if (job.indexVal(ind_Autoren_des_Artikels, 2, col_Autor_GND_ID_des_Autors).length)
      xr.addSubField  ("2", "gnd");

   xr.addSubField  ("4", "aut");

} // job.indexNumRows(ind_Autoren_des_Artikels) >= 2

if (job.indexNumRows(ind_Autoren_des_Artikels) >= 3) {

   //
   // second author:
   //
   xr.addDataField ("1", "", "700");
   xr.addSubField  ("a", job.indexVal(ind_Autoren_des_Artikels, 3, col_Autor_Autorname));

   if (job.indexVal(ind_Autoren_des_Artikels, 3, col_Autor_GND_ID_des_Autors).length)
      xr.addSubField  ("0", "(DE-588)" + job.indexVal(ind_Autoren_des_Artikels, 3, col_Autor_GND_ID_des_Autors));

   xr.addSubField  ("e", "VerfasserIn");

   if (job.indexVal(ind_Autoren_des_Artikels, 3, col_Autor_GND_ID_des_Autors).length)
      xr.addSubField  ("2", "gnd");

   xr.addSubField  ("4", "aut");

} // job.indexNumRows(ind_Autoren_des_Artikels) >= 3

//
// datafield 700: other people involved
//
if (job.indexNumRows(ind_Sonstige_Personen) >= 1) {

   //
   // iterate over all other persons...
   //
   for (i = 1; i <= job.indexNumRows(15); i++) {

      role        = job.indexVal(ind_Sonstige_Personen, i, col_Sonst_Rolle);
      role_abbrev = ut.subString(role, 0, 3);
      role_name   = ut.subString(role, 4);

      xr.addDataField ("1", "", "700");
      xr.addSubField  ("a", job.indexVal(ind_Sonstige_Personen, i, col_Sonst_Name));

      if (job.indexVal(ind_Sonstige_Personen, i, col_Sonst_GND_ID).length)
         xr.addSubField  ("0", "(DE-588)" + job.indexVal(ind_Sonstige_Personen, i, col_Sonst_GND_ID));

      if (job.indexVal(ind_Sonstige_Personen, i, col_Sonst_GND_ID).length)
         xr.addSubField  ("2", "gnd");

      xr.addSubField  ("e", role_name);
      xr.addSubField  ("4", role_abbrev);

   } // for i...

} // job.indexNumRows(ind_Sonstige_Personen) >= 1


//
// datafield 773: Title of the newspaper/magazine
//
xr.addDataField ("0", "8", "773");
// subfield i: Text 'In'
// subfield t: Title of the newspaper/magazine
// subfield w: ZDB-ID, (DE-600) is prefixed
xr.addSubField ("i", "Enthalten in");
xr.addSubField ("t", job.index(ind_Titel_der_Zeitung_Zeitschrift));
xr.addSubField ("w", "(DE-600)" + job.index(ind_ZDB_ID));

//
// datafield 852: Signature of the dossiers
//
xr.addDataField ("", "", "852");
// subfield a: ISIL
// subfield c: Signatur
xr.addSubField ("a", job.myBibOrderRec['or_responder_id']);
xr.addSubField ("e", job.myBibOrderRec['or_item_callname']);

//
// datafield 856: eL Link
//

//
// Aufruf-Formate: Java oder Web-Frontend...
//
// Web-Frontend: http://<server-url>/<mel-name>/web/index.html?open=<medianumber>
// Java Viewer:  http://<server-url>/<mel-name>/browser/open/<medianumber>
//
eL_name    = job.getWfKeyValue("EL_NAME");
server_url = job.getWfKeyValue("EL_SERVER");
url        = server_url + eL_name + '/web/index.html?open=' + job.index1;

xr.addDataField ("4", "2", "856");
// subfield u: URL
// subfield x: always "D"
// subfield y: always "elektronical resource eL"
xr.addSubField ("u", url);
xr.addSubField ("x", "D");
xr.addSubField ("y", "elektronical resource eL");

//
// datafield 936: fill from index ind_release_date (release_date) if it is not empty
//
// else take index ind_band (volume) and ind_year(year)
//

var date_str   = new String(job.index(ind_Erscheinungsdatum));
var date_year  = new String;
var date_month = new String;
var date_day   = new String;

//
// Divide the index 9 into three parts if possible...
//
pos_point      = date_str.indexOf('.');
pos_last_point = date_str.lastIndexOf('.');

if (pos_point > 0) {

  date_day = date_str.substring(0, pos_point).trim();

  if (pos_last_point > pos_point) {
     date_month = date_str.substring(pos_point + 1, pos_last_point).trim();
     date_year  = date_str.substring(pos_last_point + 1, date_str.length).trim();
  } // pos_last_point > pos_point

} // pos_point > 0...

if ((date_year.length > 0) || (date_month.length > 0) || (date_day.length > 0)) {

  //
  // Fall 1: index ind_Erscheinungsdatum is available and year/month/day (or parts) could be extracted ...
  //

  //
  // datafield 936: Volume, Edition, Pages
  //
  xr.addDataField ("u", "w", "936");
  // subfield b: Day
  // subfield c: Month
  // subfield j: Year
  // subfield h: Page number
  xr.addSubField ("b", date_day);
  xr.addSubField ("c", date_month);
  xr.addSubField ("j", date_year);
  xr.addSubField ("h", job.index(ind_Seitenangabe));

} // date_year.length > 0...
else {

  //
  // Case 2: Index 9 was empty, now get volume and year (Index 10/11)
  //

   var sl    = ut.split(job.index(ind_Band), '=');
   var band  = '';
   var monat = '';

   if (sl.length >= 2) {
    band  = ut.trimmed(sl[0]);
    monat = ut.trimmed(sl[1]);
   } else {
    band = ut.trimmed(job.index(ind_Band));
   }

  //
  // datafield 936: Volume, edition, pages
  //
  xr.addDataField ("u", "w", "936");
  // subfield d: Edition/Volume
  // subfield c: Month
  // subfield j: Year
  // subfield h: Page number
  if (band.length > 0)
     xr.addSubField ("d", volume);
  if (monat.length > 0)
     xr.addSubField ("c", month);
  xr.addSubField ("j", job.index(ind_Jahr));
  xr.addSubField ("h", job.index(ind_Seitenangabe));

} // date_year.length > 0...


//
// Speichern des XML-Ergebnisses in der KeyMap des Jobs unter 'Marc21XML'
//
job.setKeyValue("Marc21XML", xr.marc21XmlString());

//
// dies liefert den (hoffentlich) erzeugten XML-String als Ergebnis zurück
// und führt dazu, dass die Datei - wie im Transferziel angegegben - geschrieben wird...
//
xr.marc21XmlString();