XSLT Overview
XSLT is another markup language based on XML. While I've witnessed quite a few doofus
XSLT and XPath are both very broad, capable languages. I can't cover all of their depth in just one chapter. However, I don't have to. XSLT and XPath can be used to perform XML transformations for a broad range of applications including not just application integration and electronic commerce but also publishing, word processing, and Web page design. XSLT and XPath were designed primarily to be used with XSL for formatting. People write whole books on each of these two technologies. But the fact of the matter is that most commonly performed business document transformations can be accomplished using a small subset of XSLT and XPath features. So, the basics of what you need to know can be covered in one chapter.
If you want to know more, which you probably will if you work with XSLT very much, there are several places you can go for help. Unlike the Schema Recommendation, I find the XSLT and XPath Recommendations to be well written and easy to understand (for the most part, once you get past the terminology). I highly recommend them. One of the reasons for their readability is that the authors put many of the conformance requirements in separate sections rather than interspersing them with other
A Simple Example: Hello WorldI think the best way to teach a programming language is to start with a very simple, contrived example. So, here is an XSLT version of Hello World. Stylesheet (HelloWorld.xsl)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<HelloWorld>Howdy!</HelloWorld>
</xsl:template>
</xsl:stylesheet>
Here is our source document. Source (Goodbye.xml)<?xml version="1.0" encoding="UTF-8"?> <Goodbye> you all </Goodbye> We can run the source document through an XSLT processor using our HelloWorld.xsl stylesheet and produce the following result. Result (HelloWorld.xml)<?xml version="1.0" encoding="UTF-8"?> <HelloWorld> Howdy! </HelloWorld> In fact, we can process any XML document using this stylesheet and produce exactly this same result. Let's look at the major parts of this stylesheet and understand why.
The first thing to note is that the stylesheet is itself a well-
In our example there is only one Element in the body of the stylesheet document, the
xsl:template
Element. To aid in clarity, I am going to use in this chapter's text the convention of using the xsl: prefix on XSLT Element
The xsl:template Element defines what is called a
template rule
. It is very aptly named because the contents of the template Element function as a template (or pattern or cookie cutter if you prefer) for a portion of the result tree. The analogy of using a template in manufacturing to stamp out several copies of a part is not quite so evident in this example because we just produce one fragment of the result tree. In our example the content of the template Element is the entire body of the result tree document. This cookie
The rule aspect of the template Element
So, now that we know what the pieces are we can understand how this stylesheet works. The XSLT processor starts at the root of the source tree and sees if there is a template rule that matches it. Our xsl:template has a match Attribute value indicating the source tree root, so the processor examines the contents. There are no Elements from the XSLT namespace, so it
Those are the bare-bones basics. Let's look at another example before we start digging into more details and concepts. Another Simple Example: Changing Tag Names
Hello World has value only as an instructional tool. However, this
Stylesheet (ProspectToCustomer.xsl)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="Prospect">
<Customer>
<FirstName>
<xsl:value-of select="FName"/>
</FirstName>
<MiddleName>
<xsl:value-of select="MI"/>
</MiddleName>
<LastName>
<xsl:value-of select="LName"/>
</LastName>
<Organization>
<xsl:value-of select="Company"/>
</Organization>
<StreetLine1>
<xsl:value-of select="Street1"/>
</StreetLine1>
<StreetLine2>
<xsl:value-of select="Street2"/>
</StreetLine2>
<CityOrMunicipalUnit>
<xsl:value-of select="City"/>
</CityOrMunicipalUnit>
<StateOrProvince>
<xsl:value-of select="State"/>
</StateOrProvince>
<PostalCode>
<xsl:value-of select="Zip"/>
</PostalCode>
</Customer>
</xsl:template>
</xsl:stylesheet>
Here are the source document and the result produced by applying the stylesheet. Source (Prospect.xml)<?xml version="1.0" encoding="UTF-8"?> <Prospect> <FName>Wilma</FName> <MI>J.</MI> <LName>Peterson</LName> <Company>Consolidated Consolidators</Company> <Street1>Suite 35</Street1> <Street2>14 Friendly Lane</Street2> <City>Amarillo</City> <State>TX</State> <Zip>79101</Zip> </Prospect> Result (Customer.xml)<?xml version="1.0" encoding="UTF-8"?> <Customer> <FirstName>Wilma</FirstName> <MiddleName>J.</MiddleName> <LastName>Peterson</LastName> <Organization>Consolidated Consolidators</Organization> <StreetLine1>Suite 35</StreetLine1> <StreetLine2>14 Friendly Lane</StreetLine2> <CityOrMunicipalUnit>Amarillo</CityOrMunicipalUnit> <StateOrProvince>TX</StateOrProvince> <PostalCode>79101</PostalCode> </Customer>
You'll notice one new XSLT Element and another new concept in this stylesheet. The new Element is the
xsl:value-of
Element. There are various ways to use it, but here we insert a Text Node that contains the string value of the Element named in the
select
Attribute. You'll also notice that we used the value of "Prospect" in the match Attribute of the xsl:template rather than the "/" value for the document root. The reason for this is that Prospect as the
document Element
is not the same thing as the document root. We often refer to the "document Element" as the "root Element," but
These two stylesheets offer examples of one particular approach to using XSLT. There are others. In the next section I
|