XSLT Lookup Tables Help.

A

Adrian Charteris

Hi I'm currently trying to use a lookup table for converting one xml
doc to another using a XSLT transformation. Ideally I would like my
first xml doc to be converted to my second xml doc below.

All that I want is to replace node names with a matching value in the
lookup table and place the result into an field attribute pair:

Example: id to be renamed instrument_id thus
<id type="master">asset #132</id> becomes
<field Name="instrument_id" IsUnique="Y">asset #132</field>

My problem is that none of the name matches are being output in the
resulting xml document.


My first XML has an asset request:

<pluginRequest>
<senderRef>store</senderRef>
<full>
<asset type="bondFuture">
<id type="master">asset #132</id>
<Category>Bond Future</Category>
<HiPort>DG Z4</HiPort>
<Bloomberg>G Z4 Index</Bloomberg>
<Expiry>29-Dec-04</Expiry>
</asset>
</full>
</pluginRequest>

My desired output:

<LzMessage RequestReference="1">
<LzCreateInstrumentRequest>
<field name="instrument_id" IsUnique="Y">asset #132</field>
<field name="name" IsUnique="N">Bond Future</field>
...etc
<field name="expiration_date" IsUnique="N">29-Dec-04</field>
</LzCreateInstrumentRequest>
</LzMessage>

My actual result:

<LzMessage RequestReference="1"
xmlns:ren="http://www.ora.com/namespaces/rename">
<LzCreateInstrumentRequest>
<field name="id" IsUnique="Y">asset #132</field>
<field name="Category" IsUnique="N">Bond Future</field>
<field name="Expiry" IsUnique="N">29-Dec-04</field>
...etc
</LzCreateInstrumentRequest>
</LzMessage>


My XSLT doc is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ren="http://www.ora.com/namespaces/rename">
<xsl:eek:utput method="xml" version="1.0" encoding="UTF-8" indent="yes"
omit-xml-declaration="yes"/>

<xsl:variable name="lookup" select="document('')/*[ren:*]"/>

<ren:element from="id" to="instrument_id"/>
<ren:element from="Category" to="name"/>
<ren:element from="Expiry" to="expiration_date"/>

<xsl:template match="/">
<LzMessage>
<xsl:attribute name="RequestReference">
<xsl:value-of select="1"/>
</xsl:attribute>
<xsl:apply-templates/>
</LzMessage>
</xsl:template>

<xsl:template match="senderRef">
<xsl:apply-templates select="full"/>
</xsl:template>

<xsl:template match="full">
<LzCreateInstrumentRequest>
<xsl:apply-templates select="asset"/>
</LzCreateInstrumentRequest>
</xsl:template>

<!-- Asset -->
<xsl:template match="asset">
<xsl:for-each select="*">
<field>
<xsl:choose>
<xsl:when test="$lookup/ren:element[@from=name(current())]">
<xsl:attribute name="name">
<xsl:value-of select="$lookup/ren:element[@from=name(current())]/@to"/>
</xsl:attribute>
</xsl:when>

<xsl:eek:therwise>
<xsl:attribute name="name">
<xsl:value-of select="local-name(.)"/>
</xsl:attribute>
</xsl:eek:therwise>
</xsl:choose>

<xsl:choose>
<xsl:when test="local-name(.) = 'id'">
<xsl:attribute name="IsUnique"><xsl:text>Y</xsl:text></xsl:attribute>
</xsl:when>

<xsl:eek:therwise>
<xsl:attribute name="IsUnique"><xsl:text>N</xsl:text></xsl:attribute>
</xsl:eek:therwise>
</xsl:choose>
<xsl:value-of select="."/>
</field>
</xsl:for-each>
</xsl:template>
<!--End Asset-->
</xsl:stylesheet>

Please help! What am I doing wrong?
Adrian
 
J

Joris Gillis

My desired output:
<LzMessage RequestReference="1">
<LzCreateInstrumentRequest>
<field name="instrument_id" IsUnique="Y">asset #132</field>
<field name="name" IsUnique="N">Bond Future</field>
...etc
<field name="expiration_date" IsUnique="N">29-Dec-04</field>
</LzCreateInstrumentRequest>
</LzMessage>

My actual result:

<LzMessage RequestReference="1"
xmlns:ren="http://www.ora.com/namespaces/rename">
<LzCreateInstrumentRequest>
<field name="id" IsUnique="Y">asset #132</field>
<field name="Category" IsUnique="N">Bond Future</field>
<field name="Expiry" IsUnique="N">29-Dec-04</field>
...etc
</LzCreateInstrumentRequest>
</LzMessage>

Hi,

Your XSLT gives the 'desired' output with me (using Saxon).
Maybe your processor cannot handle tree fragments in variables.
You could try using:
<xsl:when test="document('')//ren:element[@from=name(current())]"/>
and omit the use of $lookup ?

By the way, I really like your idea of the lookup table for attribute
renaming.

regards,
 
R

Richard Tobin

Joris Gillis said:
Your XSLT gives the 'desired' output with me (using Saxon).

And with all the processors I have to hand. Which one does it not
work with?
Maybe your processor cannot handle tree fragments in variables.

There isn't actually a result-tree fragment there: it's opening the
stylesheet itself with document('') and the value of the variable is
just a node set from that document.

The most likely thing seems to be that your processor doesn't handle
document('') properly. You could try putting the table in a different
file.

-- Richard
 
A

Adrian Charteris

Thanks for taking the time to look at my problem.
I'm currently developing in C# on windows XP with MSXML4.
I call my transformation something like this:

xslt.Transform(xpathDocument, null, xmlMemoryStream, null);

I did try using
document('')//ren:element[@from=name(current())]
instead of the $lookup variable, but no change in output. I have a nasty
feeling that MSXML4 doesn't support the document() function call, I know
ealier versions didn't.

I don't have access to any other parsers unfortunately.

Any alternative approach suggestions as to using lookup tables would be
great as the lookup table is exactly what I require else I will have to
write serveral hundred templates, which is not idealic.
 
A

Adrian Charteris

Thanks for taking the time to look at my problem.
I'm currently developing in C# on windows XP with MSXML4.
I call my transformation something like this:

xslt.Transform(xpathDocument, null, xmlMemoryStream, null);

I did try using
document('')//ren:element[@from=name(current())]
instead of the $lookup variable, but no change in output. I have a nasty
feeling that MSXML4 doesn't support the document() function call, I know
ealier versions didn't.

I don't have access to any other parsers unfortunately.

Any alternative approach suggestions as to using lookup tables would be
great as the lookup table is exactly what I require else I will have to
write serveral hundred templates, which is not idealic.

Adrian Charteris
BA/Developer
Insight Investments
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top