Home > other >  2 XML files, 1 XLST: extracting data using shared ID number
2 XML files, 1 XLST: extracting data using shared ID number

Time:06-16

I would really appreciate some help here. Relatively new to XSLT and attempting to pull together information found in 2 xml files via an XSLT. In one file I have the ID number and title. In the other file I have the ID number and subject headings. Based on the shared ID number I would like to be able to create 2 separate records in the XML output that list the associated subject headings for each item. Here is where I am at:

---XML File 1---

<?xml version="1.0" encoding="UTF-8" ?>
<root>
    <Details Level="1">
        <Section SectionNumber="0">
            <Field Name="IDNUMBER1">
                <Value>MMK.01922</Value>
            </Field>
            <Field Name="TITLE1">
                <Value>This is the first title</Value>
            </Field>
        </Section>
    </Details>
    <Details Level="1">
        <Section SectionNumber="0">
            <Field Name="IDNUMBER1">
                <Value>MMK.01984</Value>
            </Field>
            <Field Name="TITLE1">
                <Value>Here is the second title</Value>
            </Field>
        </Section>
    </Details>
</root>

---XML File 2---

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mimsy_subject_data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <record>
        <id_number>MMK.01922</id_number>
        <subjects>
            <thesaurus_lcna_geo1>Scotland</thesaurus_lcna_geo1>
            <thesaurus_lcna_geo2>Abbotsford (Scotland)</thesaurus_lcna_geo2>
            <thesaurus_lcna_name1>Scott, Walter, 1771-1832</thesaurus_lcna_name1>
            <thesaurus_lcsh1>Scottish Borders (England and Scotland)</thesaurus_lcsh1>
        </subjects>
    </record>
    <record>
        <id_number>MMK.01984</id_number>
        <subjects>
            <thesaurus_lcna_geo1>Scotland</thesaurus_lcna_geo1>
            <thesaurus_lcna_geo2>Edinburgh (Scotland)</thesaurus_lcna_geo2>
            <thesaurus_lcna_corp1>Historic Royal Palaces (Great Britain)</thesaurus_lcna_corp1>
            <thesaurus_lcsh1>Palaces</thesaurus_lcsh1>
            <thesaurus_lcsh2>Landscapes</thesaurus_lcsh2>
            <thesaurus_lcsh3>Buildings</thesaurus_lcsh3>
        </subjects>
    </record>
</mimsy_subject_data>

---XSLT---

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:str="http://exslt.org/strings"
    xmlns:functx="http://www.functx.com" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:marc="http://www.loc.gov/MARC21/slim" xmlns:oai="http://www.openarchives.org/OAI/2.0/">

    <xsl:output method="xml" indent="yes" encoding="UTF-8"/>
    <xsl:strip-space elements="*"/>
    <!--  <xsl:template match="/"> -->

    <xsl:template match="root">
        <marc:collection>
            <xsl:apply-templates/>
        </marc:collection>
    </xsl:template>

    <xsl:template match="Details">
        <xsl:apply-templates select="Section"/>
    </xsl:template>

    <xsl:template match="Section">

        <xsl:param name = "subjectheading" select="document('mimsy_subject_data_export_tidied_for_SO.xml')/mimsy_subject_data/record" />

        <marc:record>
            <!-- BEGIN subjects-->
         
            <xsl:for-each select=".">
                <xsl:variable name="ID" select="Field[@Name = 'IDNUMBER1']/Value" />
                <xsl:variable name="shcheck" select="$subjectheading[id_number=$ID]" />

                <xsl:if test="$shcheck">
                  <marc:datafield tag="650" ind1=" " ind2=" ">
                    <marc:subfield code="a">
                     <xsl:copy-of select="$subjectheading/subjects"/>
                   </marc:subfield>
                  </marc:datafield>
               </xsl:if>
          </xsl:for-each>
        </marc:record>
    </xsl:template>
    <xsl:template match="*"/>
</xsl:stylesheet>

---Current Output---

<?xml version="1.0" encoding="UTF-8"?>
<marc:collection xmlns:str="http://exslt.org/strings"
                 xmlns:functx="http://www.functx.com"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xmlns:marc="http://www.loc.gov/MARC21/slim"
                 xmlns:oai="http://www.openarchives.org/OAI/2.0/">
   <marc:record>
      <marc:datafield tag="650" ind1=" " ind2=" ">
         <marc:subfield code="a">
            <subjects>
               <thesaurus_lcna_geo1>Scotland</thesaurus_lcna_geo1>
               <thesaurus_lcna_geo2>Abbotsford (Scotland)</thesaurus_lcna_geo2>
               <thesaurus_lcna_name1>Scott, Walter, 1771-1832</thesaurus_lcna_name1>
               <thesaurus_lcsh1>Scottish Borders (England and Scotland)</thesaurus_lcsh1>
            </subjects>
            <subjects>
               <thesaurus_lcna_geo1>Scotland</thesaurus_lcna_geo1>
               <thesaurus_lcna_geo2>Edinburgh (Scotland)</thesaurus_lcna_geo2>
               <thesaurus_lcna_corp1>Historic Royal Palaces (Great Britain)</thesaurus_lcna_corp1>
               <thesaurus_lcsh1>Palaces</thesaurus_lcsh1>
               <thesaurus_lcsh2>Landscapes</thesaurus_lcsh2>
               <thesaurus_lcsh3>Buildings</thesaurus_lcsh3>
            </subjects>
         </marc:subfield>
      </marc:datafield>
   </marc:record>
   <marc:record>
      <marc:datafield tag="650" ind1=" " ind2=" ">
         <marc:subfield code="a">
            <subjects>
               <thesaurus_lcna_geo1>Scotland</thesaurus_lcna_geo1>
               <thesaurus_lcna_geo2>Abbotsford (Scotland)</thesaurus_lcna_geo2>
               <thesaurus_lcna_name1>Scott, Walter, 1771-1832</thesaurus_lcna_name1>
               <thesaurus_lcsh1>Scottish Borders (England and Scotland)</thesaurus_lcsh1>
            </subjects>
            <subjects>
               <thesaurus_lcna_geo1>Scotland</thesaurus_lcna_geo1>
               <thesaurus_lcna_geo2>Edinburgh (Scotland)</thesaurus_lcna_geo2>
               <thesaurus_lcna_corp1>Historic Royal Palaces (Great Britain)</thesaurus_lcna_corp1>
               <thesaurus_lcsh1>Palaces</thesaurus_lcsh1>
               <thesaurus_lcsh2>Landscapes</thesaurus_lcsh2>
               <thesaurus_lcsh3>Buildings</thesaurus_lcsh3>
            </subjects>
         </marc:subfield>
      </marc:datafield>
   </marc:record>
</marc:collection>

---Required Output---

<?xml version="1.0" encoding="UTF-8"?>
<marc:collection xmlns:str="http://exslt.org/strings"
                 xmlns:functx="http://www.functx.com"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xmlns:marc="http://www.loc.gov/MARC21/slim"
                 xmlns:oai="http://www.openarchives.org/OAI/2.0/">
   <marc:record>
      <marc:datafield tag="650" ind1=" " ind2=" ">
         <marc:subfield code="a">
            <subjects>
               <thesaurus_lcna_geo1>Scotland</thesaurus_lcna_geo1>
               <thesaurus_lcna_geo2>Abbotsford (Scotland)</thesaurus_lcna_geo2>
               <thesaurus_lcna_name1>Scott, Walter, 1771-1832</thesaurus_lcna_name1>
               <thesaurus_lcsh1>Scottish Borders (England and Scotland)</thesaurus_lcsh1>
            </subjects>
         </marc:subfield>
      </marc:datafield>
   </marc:record>

   <marc:record>
      <marc:datafield tag="650" ind1=" " ind2=" ">
         <marc:subfield code="a">
            <subjects>
               <thesaurus_lcna_geo1>Scotland</thesaurus_lcna_geo1>
               <thesaurus_lcna_geo2>Edinburgh (Scotland)</thesaurus_lcna_geo2>
               <thesaurus_lcna_corp1>Historic Royal Palaces (Great Britain)</thesaurus_lcna_corp1>
               <thesaurus_lcsh1>Palaces</thesaurus_lcsh1>
               <thesaurus_lcsh2>Landscapes</thesaurus_lcsh2>
               <thesaurus_lcsh3>Buildings</thesaurus_lcsh3>
            </subjects>
         </marc:subfield>
      </marc:datafield>
   </marc:record>
</marc:collection>

The current output creates 2 records that contain all the subject headings listed in the xml file. I would like the output to have 2 records that only include the subject headings associated with each ID number. I am thinking this might require me to use another template but I'm just not sure how to get there. Any help here is very much appreciated. Many thanks in advance!

CodePudding user response:

Declare a key for the cross reference e.g.

<xsl:key name="id" match="record" use="id_number"/>

then use it in the template for

<xsl:template match="Section">
  <xsl:variable name="referenced-record" select="key('id', Field[@Name = 'IDNUMBER1']/Value, doc('mimsy_subject_data_export_tidied_for_SO.xml'))"/>

and simply select/output the subjects where you need them with e.g. <xsl:copy-of select="$referenced-record/subjects"/> in that template.

  • Related