Re: [xsl] Merging XML from Lookup table

Subject: Re: [xsl] Merging XML from Lookup table
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Sun, 22 Feb 2004 16:17:09 -0500
At 2004-02-22 10:37 -0800, Tracy wrote:
I have an XML in which data is coming from one source and another XML which
contains the matching IDs and their format is completely different (I can
modify the format if it doesn't work for the solution).

That is not necessary.

I need to generate an XML with matching IDs.

I wouldn't have understood your requirement from your one line description, but I can match your required outputs by taking advantage of multiple input documents in XSLT.

The feature is document() and the quick way to find information is through keys. In the example I load up two key tables from your table.xml and then process the input to the output adding entries from the key table. Note how I have to use a variable to save the value of a node in one context after changing the context to get at the key tables of the table document.

I hope this helps.

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

p.s. remember in XML that attribute order is not significant and can be in any order, so even though the order of the attributes doesn't match your requested output, the outputs are equivalent from an XML perspective.

T:\ftemp2>type data.xml
                <row num="2" zip="56780" typeID="1" />
                <row num="3" zip="98264" typeID="1" />
                <row num="5" zip="45424" typeID="2" />
                <row num="1" zip="45722" typeID="1" />
                <row num="4" zip="75565" typeID="2" />

T:\ftemp2>type table.xml
<table name="customer">
                <row num="1" name="Kate Hudson" />
                <row num="2" name="Jon Wright" />
                <row num="3" name="Craig Wills" />
                <row num="4" name="Ben Stills" />
                <row num="5" name="Le Yan" />
                <row num="6" name="Mc Kin" />
                <row num="7" name="Vincent Dsouza" />
<table name="customer">
                <row typeID="1" type="Lease" />
                <row typeID="2" type="Hire" />

T:\ftemp2>type tracy.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="";

<xsl:output indent="yes"/>

<xsl:key name="name-stuff" match="@name" use="../@num"/>
<xsl:key name="type-stuff" match="@type" use="../@typeID"/>

<xsl:template match="row">
  <xsl:variable name="num" select="@num"/>
  <xsl:variable name="typeID" select="@typeID"/>
    <xsl:copy-of select="@zip"/>
    <xsl:for-each select="document('table.xml')"><!--change context-->
      <xsl:copy-of select="key('name-stuff',$num)"/>
      <xsl:copy-of select="key('type-stuff',$typeID)"/>

<xsl:template match="@*|node()"><!--identity for all other nodes-->
    <xsl:apply-templates select="@*|node()"/>
                                         <!--this cleans up the output-->
<xsl:template match="text()[not(normalize-space())]"/>


T:\ftemp2>saxon data.xml tracy.xsl
<?xml version="1.0" encoding="utf-8"?>
      <row zip="56780" name="Jon Wright" type="Lease"/>
      <row zip="98264" name="Craig Wills" type="Lease"/>
      <row zip="45424" name="Le Yan" type="Hire"/>
      <row zip="45722" name="Kate Hudson" type="Lease"/>
      <row zip="75565" name="Ben Stills" type="Hire"/>

Public courses: Spring 2004 world tour of hands-on XSL instruction
Each week:   Monday-Wednesday: XSLT/XPath; Thursday-Friday: XSL-FO
United States: Washington, DC March 15; San Francisco, CA March 22
Finland April 26; Hong Kong May 17; Germany May 24; London June 07
World-wide on-site corporate, government & user group XML training

G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
Male Breast Cancer Awareness

XSL-List info and archive:

Current Thread