XSLT Lookup Help!!! Options

Discussion in 'XML' started by jkposey@gmail.com, May 17, 2007.

  1. Guest

    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:stylesheet .....>
    <xsl:eek:utput omit-xml-declaration="yes" method="xml" version="1.0" /
    >

    <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.
     
    , May 17, 2007
    #1
    1. Advertising

  2. 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



    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
     
    Joseph Kesselman, May 17, 2007
    #2
    1. Advertising

  3. Guest

    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?
     
    , May 18, 2007
    #3
  4. Pavel Lepin Guest

    <> wrote in
    <>:
    > 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.

    --
    Pavel Lepin
     
    Pavel Lepin, May 18, 2007
    #4
  5. >Can you provide a link to the XSLT FAQ you referenced?

    In case you're still looking:
    http://www.dpawson.co.uk/xsl/sect2/sect21.html
    In particular, see the sections titled document and lookup.

    Of course a websearch for "XSLT FAQ", or a glance through recent posts
    to this newsgroup, would have found this.

    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    Joe Kesselman, May 19, 2007
    #5
  6. Guest

    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 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>
     
    , May 23, 2007
    #6
  7. Pavel Lepin Guest

    Nice, helpful. Choose one.

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

    <> wrote in
    <>:
    > 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.

    --
    Pavel Lepin
     
    Pavel Lepin, May 24, 2007
    #7
  8. Guest

    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>
     
    , May 24, 2007
    #8
  9. Pavel Lepin Guest

    <> wrote in
    <>:
    > 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?

    --
    Pavel Lepin
     
    Pavel Lepin, May 24, 2007
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Adrian Charteris

    XSLT Lookup Tables Help.

    Adrian Charteris, Oct 15, 2004, in forum: XML
    Replies:
    4
    Views:
    955
    Adrian Charteris
    Oct 18, 2004
  2. Replies:
    18
    Views:
    2,762
    Joseph Kesselman
    Oct 4, 2006
  3. Cliff  Martin
    Replies:
    1
    Views:
    3,042
    Larry Smith
    Jan 31, 2007
  4. jacksu
    Replies:
    0
    Views:
    511
    jacksu
    Oct 9, 2007
  5. Utkado
    Replies:
    2
    Views:
    2,117
Loading...

Share This Page