By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 974 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

match muliple header records to associated detail records

P: n/a
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:output method="text"/>

<xsl:variable name="delim" select="'&#x9;'"/> <!-- tab -->
<xsl:variable name="nl" select="'&#xA;'"/> <!-- 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.
Jul 20 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.