Formatting the Text


The bulk of the stylesheet is taken up with template rules to process simple textual markup within the body of the document. Most of these are very straightforward, and to avoid tedious repetition I will show only a small sample of them.

Probably the most common element is the <p> element, which marks a paragraph, as in HTML:

  <xsl:template match="p">   <p>   <xsl:if test="@id">   <xsl:attribute name="id">   <xsl:value-of select="@id"/>   </xsl:attribute>   </xsl:if>   <xsl:if test="@role">   <xsl:attribute name="class">   <xsl:value-of select="@role"/>   </xsl:attribute>   </xsl:if>   <xsl:apply-templates/>   </p>   </xsl:template>  

You've probably got the message by now that I don't much like unnecessary verbosity . The first thing I notice about this template rule is that the five lines:

  <xsl:if test="@id">   <xsl:attribute name="id">   <xsl:value-of select="@id"/>   </xsl:attribute>   </xsl:if>  

are equivalent to the single line:

  <xsl:copy-of select="@id">  

The next is that <xsl:if> renames the «role » attribute as «class » , so it's less easy to simplify, though with XSLT 2.0 you can reduce it to:

  <xsl:if test="@role">   <xsl:attribute name="class" select="@role"/>   </xsl:if>  

But the essential structure of this template rule is typical of many others: it translates one element in the source document into one element in the result document, making minor adjustments to the attributes, and then calls <xsl:apply-templates/> to process the content of the element. This is the typical style of a rule-based stylesheet. Here are some other simple examples of such rules:

  <!-- sub: subscript -->   <xsl:template match="sub">   <sub>   <xsl:apply-templates/>   </sub>   </xsl:template>   <!-- term: the actual mention of a term within a termdef -->   <xsl:template match="term">   <b><xsl:apply-templates/></b>   </xsl:template>   <!-- emph: in-line emphasis -->   <xsl:template match="emph">   <em><xsl:apply-templates/></em>   </xsl:template>   <!-- rfc2119: identifies RFC 2119 keywords -->   <xsl:template match="rfc2119">   <strong><xsl:apply-templates/></strong>   </xsl:template>   <!-- item: generic list item -->   <xsl:template match="item">   <li>   <xsl:apply-templates/>   </li>   </xsl:template>   <!-- quote: a quoted string or phrase -->   <!-- it would be nice to use HTML <q> elements, but browser support is   abysmal-->   <xsl:template match="quote">   <xsl:text>"</xsl:text>   <xsl:apply-templates/>   <xsl:text>"</xsl:text>   </xsl:template>   <!-- affiliation: follows a name in author and member -->   <xsl:template match="affiliation">   <xsl:text>, </xsl:text>   <xsl:apply-templates/>   </xsl:template>  

There are some elements in the XML that are not rendered in the HTML at all, for example:

  <xsl:template match="revisiondesc">   <!-- suppressed by default -->   </xsl:template>  

I generally write empty template rules using the shorter style:

  <xsl:template match="revisiondesc"/>  

but one can't criticize this writer for adding a comment to make the intention clear.

The XML specification represents tables in exactly the same way as HTML, except that some additional attributes are permitted. So the template rule's job is essentially to copy the element while adjusting those attributes:

  <!-- table: the HTML table model adopted wholesale; note however that we -->   <!-- do this such that the XHTML stylesheet will do the right thing. -->   <xsl:template match="captioncolcolgrouptdtfootththeadtrtbody">   <xsl:element name="{local-name(.)}">   <xsl:for-each select="@*">   <!-- Wait: some of these aren't HTML attributes after all... -->   <xsl:choose>   <xsl:when test="local-name(.) = 'role'">   <xsl:attribute name="class">   <xsl:value-of select="."/>   </xsl:attribute>   </xsl:when>   <xsl:when test="local-name(.) = 'diff'">   <!-- nop -->   </xsl:when>   <xsl:otherwise>   <xsl:copy>   <xsl:apply-templates/>   </xsl:copy>   </xsl:otherwise>   </xsl:choose>   </xsl:for-each>   <xsl:apply-templates/>   </xsl:element>   </xsl:template>  

I think I would have been inclined to handle these attributes using template rules, especially as we've already seen the same code to rename a role attribute as a class attribute, elsewhere in the stylesheet.

Instead of <xsl:for-each select="@*"> and the big <xsl:choose> instruction, I would write <xsl:apply-templates select="@*" mode=" table-att" /> , with the three template rules:

  <xsl:template match="@role" mode="table-att">   <xsl:attribute name="class" select="."/>   </xsl:template>   <xsl:template match="@diff" mode="table-att"/>   <xsl:template match="@*" mode="table-att">   <xsl:copy/>   </xsl:template>  



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