match muliple header records to associated detail records

L

Luke Airig

I am using the Saxon engine and I have an xml file that contains
batches of records. Each batch starts with a header record and the
associated detail records immediately follow each header. There can
be multiple batches in
a single file, each batch indicated by a new header.

I need to create a tab-delimited output file that appends all of the
header record fields in front of each associated detail record within
the same batch. The following xsl works for a file with a single
batch/header but not for multiple batches/headers. Can someone help
me tweak this to work properly? The Saxon command line syntax is
commented at the end of the xsl.

transform_bus_cov_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="/">
<xsl:apply-templates select="root/record"/>
</xsl:template>

<xsl:template match="record">
<xsl:value-of select="$head"/>
<xsl:for-each select="*">
<xsl:value-of select="concat(., $delim)"/>
</xsl:for-each>
<xsl:value-of select="$nl"/>
</xsl:template>

</xsl:stylesheet>

<!-- java com.icl.saxon.StyleSheet -o trans_cidcov1.dat
trans_cidcov1.xml transform_bus_cov_to_tab_delim.xsl -->

Here is my test data input file trans_cidcov1.xml:

<?xml version="1.0" ?>
<root>
<header>
<driver_id>DRIVER_ID_1</driver_id>
<vehicle_id>BUS_99</vehicle_id>
<duty_shift_id>AM</duty_shift_id>
<route_id>72X</route_id>
<cid_terminal_id>COV_1</cid_terminal_id>
</header>
<record>
<date_time>2003/12/10.8:10</date_time>
<tag_id>TAG_ID_1</tag_id>
<stop_location_id>BUS_STOP_1</stop_location_id>
<fare_type_cd>L</fare_type_cd>
<blacklist_cd />
</record>
<record>
<date_time>2003/12/10.8:10</date_time>
<tag_id>TAG_ID_3</tag_id>
<stop_location_id>BUS_STOP_1</stop_location_id>
<fare_type_cd>L</fare_type_cd>
<blacklist_cd />
</record>
<record>
<date_time>2003/12/10.8:30</date_time>
<tag_id>TAG_ID_5</tag_id>
<stop_location_id>BUS_STOP_2</stop_location_id>
<fare_type_cd>R</fare_type_cd>
<blacklist_cd />
</record>
<record>
<date_time>2003/12/10.8:30</date_time>
<tag_id>TAG_ID_5</tag_id>
<stop_location_id>BUS_STOP_2</stop_location_id>
<fare_type_cd>R</fare_type_cd>
<blacklist_cd />
</record>
<record>
<date_time>2003/12/10.8:45</date_time>
<tag_id>TAG_ID_4</tag_id>
<stop_location_id>BUS_STOP_3</stop_location_id>
<fare_type_cd>L</fare_type_cd>
<blacklist_cd>I</blacklist_cd>
</record>
<record>
<date_time>2003/12/10.10:00</date_time>
<tag_id>TAG_ID_2</tag_id>
<stop_location_id>BUS_STOP_4</stop_location_id>
<fare_type_cd>E</fare_type_cd>
<blacklist_cd />
</record>
<record>
<date_time>2003/12/10.10:10</date_time>
<tag_id>TAG_ID_6</tag_id>
<stop_location_id>BUS_STOP_4</stop_location_id>
<fare_type_cd>L</fare_type_cd>
<blacklist_cd />
</record>
<record>
<date_time>2003/12/10.11:20</date_time>
<tag_id>TAG_ID_1</tag_id>
<stop_location_id>BUS_STOP_5</stop_location_id>
<fare_type_cd>L</fare_type_cd>
<blacklist_cd />
</record>
<record>
<date_time>2003/12/10.11:22</date_time>
<tag_id>TAG_ID_7</tag_id>
<stop_location_id>BUS_STOP_5</stop_location_id>
<fare_type_cd>L</fare_type_cd>
<blacklist_cd>I</blacklist_cd>
</record>
<header>
<driver_id>DRIVER_ID_1</driver_id>
<vehicle_id>BUS_1</vehicle_id>
<duty_shift_id>AM</duty_shift_id>
<route_id>72X</route_id>
<cid_terminal_id>COV_1</cid_terminal_id>
</header>
<record>
<date_time>2003/12/10.8:10</date_time>
<tag_id>TAG_ID_1</tag_id>
<stop_location_id>BUS_STOP_1</stop_location_id>
<fare_type_cd>L</fare_type_cd>
<blacklist_cd />
</record>
<record>
<date_time>2003/12/10.8:10</date_time>
<tag_id>TAG_ID_3</tag_id>
<stop_location_id>BUS_STOP_1</stop_location_id>
<fare_type_cd>L</fare_type_cd>
<blacklist_cd />
</record>
<record>
<date_time>2003/12/10.8:30</date_time>
<tag_id>TAG_ID_5</tag_id>
<stop_location_id>BUS_STOP_2</stop_location_id>
<fare_type_cd>R</fare_type_cd>
<blacklist_cd />
</record>
<record>
<date_time>2003/12/10.8:30</date_time>
<tag_id>TAG_ID_5</tag_id>
<stop_location_id>BUS_STOP_2</stop_location_id>
<fare_type_cd>R</fare_type_cd>
<blacklist_cd />
</record>
<record>
<date_time>2003/12/10.8:45</date_time>
<tag_id>TAG_ID_4</tag_id>
<stop_location_id>BUS_STOP_3</stop_location_id>
<fare_type_cd>L</fare_type_cd>
<blacklist_cd>I</blacklist_cd>
</record>
<record>
<date_time>2003/12/10.10:00</date_time>
<tag_id>TAG_ID_2</tag_id>
<stop_location_id>BUS_STOP_4</stop_location_id>
<fare_type_cd>E</fare_type_cd>
<blacklist_cd />
</record>
<record>
<date_time>2003/12/10.10:10</date_time>
<tag_id>TAG_ID_6</tag_id>
<stop_location_id>BUS_STOP_4</stop_location_id>
<fare_type_cd>L</fare_type_cd>
<blacklist_cd />
</record>
<record>
<date_time>2003/12/10.11:20</date_time>
<tag_id>TAG_ID_1</tag_id>
<stop_location_id>BUS_STOP_5</stop_location_id>
<fare_type_cd>L</fare_type_cd>
<blacklist_cd />
</record>
<record>
<date_time>2003/12/10.11:22</date_time>
<tag_id>TAG_ID_7</tag_id>
<stop_location_id>BUS_STOP_5</stop_location_id>
<fare_type_cd>L</fare_type_cd>
<blacklist_cd>I</blacklist_cd>
</record>
</root>

Thanks for your help.
 

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,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top