Tweak xsl to eliminate duplicate data and blank lines

L

Luke Airig

I am trying to merge two xml files based on common date/time and then
write out a tab-delimited xml file with the header record from one of
the input files concatenated in front of the merged detail records in
the output file. The version I have is close to what I need except it
concatenates two sets of header records in front of each merged detail
record and the first two lines in the file and the last line is blank.

Can someone please tell me how to tweak the xsl stylesheet to get rid
of the duplicate concatenated header fields and the blank lines?

Here are the xsl and xml files:

merge_lrv_gps_and_trans_to_tab_delim.xsl:
-----------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput method="text"/>
<xsl:variable name="delim" select="' '"/> <!-- tab -->
<xsl:variable name="nl" select="'
'"/> <!-- newline -->
<xsl:variable name="head">
<xsl:for-each select="/root/header/*">
<xsl:value-of select="concat(., $delim)"/>
</xsl:for-each>
</xsl:variable>

<xsl:template match="/root/record">
<!--note: if lrv_gps.xml is in a different directory, you will
need to use the relative path or URL-->
<xsl:variable name="gps"
select="document('lrv_gps.xml')/root/record[date_time =
current()/date_time]"/>
<xsl:value-of select="$head"/>
<xsl:value-of select="concat($gps/longitude, $delim,
$gps/latitude, $delim)"/>
<xsl:for-each select="*">
<xsl:value-of select="concat(., $delim)"/>
</xsl:for-each>
</xsl:template>

<!--this empty template is to stop the header values from printing at
top of page-->
<xsl:template match="header">
</xsl:template>

</xsl:stylesheet>


lrv_gps.xml:
------------
<?xml version="1.0"?>
<root>
<record>
<longitude>-105.111111</longitude>
<latitude>39.111111</latitude>
<date_time>2003/12/10.16:08</date_time>
</record>
<record>
<longitude>-105.222222</longitude>
<latitude>39.222222</latitude>
<date_time>2003/12/10.16:18</date_time>
</record>
<record>
<longitude>-105.111111</longitude>
<latitude>39.111111</latitude>
<date_time>2003/12/10.16:28</date_time>
</record>
<record>
<longitude>-105.111111</longitude>
<latitude>39.111111</latitude>
<date_time>2003/12/10.17:08</date_time>
</record>
<record>
<longitude>-105.222222</longitude>
<latitude>39.222222</latitude>
<date_time>2003/12/10.17:18</date_time>
</record>
<record>
<longitude>-105.111111</longitude>
<latitude>39.111111</latitude>
<date_time>2003/12/10.17:28</date_time>
</record>
</root>

lrv_trans.xml:
--------------
<?xml version="1.0"?>
<root>
<header>
<driver_id>driver_id_from_trans</driver_id>
<vehicle_id>vehicle_id_from_trans</vehicle_id>
<duty_shift_id>duty_shift_id_from_trans</duty_shift_id>
<route_id>route_id_from_trans</route_id>
<cid_terminal_id>cid_terminal_id_from_trans</cid_terminal_id>
</header>
<record>
<date_time>2003/12/10.16:08</date_time>
<tag_id>tag_id_from_trans_1</tag_id>
<stop_location_id>stop_location_id_from_trans_1</stop_location_id>
<fare_type_cd>fare_type_cd_from_trans_1</fare_type_cd>
<blacklist_cd>blacklist_cd_from_trans_1</blacklist_cd>
</record>
<record>
<date_time>2003/12/10.16:18</date_time>
<tag_id>tag_id_from_trans_4</tag_id>
<stop_location_id>stop_location_id_from_trans_4</stop_location_id>
<fare_type_cd>fare_type_cd_from_trans_4</fare_type_cd>
<blacklist_cd>blacklist_cd_from_trans_4</blacklist_cd>
</record>
<header>
<driver_id>driver_id_from_trans</driver_id>
<vehicle_id>vehicle_id_from_trans</vehicle_id>
<duty_shift_id>duty_shift_id_from_trans</duty_shift_id>
<route_id>route_id_from_trans</route_id>
<cid_terminal_id>cid_terminal_id_from_trans</cid_terminal_id>
</header>
<record>
<date_time>2003/12/10.17:08</date_time>
<tag_id>tag_id_from_trans_1</tag_id>
<stop_location_id>stop_location_id_from_trans_1</stop_location_id>
<fare_type_cd>fare_type_cd_from_trans_1</fare_type_cd>
<blacklist_cd>blacklist_cd_from_trans_1</blacklist_cd>
</record>
<record>
<date_time>2003/12/10.17:18</date_time>
<tag_id>tag_id_from_trans_4</tag_id>
<stop_location_id>stop_location_id_from_trans_4</stop_location_id>
<fare_type_cd>fare_type_cd_from_trans_4</fare_type_cd>
<blacklist_cd>blacklist_cd_from_trans_4</blacklist_cd>
</record>
</root>


TIA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top