Ben-
Great question!
You might need to change your lookup table slightly. Perhaps something
like this:
<?xml version="1.0"?>
<xref>
<trans file="file1" orig="col1" new="label1" />
<trans file="file1" orig="col2" new="label2" />
<trans file="file1" orig="col3" new="label3" />
<trans file="file1" orig="col4" new="label4" />
<trans file="file1" orig="col5" new="label5" />
<trans file="file2" orig="col1" new="label1" />
<trans file="file2" orig="col2" new="label2" />
</xref>
This is because the <abbr> and <name> nodes are siblings, and therefore
relying solely on the document order to figure out which to use for the
replacement is risky. As far as the file attribute goes, It would
certainly be nicer to group them in a "file1" element (or whatever), but
I'm not sure how to modify the XPath expression below (using the
'document( )' function to accept a variable as a node name. Perhaps
someone on the list might be able to help out there.
You might try something similar to the following (this should be run
against your original XML document):
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" />
<xsl:variable
name="lookup-table">/home/heddings/tmp/lookup.xml</xsl:variable>
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="*">
<xsl:call-template name="do-translation">
<xsl:with-param name="file" select="name( )" />
</xsl:call-template>
</xsl:template>
<xsl:template name="do-translation">
<xsl:param name="file" />
<xsl:element name="{$file}">
<xsl:for-each select="*">
<xsl:variable name="orig-elem-name" select="name( )" />
<xsl:variable name="new-elem-name" select="document( $lookup-table
)/xref/
trans[@file=$file][@orig=$orig-elem-name]/@new" />
<xsl:variable name="text" select="." />
<xsl:element name="{$new-elem-name}">
<xsl:value-of select="$text" />
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
** Note that above, all <xsl:variable .../> elements are on one line
(they may get wrapped in the mail).
You can see that it will go into the lookup-table for every col(n) that
it finds in the source XML, thus making it a little slow (to the
computer at least).
Feel free to ask for clarification on anything.
HTH,
--jah
[EMAIL PROTECTED] wrote:
>
> Hi,
>
> I would like to transform from one xml into another one
> using a 'lookup' xml file.
>
> Here is the original xml file :
>
> <?xml version="1.0"?>
> <file1>
> <col1>01</col1>
> <col2>Hello</col2>
> <col3>world</col3>
> <col4>GTP</col4>
> <col5>02 Jul 1999 09:45:05:706</col5>
> </file1>
>
> using this 'lookup' xml file :
> <?xml version="1.0"?>
> <xref>
> <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>
> </xref>
>
> the xml result is :
> <?xml version="1.0"?>
> <file1>
> <label1>01</label1>
> <label2>Hello</label2>
> <label3>world</label3>
> <label4>GTP</label4>
> <label5>02 Jul 1999 09:45:05:706</label5>
> </file1>
>
> If the original xml file :
>
> <?xml version="1.0"?>
> <file2>
> <col1>12345</col1>
> <col2>Welcome</col2>
> </file2>
>
> using the same 'lookup' xml file, the result is :
> <?xml version="1.0"?>
> <file2>
> <header1>12345</header1>
> <header2>Welcome</header2>
> </file2>
>
> Any idea how to ?
> Thanks for any suggestions.
>
> ben.
>
> ************************************************************
> JLT Management Services Limited
> 6 Crutched Friars, London EC3N 2PH. Co Reg No 1536540
> Tel: (44) (0)20 7528 4000 Fax: (44) (0)20 7528 4500
> http://www.jltgroup.com
> ------------------------------------------------------------
> The content of this e-mail (including any attachments) as
> received may not be the same as sent. If you consider that
> the content is material to the formation or performance of
> a contract or you are otherwise relying upon its accuracy,
> you should consider requesting a copy be sent by facsimile
> or normal mail. The information in this e-mail is
> confidential and may be legally privileged. If you are not
> the intended recipient, please notify the sender immediately
> and then delete this e-mail entirely - you must not retain,
> copy, distribute or use this e-mail for any purpose or
> disclose any of its content to others.
>
> Opinions, conclusions and other information in this e-mail
> that do not relate to the official business of JLT
> Management Services Limited shall be understood as neither
> given nor endorsed by it. Please note we intercept and
> monitor incoming / outgoing e-mail and therefore you should
> neither expect nor intend any e-mail to be private in nature.
>
> We have checked this e-mail for viruses and other harmful
> components and believe but not guarantee it virus-free prior
> to leaving our computer system. However, you should satisfy
> yourself that it is free from harmful components, as we do
> not accept responsibility for any loss or damage it may
> cause to your computer systems.
> ************************************************************
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\\\|/// Jason Heddings ((
\\ ~ ~ // 303.272.5166 (x75166) C|~~|
(/ @ @ /) [EMAIL PROTECTED] `__'
~~oOOo~(_)~oOOo~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~