A Simple XSLT Stylesheet - Page 4
October 26, 2001
An XSLT transformation is specified by a well-formed XML document
called a stylesheet. The key elements in a stylesheet are the
specialized elements from the XSLT namespace. (A namespace is a
unique name for a given set of element and attribute names. Their
use is usually declared in an XML document’s document element
with a short nickname that the document uses as a prefix for
names from that namespace.) When an XSLT processor reads one of
these stylesheets, it recognizes these specialized elements and
executes their instructions.
XSLT stylesheets usually assign “xsl” as the prefix for the XSLT
namespace (ironically, XSL stylesheets usually use the prefix
“fo” to identify their “formatting objects”), and XSLT
discussions usually refer to these element types using the “xsl”
prefix. This way, when something refers to the
xsl:text or xsl:message elements you
can assume that they mean the text and message element types from
the XSLT namespace and not from somewhere else.
An XSLT stylesheet doesn’t have to use “xsl” as the namespace
prefix. For example, if the stylesheet below began with the
namespace declaration
xmlns:harpo="http://www.w3.org/1999/XSL/Transform"
the stylesheet’s XSLT elements would need names like harpo:text
and harpo: message for an XSLT processor to recognize them and
perform their instructions.
The following stylesheet demonstrates many common features of an
XSLT stylesheet. It’s a well-formed XML document with a root
element of xsl:stylesheet. (You can also use name xsl:transform
for your stylesheet’s root element, which means the same thing to
the XSLT processor.):
<!-- xq15.xsl: converts xq16.xml into xq17.xml -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="year">
<vintage>
<xsl:apply-templates/>
</vintage>
</xsl:template>
<xsl:template match="price">
</xsl:template>
<!-- Copy all the other elements and attributes, and text nodes -->
<xsl:template match="*|@*|text()">
<xsl:copy>
<xsl:apply-emplates select="*|@*|text()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
XSLT and Alternatives (Con't) - Page 3
XSLT Quickly
Template Rules - Page 5
|