Tags

,

XSLT In Action

Post on October 22nd, 2007

Raw XML is rarely suitable for direct presentation, and frequently must be organized, or transformed, to suit the needs of a specific output device. The most widely used option for this is XSLT. Older techniques such DTD and Schemas can be used as well, but as discussed in my first post about XML, XSLT was specifically created for this and should be used when possible.

XSLT provides an effective procedure – a tree transformation process – for manipulating XML at the level of elements and attributes. A special-purpose language, XSLT has features that simplify that task of defining transformations of XML.

Here is the xml file (.xml) that is used throughout the explanations.

In many ways, XSLT can function like a database language such as SQL (Structured Query Language), because it enables you to extract the data you want from XML documents, much like applying an SQL statement to a database. Some people even think of XSLT as the SQL of the Web, and if you’re familiar with SQL, that gives you some idea of the boundless horizons available to XSLT.

How XSLT Processors Work

The XSLT processor (the browser in this case), analyzes the XML document and converts it into a tree node. From this workshop you should all know what a node is right? A node is nothing more than one individual piece of the XML document (like an element, an attribute, or some text content). A node tree is a hierarchal representation of the entire XML document. Remember, an XHTML document is just another form of XML.

Once the processor has identified the nodes in the source XML, it then looks to a XSLT style sheet for instructions on what to do with those nodes. Those instructions are contained in templates. Each template has two parts: first, a sort of label that identifies the nodes in the XML document that the template can be applied to, and second, instructions about the actual transformation that should take place.

XSLT Components

There are basically two kinds of components in an XSLT style sheet: instructions and literals. The XSLT instructions describe how the source XML document will be transformed. The literals — typically HTML code and text — are output just as they appear in the style sheet. In your root template, you create the structure for the final transformed document. For HTML output, you’ll want to add HTML header information (head, body, etc.) at the very least. And if you like you can add extensive HTML formatting as well.

Outputting a Node’s Content

Once you created the HTML code that will format a given node’s content, you’ll want to actually output that content (called string value) . The simplest way to add the content of a node (like an element) to the output document is to write it out as it is.

<xsl:value of select="expression">

where expression identifies the node set from the XML source document whose content should be output at this point.

The string value of a node is generally the text that that node contains. If the node has child elements, however, the string value includes the text contained in those child elements as well. If the node set is empty, there is nothing to output.

Batch-Processing Nodes

The xsl:for-each element can process all the nodes in a given set in the same way, one after another.

<xsl:for-each select="expression">
 
</xsl:for-each>

where expression identifies the node set to process through.

The xsl:for-each element is typically used to create HTML tables, with the opening and closing table tags preceding and following the element, respectively, and with the tr and td tags as part of the processing. In general, place the xsl:for-each right before the rules that should be repeated for each node found.

Here is the XSLT stylesheet (.xsl) and the transformed results.

Sorting Nodes Before Processing

By default, the nodes are processed in the order in which they appear in the document. If you’d like to process the in some other order, you can add an xsl:sort element to the xsl:apply-templates or xsl:for-each elements.

<xsl:for-each select="catalog/video">
<xsl:sort select="title" />
 
</xsl:for-each>

Here is the updated XSLT stylesheet (.xsl) and the transformed results.

Processing Nodes Conditionally

It’s not uncommon to want to process a node or a set of nodes only if a certain condition is met. The condition is written as an expression. For example, you might want to perform a certain action if a particular node set is not empty, or if the string value of a node is equal to a particular word.

<xsl:if test="expression">
 
</xsl:if>

Here is the updated XSLT stylesheet (.xsl) and the transformed results.

The xsl:if instruction only allows for one condition and one resulting action. You can use xsl:choose when you want to test for several different situations, and react accordingly to each one.

<xsl:choose>
     <xsl:when test="expression">
 
     </xsl:when>
     <xsl:otherwise>
 
     </xsl:otherwise>
</xsl:choose>

Here is the updated XSLT stylesheet (.xsl) and the transformed results.

From Web stuff

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.