An Introduction to XSLT - Create Elements to Apply the Transformations
(Page 2 of 6 )
Now that we have the declarations out of the way, we can focus on creating the elements that apply the transformations. In order to transform the original XML elements, you need to create templates that match these elements and change them into other elements for display. The original elements that are matched are called patterns, so really you're creating templates that search for and match patterns. The first pattern that we match in the source document is the root elements, in our case the <news> element.
Add the following code to your XSLT file:
<xsl:template match="/">
<Html>
<Head>
<Title>News</Title>
</Head>
<Body>
<CENTER><H1>Todays News</H1></CENTER><BR/>
<xsl:apply-templates select="news/article"/>
</Body>
</Html>
</xsl:template>
What this block of code actually does is look for the root element of the source document, which is specified by the "/" in the first line of the template, and transforms the source element into a standard HTML document. We could just as easily have put the element name, in which case the first line of the template would read as:
<xsl:template match="news">
This does not work with Internet Explorer however, but would with other XML fluent browsers. Nevertheless, our HTML document is given the standard set of tags, with the addition of a nice large, centred heading. The template then specifies that additional templates should be matched to grand-children of the <news> element (or children of the <article> element), and added below the main heading. Now we need to create the templates that match the headline and story elements:
<xsl:template match="article">
Next: Another Template >>
More XML Articles
More By Dan Wellman