XSLT Lookup Help!!! Options

J

jkposey

I have the need to lookup values based on a given code within my
mapping. I am trying to use custom XSLT and have not been able to
get
any of the examples I have found on this technique to work. I would
like to create an external xml file to hold my lookup values and
refer
to it from my map. Here is what I have:

Source Schema:
-Root
-Flower
-Name
-ColorCode


Destination Schema:
-Root
-Flower
-Name
-Color


I want to look up the color code in an external xml that looks like
this:
<colors>
<color><code>1</code><name>red</name></color>
<color><code>2</code><name>yellow</name></color>
</colors>


My map (without the lookup functionality I need) looks like this:
<xsl:template match="/">
<xsl:apply-templates select="/s0:Root" />
</xsl:template>
<xsl:template match="/s0:Root">
<ns0:Root>
<xsl:for-each select="Flower">
<Flower>
<Name>
<xsl:value-of select="Name" />
</Name>
<Color>
<xsl:value-of select="ColorCode" />
</Color>
</Flower>
</xsl:for-each>
</ns0:Root>
</xsl:template>
</xsl:stylesheet>


And I would like the output to get the color value and produce this:
<ns0:Root ...>
<Flower>
<Name>Rose</Name>
<Color>Red</Color>
</Flower>
<Flower>
<Name>Tulip</Name>
<Color>Yellow</Color>
</Flower>
</ns0:Root>


What is the best way to do this? Thanks for any help with this. An
example on how to accomplish would be great given my lack of success.
Thanks SO much.
 
J

Joseph Kesselman

Use the XSLT document() function to read the helper document, and use
xpaths against that to perform the lookups. It shouldn't be hard to find
examples of this; first place to start looking is probably the XSLT FAQ
website.

You may find that using keys to precalculate how the values in the table
map to each other significantly improves performance vs. having to
search the table each time to find the desired entry
 
J

jkposey

I still haven't been able to find a sample that has worked for me.
Can you provide a link to the XSLT FAQ you referenced?
 
P

Pavel Lepin

I still haven't been able to find a sample that has worked
for me. Can you provide a link to the XSLT FAQ you
referenced?

GIYF. Search terms should be obvious.
 
J

jkposey

Thanks you all for your responses. I have founds several articles on
lookups and still have not been able to get this functionality to
work. I found an article at http://www-128.ibm.com/developerworks/library/x-xsltip.html
that appears to be exactly what I would like to do. Unfortunately, I
can not get this to work. Can anyone help me find my error? I have
tried example after example with no luck.

colors.xml:
<?xml version="1.0" encoding="UTF-8"?>
<colors>
<color>
<cid>1</cid>
<cname>Red</cname>
</color>
<color>
<cid>2</cid>
<cname>Yellow</cname>
</color>
</colors>

source xml:
<ns0:Root xmlns:ns0="http://BizTest3.Flowers">
<Flower>
<Name>Rose</Name>
<ColorCode>1</ColorCode>
</Flower>
<Flower>
<Name>Tulip</Name>
<ColorCode>2</ColorCode>
</Flower>
</ns0:Root>

xslt doc:
<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet ...>
<xsl:eek:utput omit-xml-declaration="yes" method="xml" version="1.0" />
<xsl:key name="color-lookup" match="cname" use="cid"/>
<xsl:variable name="colors-top" select="document('colors.xml')/
colors"/>
<xsl:template match="/s0:Root">
<ns0:Root>
<xsl:for-each select="Flower">
<Flower>
<Name>
<xsl:value-of select="Name" />
</Name>
<Color>
</xsl:apply-templates>
</Color>
</Flower>
</xsl:for-each>
</ns0:Root>
</xsl:template>
<xsl:template match="colors">
<xsl:param name="curr-color"/>
<xsl:value-of select="key('color-lookup', $curr-color)/cname"/>
</xsl:template>

</xsl:stylesheet>
 
P

Pavel Lepin

Nice, helpful. Choose one.

For nice, delete the message and have a nice day. For
helpful, read on.

Thanks you all for your responses. I have founds several
articles on lookups and still have not been able to get
this functionality to work.

Well, you could've tried the obvious instead of messing with
the keys.
I found an article at
[DeveloperWorks]

that appears to be exactly what I would like to do.

Nice stuff. I wouldn't expect that to work.
Unfortunately, I can not get this to work. Can anyone
help me find my error?

Well, the key() doesn't seem to return anything, right? So
perhaps there's something wrong with your xsl:key. (Okay,
there *is* something wrong with you xsl:key. Read it again
and try to understand what it does and what you really want
it to do.)
xslt doc:
<?xml version="1.0" encoding="UTF-16"?>

Oh, great. Everybody loves BOMs.
<xsl:stylesheet ...>

A stroke of brilliance. What's the reason for dropping the
attributes and namespace declarations on xsl:stylesheet? So
that people trying to help you would have a harder time
running your example? Good thinking, lad.
<xsl:key name="color-lookup" match="cname" use="cid"/>

The error is right there, in plain sight, but since I spent
five minutes cursing vilely trying to make your example
work, I'll leave figuring out what exactly you did wrong to
you.

The usual: try understanding the magic before invoking it.
 
J

jkposey

My aplogies for excluding some of the details of my example, I was not
considering the fact that someone would take the time to run it
instead of just reading through the post. I appreciate your time, and
your sarcasm and am trying to understand the "magic" through working
with examples like the one I linked to. All that said, I still
haven't been able to find the error in the key. If you are able to
help, I would greatly appreciate it. I've copied the complete
versions of the files below. Thank you.

colors.xml:
<?xml version="1.0" encoding="UTF-8"?>
<colors>
<color>
<cid>1</cid>
<cname>Red</cname>
</color>
<color>
<cid>2</cid>
<cname>Yellow</cname>
</color>
</colors>

source xml:
<ns0:Root xmlns:ns0="http://BizTest3.Flowers">
<Flower>
<Name>Rose</Name>
<ColorCode>1</ColorCode>
</Flower>
<Flower>
<Name>Tulip</Name>
<ColorCode>2</ColorCode>
</Flower>
</ns0:Root>

xslt doc:
<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/
Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:n1="http://BizTest3.Flowers" xmlns:xs="http://www.w3.org/2001/
XMLSchema" xmlns="http://BizTest3.FlowersOut" exclude-result-
prefixes="n1 xs">
<xsl:eek:utput omit-xml-declaration="yes" method="xml" version="1.0" / <xsl:key name="color-lookup" match="cname" use="cid"/>
<xsl:variable name="colors-top" select="document('colors.xml')/
colors"/>
<xsl:template match="/s0:Root">
<ns0:Root>
<xsl:for-each select="Flower">
<Flower>
<Name>
<xsl:value-of select="Name" />
</Name>
<Color>
<xsl:apply-templates select="$colors-top">
<xsl:with-param name="curr-color"
select="ColorCode"/
</xsl:apply-templates>
</Color>
</Flower>
</xsl:for-each>
</ns0:Root>
</xsl:template>
<xsl:template match="colors">
<xsl:param name="curr-color"/>
<xsl:value-of select="key('color-lookup', $curr-color)/cname"/>
</xsl:template>
</xsl:stylesheet>
 
P

Pavel Lepin

I appreciate your time, and your sarcasm

That wasn't sarcasm, not really.
and am trying to understand the "magic" through working
with examples like the one I linked to.

There's no need to put magic into quotes. See Jargon
Dictionary, magic, sense 1. My point was that using magic
is inherently dangerous and often inefficient. See Jargon
Dictionary, voodoo programming, sense 1. Once you
understand a given piece of magic, though, it's no longer
magic and may be used much more safely.
colors.xml:
<?xml version="1.0" encoding="UTF-8"?>
<colors>
<color>
<cid>1</cid>
<cname>Red</cname>
</color>
<color>
<cid>2</cid>
<cname>Yellow</cname>
</color>
</colors>
<xsl:key name="color-lookup" match="cname" use="cid"/>

So. You define a key that applies to cname elements, and
uses the content of cid children elements to index said
cname elements.

Question: are there any cname elements with cid children in
your colors.xml document?

*Hint*: there aren't any.
<xsl:value-of select="key('color-lookup',
$curr-color)/cname"/>

Now you're trying to retrieve a cname element from
colors.xml document that has cid children with content that
equals to $curr-color value. After retrieving cname
element, you trying to get content of its cname children.

Question: are there any cname elements with cname children
in colors.xml?

Question: are there any cname elements in colors.xml that
would satisfy the condition given?

*Hint*: there aren't any.

Question: shouldn't you be trying to retrieve something else
entirely? A different element, perhaps?
 

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

Staff online

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,805
Latest member
ClydeHeld1

Latest Threads

Top