How to copy a value of an element as a new element in a multi record xml document using XSLT

76 Views Asked by At

I just started working with XSLT...I have an xml document with multiple records. I want to take the value of of each under and insert it as a new element under the corresponding . Unfortunately, my XSLT takes all the values of oai:identifier and dumps it under the value of a mods:identifier for every record since I do not know the right expression to use.

Here is a fragment of the xml document.

<OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xip="http://www.tessella.com/XIP/v4"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
    <responseDate>2019-06-15T02:21:52Z</responseDate>
    <request verb="ListRecords" metadataPrefix="MODS" from="2019-06-01T00:00:00Z"
        until="2019-06-04T23:59:59Z">https://lac.preservica.com/OAI-PMH/</request>
    <ListRecords>
        <record>
            <header>
                **<identifier>oai:du:191f96fe-fcf7-4205-a6b0-980e74b51178</identifier>**
                <datestamp>2019-06-04T22:42:33Z</datestamp>
            </header>
            <metadata>
                <mods:mods xmlns:mods="http://www.loc.gov/mods/v3"
                    xmlns="http://www.loc.gov/mods/v3" xmlns:xs="http://www.w3.org/2001/XMLSchema"
                    xmlns:local="http://www.loc.org/namespace" version="3.4">
                    <titleInfo>
                        <title>Cadre de surveillance</title>
                    </titleInfo>
                    <name type="corporate">
                        <namePart>Canada.</namePart>
                        <namePart>Agence de la consommation en matière financière du
                            Canada</namePart>
                    </name>
                    <typeOfResource>text</typeOfResource>
                    <genre authority="rdacontent/fre">texte</genre>
                    <originInfo>
                        <place>
                            <placeTerm authority="marccountry" type="code">onc</placeTerm>
                        </place>
                        <dateIssued encoding="marc">20182017</dateIssued>
                        <edition>Version finale révisée.</edition>
                        <issuance>monographic</issuance>
                    </originInfo>
                    <originInfo displayLabel="publisher">
                        <place>
                            <placeTerm type="text">Ottawa : </placeTerm>
                        </place>
                        ....
                    **<identifier invalid="yes">9780660082752</identifier>**
                    ....
                </mods:mods>
            </metadata>
            <about>
                <aboutRecord:aboutRecord
                    xmlns:aboutRecord="http://www.preservica.com/OAI-PMH/Extension"
                    xmlns="http://www.preservica.com/OAI-PMH/Extension">
                    <Identifier>oai:du:191f96fe-fcf7-4205-a6b0-980e74b51178</Identifier>
                    <CurrentVersion>0cb46171-b015-4ba1-bde8-4fd87f4c6cee</CurrentVersion>
                    <ChangeType>Created</ChangeType>
                    <XIP schemaURI="http://www.tessella.com/XIP/v4">
                        <xip:DeliverableUnit status="new">

                            ...
                         </xip:CurrentVersion>
                        </xip:DeliverableUnit>
                    </XIP>
                </aboutRecord:aboutRecord>
            </about>
        </record>
<record>
            <header>
                <identifier>oai:du:f0dbbd4c-ec70-40cd-bbb2-4b88926043fd</identifier>
                <datestamp>2019-06-04T22:42:33Z</datestamp>
            </header>
            <metadata>
                <mods:mods xmlns:mods="http://www.loc.gov/mods/v3"
                    xmlns="http://www.loc.gov/mods/v3" xmlns:xs="http://www.w3.org/2001/XMLSchema"
                    xmlns:local="http://www.loc.org/namespace" version="3.4">
                    <titleInfo>
                        <title>Compréhension et sensibilisation aux commotions liées au sport, en
....

XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mods="http://www.loc.gov/mods/v3"
    xmlns:oai="http://www.openarchives.org/OAI/2.0/" xmlns:xip="http://www.tessella.com/XIP/v4"
    xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
    exclude-result-prefixes="xs xd mods xip oai aboutRecord"
    xmlns:aboutRecord="http://www.preservica.com/OAI-PMH/Extension"
    xmlns="http://www.preservica.com/OAI-PMH/Extension" version="2.0">
    <xsl:output method="xml" indent="yes"/>

    <!-- Identity template. Copies everything -->
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <!-- Overide identity template to add a new identifier to the MODS identifier element -->
    <xsl:template match="mods:mods/mods:identifier[1]">
        <!-- Copy the existing elements -->
        <xsl:copy>
            <!-- And everything inside it -->
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
        <!-- Add new node -->
        <identifier type="preservation">
            <xsl:value-of select="/oai:OAI-PMH/oai:ListRecords/oai:record/oai:header/oai:identifier"></xsl:value-of>
        </identifier>
    </xsl:template>

Expected result is to take value of oai:identifier (oai:du:191f96fe-fcf7-4205-a6b0-980e74b51178) and insert it as a corresponding new mods:identifier and repeat this process for every record. As follows:

<identifier type="preservation">oai:du:191f96fe-fcf7-4205-a6b0-980e74b51178</identifier>

Actual result ends up as:

<identifier xmlns="http://www.preservica.com/OAI-PMH/Extension" type="preservation">oai:du:191f96fe-fcf7-4205-a6b0-980e74b51178 oai:du:f0dbbd4c-ec70-40cd-bbb2-4b88926043fd oai:du:17010ebc-1fbc-47bb-96ab-5fb17f7171cb....
  • I only want one corresponding oai:du:xxxxxxxx per record.
  • I don't want xmlns="http://www.preservica.com/OAI-PMH/Extension"
1

There are 1 best solutions below

3
Martin Honnen On

You get that namespace because the stylesheet declares xmlns="http://www.preservica.com/OAI-PMH/Extension" on its root element. It is not clear whether you need to for other elements you create, if not, simply take it out, otherwise use

    <!-- Add new node -->
    <identifier xmlns="http://www.loc.gov/mods/v3" type="preservation">

(respectively any namespace you neeed for the identifier element, with all the data you have thrown in I got a bit lost).

As for the referencing the corresponding identifier, I think you want to reference

  <xsl:value-of select="ancestor::oai:record/oai:header/oai:identifier"/>