Re: [xsl] Generic Lookup (Element Mapping)

Subject: Re: [xsl] Generic Lookup (Element Mapping)
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Tue, 07 May 2002 11:23:38 -0400
At 2002-05-07 08:15 -0500, Faroukh Fekravar wrote:
I have been trying to write a generic xslt template to map the element names
based on a lookup table.

Is there a reason you want to do this in one stylesheet with both the mapping file and the data file?

The lookup xml file has always a subset of the elements and therefore I do
not want to hard code the element names.

Then you need a customized stylesheet based on your mappings, especially for speed in order to do a single pass on the input data.

The output file is a flat xml file in opposite to the input file.

This was a particularly difficult criterion to meet.

But my main problem is how navigate through the input file in a generic
I'm also just a little concern about the overall performance because the
input file might be MBs big.

For performance reasons, I recommend you write a stylesheet that, based on your mapping, emits a single stylesheet that is then run against your data. This emitted stylesheet does simple matches and should meet your performance requirements.

An example of writing a stylesheet that writes a stylesheet is in Chapter 5 of "Definitive XSLT and XPath" which happens to be the freely-downloadable chapter made available by Prentice Hall on the Amazon site. It is also in Chapter 5 of our electronic book from which the paper book was derived.

A working answer to your requirements is below.

I hope this helps.

...................... Ken

t:\ftemp>type data.xml <?xml version="1.0" encoding="iso-8859-1"?> <project> <aa1> <aa11>aa11_value</aa11> <aa12>aa12_value</aa12> <aa13>aa13_value</aa13> <aa2> <aa21>aa21_value</aa21> <aa22>aa22_value</aa22> <aa3> <aa31>aa31_value</aa31> <aa4> <aa41>aa41_value</aa41> </aa4> </aa3> </aa2> </aa1> </project>

t:\ftemp>type lookup.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<Map> <!-- combination of SEName, SAname and TEname is unique through
the file.-->
<SEname>aa1</SEname> <!-- element name in the input file-->
<SAname>aa11</SAname> <!-- element name in the input file, it's always
the child element of SEName-->
<TEname>aa_1</TEname> <!-- element name in output-->
<TAname>aa_11</TAname> <!-- element nemae in output, it should be the
child element of TEname-->
<!-- no mapping for aa22 -->

t:\ftemp>type faroukh.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="";

<xsl:namespace-alias stylesheet-prefix="xslo" result-prefix="xsl"/>

<xsl:key name="parents" match="Map" use="SEname"/>
<xsl:key name="children" match="Map" use="concat(SEname,' ',TEname)"/>

<xsl:template match="/">
  <xslo:stylesheet version="1.0">
    <xslo:output indent="yes"/>
    <xslo:template match="/project"><!--preserve project element-->
    <xslo:template match="*"/><!--throw away unmapped-->
    <xslo:template match="text()"/><!--only keep explicitly copied text-->

<xsl:template match="Map">
<!--only put out a single template for the parent-->
<xsl:if test="generate-id(.)=generate-id(key('parents',SEname))">
<xslo:template match="{SEname}">
<!--needs as many apply templates as unique parent mappings-->
<xsl:for-each select="key('parents',SEname)[generate-id(.)=
generate-id(key('children',concat(SEname,' ',TEname)))]">
<xslo:element name="{TEname}">
<xslo:apply-templates mode="mapping">
<xsl:attribute name="select">
<xsl:for-each select="key('children',concat(SEname,' ',TEname))">
<xsl:if test="position()>1">|</xsl:if>
<xsl:value-of select="SAname"/>
<xslo:template match="{SEname}/{SAname}" mode="mapping">
<xslo:element name="{TAname}">
<xslo:value-of select="."/>


t:\ftemp>saxon -o mapping.xsl lookup.xml faroukh.xsl

t:\ftemp>saxon -o faroukh.out data.xml mapping.xsl

t:\ftemp>type faroukh.out
<?xml version="1.0" encoding="utf-8"?>
t:\ftemp>rem Done!

-- Upcoming: 3-days XSLT/XPath and/or 2-days XSLFO: June 17-21, 2002 - : 3-days XML Information Modeling: July 31-August 2, 2002

G. Ken Holman                mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.
Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (Fax:-0995)
ISBN 0-13-065196-6                      Definitive XSLT and XPath
ISBN 1-894049-08-X  Practical Transformation Using XSLT and XPath
ISBN 1-894049-07-1               Practical Formatting Using XSLFO
XSL/XML/DSSSL/SGML/OmniMark services, books(electronic, printed),
articles, training(instructor-live,Internet-live,web/CD,licensed)
Next public training:               2002-05-06,07,09,10,13,15,20,
-                    06-04,07,10,11,13,14,17,20,07-31,08-05,27,30

XSL-List info and archive:

Current Thread