How to merge two files using XSLT

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thoby
    New Member
    • Apr 2019
    • 1

    How to merge two files using XSLT

    I am trying to merge two xml files using XSLT1.0. I am trying to merge FileOne and Filetwo to merge to a new xml file. The resultant xml should contain one element from file two based on the value from measurement tag. Any help would be greatly appreciated

    Fileone.xml
    <Schedule name="NE3S">
    <Item scheduleId="1" startDate="2013-01-01" stopDate="2037-12-31">
    <measurements >
    <measurement>PG W0001</measurement>
    </measurements>
    <measPeriods>
    <period day="0" duration="0" hour="0" interval="15" minutes="0"/>
    </measPeriods>
    </Item>
    <Item scheduleId="2" startDate="2013-01-01" stopDate="2037-12-31">
    <measurements >
    <measurement>PG W0002</measurement>
    </measurements>
    <measPeriods>
    <period day="0" duration="0" hour="0" interval="60" minutes="0"/>
    </measPeriods>
    </Item>
    </Schedule>
    Filetwo.xml
    <Schedule name="NE3S">
    <Item scheduleId="1" startDate="2013-01-01" stopDate="2037-12-31">
    <measurements >
    <measurement>PG W0001</measurement>
    </measurements>
    <measPeriods>
    <period day="0" duration="0" hour="0" interval="60" minutes="0"/>
    </measPeriods>
    </Item>
    <Item scheduleId="2" startDate="2013-01-01" stopDate="2037-12-31">
    <measurements >
    <measurement>PG D0001</measurement>
    </measurements>
    <measPeriods>
    <period day="0" duration="0" hour="0" interval="60" minutes="0"/>
    </measPeriods>
    </Item>
    <Item scheduleId="3" startDate="2013-01-01" stopDate="2037-12-31">
    <measurements >
    <measurement>PG W0002</measurement>
    </measurements>
    <measPeriods>
    <period day="0" duration="0" hour="0" interval="60" minutes="0"/>
    </measPeriods>
    </Item>
    </Schedule>
    My expected output is

    <Schedule name="NE3S">
    <Item scheduleId="1" startDate="2013-01-01" stopDate="2037-12-31">
    <measurements >
    <measurement>PG W0001</measurement>
    </measurements>
    <measPeriods>
    <period day="0" duration="0" hour="0" interval="15" minutes="0"/>
    </measPeriods>
    </Item>
    <Item scheduleId="2" startDate="2013-01-01" stopDate="2037-12-31">
    <measurements >
    <measurement>PG D0001</measurement>
    </measurements>
    <measPeriods>
    <period day="0" duration="0" hour="0" interval="60" minutes="0"/>
    </measPeriods>
    </Item>
    <Item scheduleId="3" startDate="2013-01-01" stopDate="2037-12-31">
    <measurements >
    <measurement>PG W0002</measurement>
    </measurements>
    <measPeriods>
    <period day="0" duration="0" hour="0" interval="60" minutes="0"/>
    </measPeriods>
    </Item>
    </Schedule>
    please note that expected output

    <measPeriods>
    <period day="0" duration="0" hour="0" interval="15" minutes="0"/>
    </measPeriods>
    </Item>
    I tried the following code

    <xsl:styleshe et version="1.0" xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:param name="fileName" select="'/opt/Filetwo.xml'" />
    <xsl:param name="updates" select="documen t($fileName)" />

    <xsl:variable name="updateMea surement" select="$update s/Schedule/Item" />

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

    <xsl:template match="Schedule ">
    <xsl:copy>
    <xsl:apply-templates select="schedul eItem[not(@scheduleId = $updateMeasurem ent/@scheduleId)]" />
    <xsl:apply-templates select="$update Measurement" />
    </xsl:copy>
    </xsl:template>
    </xsl:stylesheet>

    `````````

    xsltproc merge.xslt /opt/Fileone.xml > /opt/FileThree.xml


    my expected output should have the <measPeriods> tag from Fileone.xml if the value in <measurements >/<measurement> matches
Working...