Defining an XSL Rule
First, XSL styles are defined as elements with an element name of
"rule" and containing patterns used to match a style with a specific
XML element. For example, the following defines an XSL rule for an
element with the label of "title", and applies a presentation
equivalent to an H1 header, with a red font and using the font-family
of "Arial":
<xsl>
<rule>
<target-element type="title"/>
<H1 color="red" font-family="Arial">
<children/>
</H1>
</rule>
</xsl>
This style rule has a target-element of "title", which means the rule
will be applied to any XML element with a name of "title". The implied
H1 element style is then applied to the "children" of the element. The
children of the element is any content contained within the beginning
and ending target element tags that is, itself, not another element.
The H1 element style is amended to also include a font color of "red"
and a font family of "Arial".
To test the XSL file, it and the test XML file are run through the
Microsoft command line XSL processor, msxsl.exe, downloadable
from a URL in the resource section. This processor does not implement
all of the XSL specification, but does process enough to demonstrate
how XSL and XML are combined to generate a document viewable by the
4.x Navigator and IE browsers.
The msxsl.exe utility is run against the XML and XSL test code using
the following line:
msxsl -i test.xml -s test.xsl -o test1.htm
This command line runs the utility, passing in the XML input file,
with the "-i" flag, the XSL style file, with the "-s" flag, and the
output file, with the "-o" flag. The result of running the processing
is the generated HTML:
<DIV><H1 style="color: red; font-family: Arial">
What is XML
</H1><H1 style="color: red; font-family: Arial">
What is a markup language
</H1> A markup language is any language that contains
markup characters that determine how the contents are processed.
<H1 style="color: red; font-family: Arial">
What is XML?
</H1> XML is Extended Markup Language, a generic language used to
generate documents without following predefined element types. </DIV>
You can actually check out the
generated output. Note that the
contents are placed within a DIV block, and the H1 header with the
associated CSS style attribute is applied to each "title" element.
The reason all three title elements are modified is because there is
no constraint in the XSL rule that limits the style to the top level
title or the title within the sidebar element. How to fine tune which
elements receive which styles is covered in the next section.
What is XSL?
Add a little style to your XML document with XSL - a Working Example
Adding Parental Patterns to the XSL Rule
|