Adding Parental Patterns to the XSL Rule
As noted in the last example, the XSL rule that is defined ends up
being applied to all of the <title> elements. However, there
are times when you might want to apply a style to an element depending
on its relationship to other elements within the document.
To identify elements that act as parents or children of the target
element, the <element> tag is used. The "element" element is
used to describe a relationship between the element that is being
matched for a style, and another associated element. For example,
let's say we want to add an XSL style to the title of the sidebar that
is different from the style applied to the other two titles in the
document. In addition, we also want to apply a style to the sidebar
"body" element. We could then add two more rules to the XSL file that
defines the styles for these two elements:
<rule>
<element type="sidebar">
<target-element type="title"/>
</element>
<H3>
<children/>
</H3>
</rule>
<rule>
<element type="sidebar">
<target-element type="body"/>
</element>
<P color="blue" font-style="italic">
<children/>
</P>
</rule>
According to the first XSL style rule, the pattern to match is a
<title> element contained within a <sidebar> element. The
style will be applied to elements matching both the target element's
name, and the relationship between the parent element and the target
element. The second style is applied to the <body> element that
is also contained within the parental <sidebar> element.
Running this XSL file with the example XML file using "msxsl.exe", the
following HTML is generated:
<DIV><H1 style="color: red; font-family: Arial">
What is XML
</H1><H3>
What is a markup language
</H3><P style="color: blue; font-style: italic">
A markup language is any language that contains markup
characters that determine how the contents are processed.
</P><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 check out this
version
of the example. Note that now, the title for the sidebar is smaller
than that of the other titles, is not red, and does not have the same
font family. Also note that the sidebar body element is blue and
italic.
You might have noticed that the generated HTML does not contain the
traditional <HTML> and <BODY> tags in the generated output.
These elements are actually implied by the browser, which is why the
examples show correctly, but there is a rule that can be used with
XSL to generate these elements, the root rule.
Defining an XSL Rule
Add a little style to your XML document with XSL - a Working Example
The Root Rule
|