Creating the HTML Outline


The main template rule is evaluated when the <spec> element in the source document is encountered . This is always the outermost element of the XML document. It's fairly lengthy, so we'll take it piece by piece.

  <!-- spec: the specification itself -->   <xsl:template match="spec">   <html>   <xsl:if test="header/langusage/language">   <xsl:attribute name="lang">   <xsl:value-of select="header/langusage/language/@id"/>   </xsl:attribute>   </xsl:if>  

The code starts by generating the <html> element, giving it a «lang » attribute if and only if the source document defines its language in the form <langusage><language @id="en" /></langusage> .

  <head>   <meta http-equiv="Content-Type".content="text/html; charset=ISO-8859-1"/>   <title>   <xsl:apply-templates select="'header/title"/>   <xsl:if test="header/version">   <xsl:text> </xsl:text>   <xsl:apply-templates select="header/version"/>   </xsl:if>   <xsl:if test="$additional.title != ' ' ">   <xsl:text> -- </xsl:text>   <xsl:value-of select="$additional.title"/>   </xsl:if>   </title>   <xsl:call-template name="css"/>   </head>  

The next stage is to output the <head> element. Producing a <meta> element giving the media type (or content type) is probably not a very good idea, because the XSLT serializer is required to do this anyway, and this way you will get two such elements, which might not even agree. It's better to leave this to the serializer, which makes sure that the encoding specified in the <meta> element is the same as the actual encoding used. With XSLT 2.0, it is possible to suppress the <meta> element from the serializer by specifying <xsl:output include-content-type="no" /> , but this option is not available in XSLT 1.0.

Most of the above section is concerned with generating the document title (as it appears in the title bar of the browser window). This is a concatenation of the <title> and <version> elements in the source <header> element. Since the HTML title can't contain any markup, I would probably have kept this code simpler, for example the following would work just as well:

  <title>   <xsl:value-of select="concat(header/title, ' ', header/version)"/>   </title>  

which in XSLT 2.0 can be abbreviated further to:

  <title>   <xsl:value-of select="header/title, header/version"/>   </title>  

This simplification ignores the code that allows an additional subtitle to be added after the version number by specifying the stylesheet parameter $additional.title .

Note the call on the named template «css » , which we'll look at in a moment:

  <body>   <xsl:apply-templates/>   <xsl:if test="//footnote[not(ancestor::table)]">   <hr/>   <div class="endnotes">   <xsl:text>&#10;</xsl:text>   <h3>   <xsl:call-template name="anchor">   <xsl:with-param name="conditional" select="0"/>   <xsl:with-param name="default.id" select="'endnotes'"/>   </xsl:call-template>   <xsl:text>End Notes</xsl:text>   </h3>   <dl>   <xsl:apply-templates select="//footnote[not(ancestor::table)]"   mode="notes"/>   </dl>   </div>   </xsl:if>   </body>   </html>   </xsl:template>  

The main content of the document is produced by the <xsl:apply-templates/> call that immediately follows the <body> start tag. This processes the children of the <spec> element, using their respective template rules, and generates the output of each of these children independently. There are generally three children: <header> , <body> , and <back> . The header contains front material such as the status section and abstract, the body contains the numbered sections of the document, and the <back> element contains the appendices.

I haven't come across a W3C document that uses footnotes or endnotes, which is what the rest of this rule is dealing with. It displays all <footnote> elements, other than those found within a <table> , as endnotes at the end of the document. A couple of comments on this code:

  • The XPath expression «//footnote[not (ancestor::table)] » is probably fairly expensive to evaluate, as it involves a scan of the whole document. Since the expression is used twice, it's a natural candidate for a variable.

  • It's possible that the author imagined that <xsl:text>&#10;</xsl:text> would cause the following text to be displayed on a new line. In fact, HTML browsers treat a newline character exactly the same as a space. Alternatively, it's possible that the author simply wanted to create some breaks in the HTML to make it usable in a text editor, without having to switch to <xsl:output indent="yes"/> . Either way, it does no harm.

The named template css , which is called from the above template rule, looks like this:

  <xsl:template name="css">   <style type="text/css">   <xsl:text>   code           { font-family: monospace; }   div.constraint,   div.issue,   div.note,   div.notice     { margin-left: 2em; }   </xsl:text>   <xsl:if test="$tabular.examples = 0">   <xsl:text>   div.exampleInner pre { margin-left: 1em;   margin-top: 0em; margin-bottom: 0em}   div.exampleOuter {border: 4px double gray;   margin: 0em; padding: 0em}   div.exampleInner { background-color: #d5dee3;   border-top-width: 4px;   border-top-style: double;   border-top-color: #d3d3d3;   border-bottom-width: 4px;   border-bottom-style: double;   border-bottom-color: #d3d3d3;   padding: 4px; margin: 0em }   div.exampleWrapper    { margin: 4px }   div.exampleHeader { font-weight: bold;   margin: 4px}   </xsl:text>   </xsl:if>   <xsl:value-of select="$additional.css"/>   </style>   <link rel="stylesheet" type="text/css">   <xsl:attribute name="href">   <xsl:text>http://www.w3.org/StyleSheets/TR/</xsl:text>   <xsl:choose>   <xsl:when test="/spec/@role='editors-copy'">base</xsl:when>   <xsl:otherwise>   <xsl:choose>   <xsl:when test="/spec/@w3c-doctype='wd'">W3C-WD</xsl:when>   <xsl:when test="/spec/@w3c-doctype='rec'">W3C-REC</xsl:when>   <xsl:when test="/spec/@w3c-doctype='pr'">W3C-PR</xsl:when>   <xsl:when test="/spec/@w3c-doctype='cr'">W3C-CR</xsl:when>   <xsl:when test="/spec/@w3c-doctype='note'">W3C-NOTE</xsl;when>   <xsl:otherwise>base</xsl:otherwise>   </xsl:choose>   </xsl:otherwise>   </xsl:choose>   <xsl:text>.css</xsl:text>   </xsl:attribute>   </link>   </xsl:template>  

This code generates two elements within the HTML <head> element: a <style> element and a <link> element. Together, these define the CSS stylesheet that is used by the browser to render the HTML. This combined use of XSLT and CSS is one that I would very much recommend. It means that the XSLT stylesheet can be concerned with getting the structure of the HTML correct, and can leave the fine detail of fonts and margins to the CSS stylesheet.

Why use both a <link> and a <style> ? The <link> contains a reference to a CSS stylesheet stored on the W3C Web server, while the <style> contains local modifications and additions. I suspect that the reason for the split is to do with change control. Changing a CSS stylesheet on the Web server, when there are many documents that refer to it, is a risky thing to do, especially when many of these documents are supposed to be stable specifications. Making minor improvements to the formatting is safer if the modifications affect only new documents, not old. This could be achieved, of course, by introducing a new version of the CSS stylesheet on the server. Perhaps, W3C (like many organizations) has change control processes that made it easier for the XSLT stylesheet authors to introduce the changes locally.

Note how both the <link> and the <style> can be customized. The <link> generates a reference to a CSS stylesheet conditionally, depending on the type of document. The various CSS stylesheets are identical except for the choice of a background image: the stylesheet for Working Drafts, for example, specifies:

  body {   background-image; url(http://www.w3.org/StyleSheets/TR/logo-WD);   }  

while that for Recommendations has:

  body {   background-image: url(http://www.w3.org/StyleSheets/TR/logo~REC);   }  

This image contains the vertical text shown on the top left-hand corner of the displayed page.

The CSS definitions generated within the <style> element include any definitions present in the content of the variable $additional.css . By default, this holds an empty string. However, the variable can be overridden in an overlay stylesheet to define additional CSS display classes, and this is commonly done: for example, the XSLT specification uses extra classes for displaying proforma XSLT element definitions.




XSLT 2.0 Programmer's Reference
NetBeansв„ў IDE Field Guide: Developing Desktop, Web, Enterprise, and Mobile Applications (2nd Edition)
ISBN: 764569090
EAN: 2147483647
Year: 2003
Pages: 324

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