XSLT Lookups
Here should go questions about transforming XML with XSLT and FOP.
-
- Posts: 1
- Joined: Fri Sep 26, 2025 1:52 am
XSLT Lookups
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
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
-
- Posts: 394
- Joined: Thu Jul 01, 2004 12:29 pm
Re: XSLT Lookups
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>
Octavian Nadolu
<oXygen/> XML Editor
http://www.oxygenxml.com
<oXygen/> XML Editor
http://www.oxygenxml.com
Jump to
- Oxygen XML Editor/Author/Developer
- ↳ Feature Request
- ↳ Common Problems
- ↳ DITA (Editing and Publishing DITA Content)
- ↳ Artificial Intelligence (AI Positron Assistant add-on)
- ↳ SDK-API, Frameworks - Document Types
- ↳ DocBook
- ↳ TEI
- ↳ XHTML
- ↳ Other Issues
- Oxygen XML Web Author
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Content Fusion
- ↳ Feature Request
- ↳ Common Problems
- Oxygen JSON Editor
- ↳ Feature Request
- ↳ Common Problems
- Oxygen PDF Chemistry
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Feedback
- ↳ Feature Request
- ↳ Common Problems
- Oxygen XML WebHelp
- ↳ Feature Request
- ↳ Common Problems
- XML
- ↳ General XML Questions
- ↳ XSLT and FOP
- ↳ XML Schemas
- ↳ XQuery
- NVDL
- ↳ General NVDL Issues
- ↳ oNVDL Related Issues
- XML Services Market
- ↳ Offer a Service