Merge two xml files using XSLT
Here should go questions about transforming XML with XSLT and FOP.
-
- Posts: 1
- Joined: Fri Apr 05, 2019 9:01 am
Merge two xml 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>PGW0001</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>PGW0002</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>PGW0001</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>PGD0001</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>PGW0002</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>PGW0001</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>PGD0001</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>PGW0002</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:stylesheet 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="document($fileName)" />
<xsl:variable name="updateMeasurement" select="$updates/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="Item[not(measurements/measurement = $updateMeasurement/measurements/measurement)]" />
<xsl:apply-templates select="/Schedule/Item//measurements" />
<xsl:apply-templates select="$updates/Schedule/Item/measPeriods" />
</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
Fileone.xml
<Schedule name="NE3S">
<Item scheduleId="1" startDate="2013-01-01" stopDate="2037-12-31">
<measurements>
<measurement>PGW0001</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>PGW0002</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>PGW0001</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>PGD0001</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>PGW0002</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>PGW0001</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>PGD0001</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>PGW0002</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:stylesheet 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="document($fileName)" />
<xsl:variable name="updateMeasurement" select="$updates/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="Item[not(measurements/measurement = $updateMeasurement/measurements/measurement)]" />
<xsl:apply-templates select="/Schedule/Item//measurements" />
<xsl:apply-templates select="$updates/Schedule/Item/measPeriods" />
</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
Jump to
- Oxygen XML Editor/Author/Developer
- ↳ Feature Request
- ↳ Common Problems
- ↳ DITA (Editing and Publishing DITA Content)
- ↳ SDK-API, Frameworks - Document Types
- ↳ DocBook
- ↳ TEI
- ↳ XHTML
- ↳ Other Issues
- Oxygen XML Web Author
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Content Fusion
- ↳ Feature Request
- ↳ Common Problems
- Oxygen JSON Editor
- ↳ Feature Request
- ↳ Common Problems
- Oxygen PDF Chemistry
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Feedback
- ↳ Feature Request
- ↳ Common Problems
- Oxygen XML WebHelp
- ↳ Feature Request
- ↳ Common Problems
- XML
- ↳ General XML Questions
- ↳ XSLT and FOP
- ↳ XML Schemas
- ↳ XQuery
- NVDL
- ↳ General NVDL Issues
- ↳ oNVDL Related Issues
- XML Services Market
- ↳ Offer a Service