Page 1 of 1

XSLT Lookups

Posted: Fri Sep 26, 2025 2:01 am
by jacobvz
Good Morning


I have an xml document that has two fields I need to lookup in another XML and translate the values (genre and sub-genre)

This is the lookup.
<lookup>
<genre id="Entertainment" ibmsGenre="LIGHT ENTERTAINMENT">
<sub-genre id="Light Entertainment" ibmsSubGenre="OTHER LIGHT ENTERTAINMENT" />
<sub-genre id="Travel" ibmsSubGenre="TRAVEL" />
</genre>
<genre id="Drama" ibmsGenre="DRAMA">
<sub-genre id="Reality" ibmsSubGenre="REALITY DRAMA" />
<sub-genre id="Travel" ibmsSubGenre="TRAVEL" />
</genre>
<lookup>


so I can get the values out of the original XML and when I only need to translate one that is fine and I can get that to work. THe issue I can not seem to get working is foloowing

I need to get the genre from the original, loop through the subgenres under that specific genre and find the matching sub-genre.

I have only been at this XSLT for about a week so if anyone can help out with this I would greatly appreciate it.
*I am pretty sure i will need a loop inside my main loop somewhere, but all my attempts sofar do not work
** I can re-format the lookup XML as that part is not fixed, if that helps with the solution

Re: XSLT Lookups

Posted: Fri Sep 26, 2025 10:33 am
by tavy
I used the "XSLT Stylesheet generated with AI" template from Oxygen AI Positron and, based on your description, created the following code. Perhaps this can serve as a starting point.

Code: Select all

<xsl:stylesheet version="3.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:lkp="urn:lookup"
    exclude-result-prefixes="lkp">
    <xsl:output method="xml" indent="yes"/>
    <!-- Assume the main input is the source XML, and the lookup XML is provided as a secondary document -->
    <!-- Example: <xsl:variable name="lookup" select="doc('lookup.xml')/lookup"/> -->
    <!-- For demonstration, we assume the lookup XML is available as a global variable -->

    <xsl:variable name="lookup">
        <lookup xmlns="urn:lookup">
            <genre id="Entertainment" ibmsGenre="LIGHT ENTERTAINMENT">
                <sub-genre id="Light Entertainment" ibmsSubGenre="OTHER LIGHT ENTERTAINMENT"/>
                <sub-genre id="Travel" ibmsSubGenre="TRAVEL"/>
            </genre>
            <genre id="Drama" ibmsGenre="DRAMA">
                <sub-genre id="Reality" ibmsSubGenre="REALITY DRAMA"/>
                <sub-genre id="Travel" ibmsSubGenre="TRAVEL"/>
            </genre>
        </lookup>
    </xsl:variable>

    <!-- Root template: match the root of your source XML -->
    <xsl:template match="/">
        <results>
            <xsl:apply-templates select="/*/*"/>
        </results>
    </xsl:template>

    <!-- Example: match an 'item' element in your source XML -->
    <xsl:template match="item">
        <item>
            <xsl:variable name="genre" select="genre"/>
            <xsl:variable name="subgenre" select="sub-genre"/>

            <!-- Lookup genre translation -->
            <xsl:variable name="lkpGenre"
                select="$lookup/lkp:lookup/lkp:genre[@id = $genre]"/>

            <genre>
                <xsl:value-of select="$lkpGenre/@ibmsGenre"/>
            </genre>

            <!-- Lookup sub-genre translation under the matched genre -->
            <sub-genre>
                <xsl:value-of select="$lkpGenre/lkp:sub-genre[@id = $subgenre]/@ibmsSubGenre"/>
            </sub-genre>
        </item>
    </xsl:template>

</xsl:stylesheet>