Re: [xsl] xsl:transform

Subject: Re: [xsl] xsl:transform
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Fri, 22 Jun 2001 16:44:13 +0100
Hi Ben,

At 08:19 PM 6/22/01, you wrote:
>I would like to transform from one xml into another one
>using a 'lookup'  xml file....

Assuming your lookup file is in a file 'lookup.xml' in the same subdirectory as your input, try

<xsl:stylesheet version="1.0"

<xsl:variable name="lookup" select="document('lookup.xml')/xref"/>

<xsl:template match="/*">
<xsl:variable name="whichfile" select="local-name()"/>
<xsl:for-each select="*">
<xsl:element name="$lookup/*[local-name()=$whichfile]/abbr[.=local-name(current())]/following-sibling::name"/>


(Not tested!)

Please ask about anything you don't understand here. There's alot of fancy logic buried in the XPath. Since it would require the patience of Jeni to explain, I'll just let you ask about the mysterious parts.

Well, okay....

First, a variable is declared, $lookup, which lets us get at the lookup table easily. It'll be the xref node in the lookup table (a good place to reach from).

The template matches the element child of the root (the document element), in your examples, either a <file1> or a <file2> element.

Its name is bound to the variable $whichfile. Then the element is copied to our result tree.

Within this element, we iterate over its element children. These are the <col1> through <col5> elements in your examples.

For each of these nodes, we create an element in our output. Its name is determined as follows:


or, in English, from the lookup xref, step to the element child(ren) whose name is the same as $whichfile (remember, the name of the element I'm inside of, either 'file1' or 'file2' as you have it), then to the 'abbr' element child(ren) whose content is the same as the name of the element we're now working with (the col1, col2 or whatever), then to the subsequent 'name' element(s). If your lookup table is correctly constructed, there'll be only one of these. If there are more than one, the name will be the name of the first one.

(If this breaks, it's possible that we may have to wrap this whole expression in a string() function: I don't remember.)

Within this new element, the content is processed (copying text node content to output by default).

Hoping it works,

>Here is the original xml file :
><?xml version="1.0"?>
>    <col1>01</col1>
>    <col2>Hello</col2>
>    <col3>world</col3>
>    <col4>GTP</col4>
>    <col5>02 Jul 1999 09:45:05:706</col5>
>using this 'lookup' xml file :
><?xml version="1.0"?>
>    <file1>
>       <abbr>col1</abbr><name>label1</name>
>       <abbr>col2</abbr><name>label2</name>
>       <abbr>col3</abbr><name>label3</name>
>       <abbr>col4</abbr><name>label4</name>
>       <abbr>col5</abbr><name>label5</name>
>    </file1>
>    <file2>
>       <abbr>col1</abbr><name>header1</name>
>       <abbr>col2</abbr><name>header2</name>
>    </file2>
>the xml result is :
><?xml version="1.0"?>
>    <label1>01</label1>
>    <label2>Hello</label2>
>    <label3>world</label3>
>    <label4>GTP</label4>
>    <label5>02 Jul 1999 09:45:05:706</label5>
>If the original xml file :
><?xml version="1.0"?>
>    <col1>12345</col1>
>    <col2>Welcome</col2>
>using the same 'lookup' xml file, the result is :
><?xml version="1.0"?>
>    <header1>12345</header1>
>    <header2>Welcome</header2>
>Any idea how to ?
>Thanks for any suggestions.

Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.      
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
  Mulberry Technologies: A Consultancy Specializing in SGML and XML

XSL-List info and archive:

Current Thread