Log in
Hot & Trending on FMT

Extensible Stylesheet Language Transformation (XSLT)

Featured Extensible Stylesheet Language Transformation (XSLT)

I recently had a reason to use Extensible Stylesheet Language Transformations (XSLT) on a project and thought I would give a little tutorial and share some information on the topic. 

XSLT is a templating language used to create and transform XML documents, similar to the way that CSS files are used to transform HTML documents.  A notable difference, however, is that HTML tags are defined by the standard. 

When I want my CSS to target an HTML <span> tag, the CSS knows what to look for because a <span> tag will always look the same.  In XML, tags are not predefined.  That is to say, I can create an XML tag with whatever name I choose.  This forces XSLT to be more dynamic, and it achieves this dynamism through a tool called XPATH, which is used to traverse elements and attributes in XML.

In short, templates are created using XPATH that will match parts of the source XML document.  When it finds a match, XSLT will change the matched section of the source XML into your destination XML or XHTML document.

Now that you have a basic understanding of the concept behind XSLT, let’s get into a brief example.  Let’s say you have some software that tracks a library of books.  This software can give you an XML output of the library. 

You want to transform that XML dump into a format that can be published on your website.  We can use XSLT to accomplish this fairly easily.  A sample of the XML output that we have looks like this: 

<?xml version="1.0" encoding="ISO-8859-1"?>
    <title>Enders Game</title>
    <author>Orson Scott Card</author>
    <author>Alan Moore</author>

The first thing to note is how to declare an XSL style sheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

Putting this declaration at the top of our document will give access to the necessary XSLT features.  Then we will create an XSL Style Sheet to transform our XML into XHTML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
<xsl:template match="/">
  <h2>Nick's Book Collection</h2>
    <xsl:for-each select="library/book">
      <td><xsl:value-of select="title"/></td>
      <td><xsl:value-of select="author"/></td>
      <td><xsl:value-of select="yearpublished"/></td>

This creates a fairly simple template that contains a foreach loop that will iterate each <book> tag, retrieving the title, author, and year published values while placing them into HTML <table> rows.  The only thing left would be to link to your XSL stylesheet in your XML document with a tag that looks something like this:

<?xml-stylesheet type=”text/xsl” href=”xslstylesheet.xsl”?>

Now when the browser loads your page, it will be a nicely formatted XHTML document. 

I hope this beginner example is helpful.  There is a lot more to XSLT and it can do some surprisingly powerful stuff.  This post should help you get a basic understanding of how it works so that you can start transforming your XML however you need.

Soliant Consulting

Soliant Consulting employs the largest FileMaker development team in the world. We have a deep bench of talent starting with our CEO, Bob Bowers: he's co-authored seven books, led the team that has written six editions of the Authorized Training Series for FileMaker, Inc., spoken at more than a dozen FileMaker Developer Conferences, and has taught literally thousands of students spanning two decades of leadership in the field. Certified Expertise Soliant's team is certified in every version of FileMaker Pro and are specialists in building high-performance databases, hybrid web-FileMaker applications, integrations with a range of technologies including SQL, and conversions from prior versions. We work with your solution as is or can build from scratch. Find out how we can take your FileMaker databases to the next level.

Website: www.soliantconsulting.com/filemaker