The Original FileMaker Community
Business Templates - Demo Apps - Video Tutorials -Samples - Help - 46000 Member Forum

The Leading Filemaker Developer Tools

Extensible Stylesheet Language Transformation (XSLT) – FileMaker Today

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="">

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.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy