XSLT

Page - 1 -|  2  |  3  |  4  

   
Intro

 

XML is similar to HTML markup language. But there's not, typically, any formatting or layout information included in simple XML files. This can be transformed, however, into HTML using an XSLT tranformation. Like server-side CGI languages, the XML is treated as a data document with information to go in certain 'slots' of a common template, to be output as HTML. Like some of those server scripts, XSLT can not only be run on the server, but is a full programming language, itself. XSLT can also be run right off a desktop or 'mobile' computer from .dll libraries, without any need for server software.

There are two general languages in XSL (XML Stylesheet Language) - XSLT (XSL Transform) and XSL-FO (XSL Formatting Objects). XSLT can be used to take XML and transform it into a different XML, into another version of XSLT, or into HTML (which is the typical use), or plain text. XSL-FO is typically used to transform XML into Adobe's PDF format (essentially bypassing the need to use Adobe's own Acrobat PDF generator). Use of XSLT is generally referred to as a, transform, and the XSLT program file typically uses the suffix, .xsl. Use of XSL-FO is generally referred to as, formatting.

XSLT is a 'functional', or 'declarative', language. It differs from something like javascript, or BASIC, or C++, in that variables, as such, are only set once, as constants. Changing variable values generally requires the use of recursion. And such declarative languages, therefore, use recursion for many things that are not commonly done by resort to recursion in other programming languages.

 
Templates

 

XSLT is designed around the idea of a template. Data from the XML document is fed into various templates. It's just that simple. And the templates, themselves, may contain the various HTML tags, if one is transforming XML to HTML. The XML is used to fill in 'holes' or slots in the template(s).

One can put in a call to transform from an XML document, directly. If, say, you have an XSLT program in a file called, trans.xsl, then at the top of your XML document:

<?xml-stylesheet type="text/xsl" href="trans.xsl"?>
 

If you open the XML document, you will automatically run the transform, and the result will be displayed in Microsoft Internet Explorer.

What might a simple XSLT program look like?

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 
<xsl:output omit-xml-declaration="yes" indent="yes"/>
 
<xsl:template match="/">
    <xsl:for-each select="//*">
       element: <xsl:value-of select="name()"/>&#160;&#160;
       uri:
       <xsl:choose>
       <xsl:when test="namespace-uri()">
         <xsl:value-of select="namespace-uri()"/>
       </xsl:when>
       <xsl:otherwise>NULL</xsl:otherwise>
       </xsl:choose><br/>
    </xsl:for-each>
</xsl:template>
 
</xsl:stylesheet>

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

 

The XSLT program is all contained in a stylesheet element. But this is in the xsl namespace. And that namespace is defined with an xmlns attribute. Here it's given the name - xsl. And the 1999 etc. string is not necessarily any web page, but just an arbitrary string that the 'core' or 'parser' or libraries will recognize in order to treat all the xsl: elements as commands, as need be; in order for the XSLT to work, in other words. The version needs to be there. It's best to stick to 1.0. But the actual number is probably ignored. Next line:

 
     <xsl:output omit-xml-declaration="yes" indent="yes"/>

 

Closing slash. XSLT is, really, just written in XML. So all the rules of XML apply. And it's customary to close an empty XML element with the "/>" shorthand. The actual output command, or output element, is important because it specifies one of the predefined sorts of output types. Three are allowed - text, XML, and HTML. HTML is the default, usually (if the situation is a certain way, XML becomes default). To make sure to get the type you want use the attribute named - method. That way it would read:

 
     <xsl:output method="html" omit-xml-declaration="yes" indent="yes"/>

 

Within that stylesheet element are, templates. In this example, there's just the one. The for-each control structure suggests a for-each loop through a collection or list as in other languages. But if-then-else is a little verbose, here, in that such is had with a xsl:choose block, xsl:when, and the 'else' is otherwise.

You can see the plain text inserted into the template - the words, "element:", "uri:", the word "NULL" and so on, the empty BR element with the slash to make it XML compatible, even the non-breaking space entities, the space 160.

 
xsl:value-of

 

It's at the xsl:value-of command where data is drawn from the source XML document. Those slashes, and the function namespace-uri(), are part of a query language used in XSLT, and in XML DOM and XML generally, called XPath. The for-each runs through every element in the XML document, simply by specifying - "//*". And each element's namespace is reported with the built-in function, namespace-uri().

Templates are triggered or fired, or whatever, in response to a match criteria. They can be call, directly, as well, with a xsl:call-template element/command. In this example, the XPath query is a single slash, which is a shorthand for - start at the root node.

Follow to the next page for another example - GO -