XSLT Tips and Tricks

Common XSLT Code Samples

IT Support Services

Our team works with a lot of XML!

Extensible Markup Language (XML) is a markup language that many explain as being “self-describing”.  In other words, you make it up yourself.  XML can be used to help represent data structures and is commonly used in web services, configuration files for applications, and in some cases (depending on the application) the raw storage mechanism for transactional data.

XML has become one of the most widely accepted formats for representing and transmitting information.  While JSON (JavaScript Object Notation) has greatly grown in popularity, XML is still widely used throughout enterprises.  Knowing how to manipulate it is very important, and we’ve found no better tool for manipulating XML than XSLT!

Learning XSLT

XSLT (Extensible Stylesheet Language Transformations) is a software language for transforming XML documents into other types of documents.  You can use XSLT to convert XML to other forms of XML, HTML, Text Files, etc.  Knowing how to manipulate XML through XSL is a powerful skill to have at your disposal.

Implementation Steps:

  • Using a text editor, create an “*.xsl” file.  The * can represent whatever name you’d like the file to have.
  • You will need to create an “*.xml” file as well.  This will be the raw representation of your data.  You will be transforming your .xml file into another form of XML, in this example.  As previously discussed, XML can be transformed via XSLT into a variety of different formats.
  • You will need an XSL pre-processor to convert XML to XSL.  There are various tools in the marketplace that can be used. A development tool we like to use is called Stylus Studio.  Feel free to contact us if you’d like to know the software applications we prefer using for manipulating XML.

<?xml version=”1.0″?>
<Customers>
<Customer>
<Name>Fizen™</Name>
<Address1>100 S. Ashley Dr, Ste #600</Address1>
<City>Tampa</City>
<State>FL</State>
<Zip>33602</Zip>
</Customer>
</Customers>

The above XML example represents a customer.  XML is self-describing, which means YOU define the elements and their meaning.  In this case we have outlined a data representation of a customer record.

Creating an XSL File

The following XSL code snippet can be placed into your *.xsl file.

  • This XSLT example passes through and preserves XML, while stripping out whitespace.
  • It can be very handy to have utilities that can cleanse and pass through XML data for further processing, as whitespace is not always handled in a uniform way between systems.

<?xml version=”1.0″?>
<xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”>
<xsl:strip-space elements=”*”/>
<xsl:template match=”/”>
<xsl:copy-of select=”*”/>
</xsl:template>
</xsl:stylesheet>

Expounding upon our previous example a bit more, let’s say we want to convert this raw XML representation of a customer to HTML.

  • The following code snippet will covert the XML customer record, into an HTML table.
  • Notice the use of a for-each loop, a control flow statement that helps specify an iteration.
  • Take note as well of the XPATH statements (look for the “select” attributes in the XSLT example).
  • With XSL or XSLT, you will need at times to clarify to your code where or what path to traverse in your associated XML document.

<?xml version=”1.0″?>
<xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”>
<xsl:output method=”html”/>
<xsl:strip-space elements=”*”/>
<xsl:template match=”/”>
<xsl:element name=”table”>
<xsl:attribute name=”border”>
<xsl:text>1</xsl:text>
</xsl:attribute>
<xsl:for-each select=”Customers/Customer”>
<xsl:element name=”tr”>
<xsl:for-each select=”*”>
<xsl:element name=”td”>
<xsl:value-of select=”./text()”/>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

About Fizen™

Fizen™ is a B2B IT Outsourcing and Technology Partner. We are IT practitioners that strive to deliver scalable and practical solutions for our customers.

For more information, please feel free to contact us.