|
|
|
|
|
|
XML Basics, Part IV: Formatting Output
Welcome to the fourth and final part in this series of XML Basics. We will complete our exploration by using XSLT to format the output of your XML files.
To recap our journey thus far, Part I explored the definition and benefits of XML, Part II examined the detailed concepts of writing and working with XML, and Part III tied it all together with an easy to follow example.
What is XSL?
XSL is an XML-based language used for stylesheets that can be used to transform XML documents into other document types and formats. It consists of the transformation language, abbreviated XSLT, and the formatting language called XSL Formatting Objects. This combination enables two of the key benefits of XML: transforming one document into multiple channels of output and system interoperability.
In order for XSLT to do its magic, you provide it with an input document (source tree) so it can do its thing (transformation) and make it look a particular way in a specified output document (result tree).
XSLT is built on a structure known as an XSL template. This structure is represented by the <xsl:template> element. There are two important pieces to every template element: the match attribute and the actual contents of the template.
The match attribute specifies the pattern in the source tree that will be transformed by a particular template element. In the contents of the template element, you specify what should go in the results tree for each occurrence of the specified pattern.
There are a number of XSLT elements that you could insert into a template to perform a variety of actions. One that is most common is the <xsl:value-of> element. With this element, you can take information from the source tree and add it to the result tree.
For example, let's use an abbreviated piece of our example from Part III:
<movie>
If we wanted to use XSLT to select each occurrence of the <title> element and output its content in the results tree, the XSLT would look as follows:
<xsl:template match="/movie/title"><xsl:value-of select="."/></xsl:template> The output in this example would be: "The Matrix". Common XSLT Elements Templates are the heart and soul of XSLT. A stylesheet is really just a collection of one or more templates that get applied to the input document (the source tree XML document) to create the output document (the result tree that can be a variety of document types). If an XSL document has more than one template, the order of operations is such that the processor starts by matching the document root to the stylesheet template that best matches it. If there is more than one match, the following order applies: If matching the root element, the first match applies; otherwise, the second (last) match is used. An important point to remember about the match attribute is that the item matched becomes the context node for that template. What that means is that the matched element becomes the root for that particular template section. Essentially, the match simply creates a subsection of the overall document hierarchy and works from that perspective for the specific template section. As with many things in the XML world, there are quite a number of XSLT elements that can be used within a template. I will describe the most common ones here and use some of them in the example to follow.
<xsl:stylesheet>
<xsl:template> The match attribute is used to indicate the pattern from the source tree in the input document that we wish to select. The name, priority, and mode attributes are optional. The name attribute is used to explicitly name the template, the priority attribute is used to force calling one template over another when more than one can satisfy the match, and mode is used when more than one template satisfies the match but performs different transformations on the content.
<xsl:apply-templates>
<xsl:value-of>
<xsl:output>
<xsl:element>
<xsl:text> Two elements are available in XSLT for conditional processing. These elements allow you to make choices in your stylesheets: <xsl:if> and <xsl:choose>.
<xsl:if>
<xsl:choose>
<xsl:choose>
<xsl:for-each>
<xsl:copy-of> Page 2: XSLT Applied to the Movie Catalog Example
Go to page: 1 2
|
Intranet Journal's Tutorials |
|
Managing Editor |