Performing a simple lookup in XSLT using XPath
Performing a simple lookup is a useful XML/XSLT pattern — previously we looked at using <xsl:key>, this time let’s look at different way of performing a lookup using XPath
In the previous XSLT lookup example, the XSLT elements <xsl:key> and <xsl:for-each> were used to help facilitate the lookup. For a large dataset <xsl:key> may be the more performant approach to take, but in programming there are always multiple ways to achieve the same outcome so it is worth being aware of a range of approaches— here is an example using XPath to perform a simple lookup.
Example — lookup without <xsl:key>, using XPath
document.xml
In this XML document we have a simple list of <lookup> elements within a <history> element. The aim will be to use the value of the <lookup> elements to pull in content from a different XML document (index.xml) during an XSLT transformation.
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="key-index.xsl"?>
<history>
<lookup>Tiberius</lookup>
<lookup>Caligula</lookup>
<lookup>Nero</lookup>
<lookup>Claudius</lookup>
</history>
index.xml
Here is a simple XML document with a list of <entry> elements with attribute “name” and the value of the attributes match the values of the elements in the document.xml.
<?xml version="1.0" encoding="UTF-8"?>
<index>
<entry name="Tiberius">Tiberius (Tiberius Julius Caesar Augustus) was emperor from AD 14 until 37.</entry>
<entry name="Caligula">Caligula (Gaius Caesar Augustus Germanicas) was emperor from AD 37 until his assassination in AD 41.</entry>
<entry name="Claudius">Claudius (Tiberius Claudius Caesar Augustus) ruled from AD 41 to 54.</entry>
<entry name="Nero">Nero (Claudius Caesar Augustus Germanicas) was the final emperor of the Julio-Claudian dynasty, reigning from AD 54 until AD 68.</entry>
</index>
What we want to achieve in our XSLT transformation is a Lookup from index.xml, given the values in document.xml e.g. — we want to <lookup>Claudius</lookup> in document.xml should pull in the value of <entry name=”Claudius”>…</entry> in index.xml
match-lookup.xsl
In the previous XSLT lookup example, the XSLT elements <xsl:key> and <xsl:for-each> were used to help facilitate the lookup. For a large dataset <xsl:key> may be the more performant approach to take.previous example.
However we can achieve a lookup with an XPath expression instead, by matching on the element value of <lookup> and the attribute value of <entry> in index.xml.
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="lookup">
<xsl:variable name="name" select="."/>
<xsl:value-of select="document('index.xml')/index/entry[@name=$name]"/>
</xsl:template>
</xsl:stylesheet>
If you want to learn more about XSLT, how to use it and understand its place in the Modern Web — follow to learn more.
- Beginners XSLT patterns explained — simple key lookup
- What is XSLT and is it still relevant in a website context in 2024?
- XSLT in the web browser — simple transformations on the server
- Using jQuery with XSLT — .ajax(), .ajax() with Promises, .get()
- XSLT with Fetch() API — modern Javascript with/without Async and Await