A Rule-Based Stylesheet
All XSLT processing is rule-based, it’s just that
some stylesheets take advantage of this fact more than others. Our
first stylesheet (Example B-2) only used one template rule (where
match="/
“). Now we’ll look at a
stylesheet that uses multiple template rules.
Example B-4 shows the source document for this
example transformation. It is a simple article that contains a
heading and multiple paragraphs. Inside the paragraphs, there is some
“mixed content,” i.e., elements
that contain both text and elements (e.g., the
emphasis
element).
Example B-4. A simple XML document containing marked-up text
<article> <heading>This is a short article</heading> <para>This is the <emphasis>first</emphasis> paragraph.</para> <para>This is the <strong>second</strong> paragraph.</para> </article>
Example B-5 shows a simple XSLT stylesheet that is designed to process documents that look like the XML document in Example B-4.
Example B-5. An XSLT stylesheet with multiple template rules
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/> <xsl:template match="/"> <html> <head> <title> <xsl:value-of select="/article/heading"/> </title> </head> <body> <h1> <xsl:value-of select="/article/heading"/> </h1> <xsl:apply-templates select="/article/para"/> </body> </html> </xsl:template> <xsl:template match="para"> <p> <xsl:apply-templates/> </p> </xsl:template> <xsl:template match="emphasis"> <i> <xsl:apply-templates/> </i> </xsl:template> ...
Get Office 2003 XML now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.