L
Luke Airig
I have two xml files that I need to merge on their common field
(date_time). The merged output file needs to have the date_time field
and all fields from both of the two input files. I am using the Saxon
xml parser. Can someone please help me to get this to work? The two
xml input files follow, as well as my attempt to write the merge xsl
(merge_on_date_time.xsl):
lrv_1_transaction_cid_1.xml:
----------------------------
<?xml version="1.0"?>
<root>
<record>
<date_time> 2003/12/10.16:08 </date_time>
<driver_id> TRANSIT_VEHICLE_ID1 </driver_id>
<vehicle_id> FARE_TYPE_CD1 </vehicle_id>
<duty_shift_id> DUTY_SHIFT_ID_1 </duty_shift_id>
<route_id> ROUTE_ID_1 </route_id>
<cid_terminal_id> CID_TERMINAL_ID </cid_terminal_id>
<tag_id> TAG_ID </tag_id>
</record>
</root>
lrv_1_gps_cid.xml:
------------------
<?xml version="1.0"?>
<root>
<record>
<stop_location_id> STOP_LOCATION_ID1 </stop_location_id>
<latitude> 39.814658 </latitude>
<longitude> -105.183682 </longitude>
<date_time> 2003/12/10.16:08 </date_time>
<vehicle_id> TRANSIT_VEHICLE_ID1 </vehicle_id>
<fare_type_cd> FARE_TYPE_CD1 </fare_type_cd>
<blacklist_cd> V </blacklist_cd>
</record>
</root>
merge_on_date_time.xsl:
-----------------------
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xslutput method="xml"/>
<!-- load the merge file -->
<xsl:variable name="transactions"
select="document('lrv_1_gps_cid.xml')"/>
<xsl:template match="/">
<root>
<xsl:for-each select="root/record">
<!-- cache the key: date_time -->
<xsl:variable name="date_time">
<xsl:value-of select="@date_time"/></xsl:variable>
<!-- copy the child nodes -->
<record>
<xsl:copy-of select="child::*"/>
<!-- copy the children of the matching record node from the
merge file -->
<xsl:copy-of
select="$transactions/root/record[@date_time=$date_time]/child::*"/>
</record>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>
TIA
(date_time). The merged output file needs to have the date_time field
and all fields from both of the two input files. I am using the Saxon
xml parser. Can someone please help me to get this to work? The two
xml input files follow, as well as my attempt to write the merge xsl
(merge_on_date_time.xsl):
lrv_1_transaction_cid_1.xml:
----------------------------
<?xml version="1.0"?>
<root>
<record>
<date_time> 2003/12/10.16:08 </date_time>
<driver_id> TRANSIT_VEHICLE_ID1 </driver_id>
<vehicle_id> FARE_TYPE_CD1 </vehicle_id>
<duty_shift_id> DUTY_SHIFT_ID_1 </duty_shift_id>
<route_id> ROUTE_ID_1 </route_id>
<cid_terminal_id> CID_TERMINAL_ID </cid_terminal_id>
<tag_id> TAG_ID </tag_id>
</record>
</root>
lrv_1_gps_cid.xml:
------------------
<?xml version="1.0"?>
<root>
<record>
<stop_location_id> STOP_LOCATION_ID1 </stop_location_id>
<latitude> 39.814658 </latitude>
<longitude> -105.183682 </longitude>
<date_time> 2003/12/10.16:08 </date_time>
<vehicle_id> TRANSIT_VEHICLE_ID1 </vehicle_id>
<fare_type_cd> FARE_TYPE_CD1 </fare_type_cd>
<blacklist_cd> V </blacklist_cd>
</record>
</root>
merge_on_date_time.xsl:
-----------------------
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xslutput method="xml"/>
<!-- load the merge file -->
<xsl:variable name="transactions"
select="document('lrv_1_gps_cid.xml')"/>
<xsl:template match="/">
<root>
<xsl:for-each select="root/record">
<!-- cache the key: date_time -->
<xsl:variable name="date_time">
<xsl:value-of select="@date_time"/></xsl:variable>
<!-- copy the child nodes -->
<record>
<xsl:copy-of select="child::*"/>
<!-- copy the children of the matching record node from the
merge file -->
<xsl:copy-of
select="$transactions/root/record[@date_time=$date_time]/child::*"/>
</record>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>
TIA