2.7 One Final Example

Finally, to wrap things up, here is an example stylesheet that shows you, once again, how to perform most of the techniques discussed in this chapter. The example starts out with the rather short document containing mixed content, final.xml:

<?xml version="1.0"?>    <message>You can add processing instructions to a document with the <courier>processing-instruction</courier> element.</message>

There isn't much to it, but you can augment final.xml with the well-rounded XSLT stylesheet, final.xsl:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/>    <xsl:attribute-set name="atts">  <xsl:attribute name="noteworthy">true</xsl:attribute>  <xsl:attribute name="priority">medium</xsl:attribute> </xsl:attribute-set>    <xsl:template match="/">  <xsl:processing-instruction name="xml-stylesheet">href="final.css" type="text/css" </xsl:processing-instruction>  <xsl:comment> final.xml as processed with final.xsl </xsl:comment>  <doc>   <heading>Final Summary</heading>   <paragraph>Following is a summary of how you can build documents with XSLT: </paragraph>   <paragraph>You can add text either literally or with the <code>text</code> element.</paragraph>   <paragraph>You can use literal result elements in stylesheets.</paragraph>   <xsl:element name="paragraph">You can use <xsl:element name="code">element </xsl:element> elements in stylesheets.</xsl:element>   <xsl:comment> you can add a line break &amp; some spaces with the text element  </xsl:comment>   <xsl:text>      </xsl:text>   <xsl:element name="paragraph"><xsl:attribute name="noteworthy">true</xsl:attribute> You can add attributes to elements with the <xsl:element name="code">attribute </xsl:element> element.</xsl:element>   <xsl:element name="paragraph" use-attribute-sets="atts">You can even add sets of  attributes to elements with the <xsl:element name="code">attribute-set</ xsl:element> top-level element.</xsl:element>   <paragraph>You can add comments with the <code>comment</code> element.</paragraph>   <xsl:element name="paragraph"><xsl:text>And last but not least: </xsl:text> <xsl:apply-templates select="message"/></xsl:element>  </doc> </xsl:template>    <xsl:template match="courier">  <xsl:element name="code"><xsl:apply-templates/></xsl:element> </xsl:template>    </xsl:stylesheet>

Processing final.xml with final.xsl, you can serialize the result tree and place it in a file:

xalan -o finally.xml final.xml final.xsl

The XML document finally.xml turns out like this:

<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet href="final.css" type="text/css"?>    <!-- final.xml as processed with final.xsl --> <doc> <heading>Final Summary</heading> <paragraph>Following is a summary of how you can build documents with XSLT: </paragraph> <paragraph>You can add text either literally or with the <code>text</code> element. </paragraph> <paragraph>You can use literal result elements in stylesheets.</paragraph> <paragraph>You can use <code>element</code> elements in stylesheets.</paragraph> <!-- you can add a line break & some spaces with the text element -->       <paragraph noteworthy="true">You can add attributes to elements with the  <code>attribute</code> element.</paragraph> <paragraph noteworthy="true" priority="medium">You can even add sets of attributes  to elements with the <code>attribute-set</code> top-level element.</ paragraph> <paragraph>You can add comments with the <code>comment</code> element. </paragraph> <paragraph>And last but not least: You can add processing instructions to a document  with the <code>processing-instruction</code> element.</paragraph> </doc>

When finally.xml is displayed in Mozilla, it depends on the CSS stylesheet final.css to figure out how to render heading, paragraph, and code elements:

heading {display: block; font-size: 16pt; font-family: sans-serif; margin: 8pt 15pt} paragraph {display: block; font-size: 12pt; font-family: serif; margin: 5pt 15pt} code {display: inline; font-size: 11pt; font-family: monospace}

The display property with value of block gives the heading and paragraph elements a block- or box-like appearance on the browser canvas or rendering space. The display value of inline for code means that elements should be displayed inline with other text. The sans-serif font family for heading indicates that you want the browser to select a sans-serif font on a best-match basis, just as with monospace for code elements. The margin property sets the top and right margins for the element to either 8 and 15 points, or 5 and 15 points, respectively.

With this CSS applied in Mozilla, finally.xml is displayed in Figure 2-4.

Figure 2-4. The document finally.xml displayed in Mozilla with final.css
figs/lxsl_0204.gif


Learning XSLT
Learning XSLT
ISBN: 0596003277
EAN: 2147483647
Year: 2003
Pages: 164

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net