2.5 Outputting Comments
Comments allow you to hide advisory text in an XML document. You can also use comments to label documents, or portions of them, which can be useful for debugging. When an XML processor sees a comment, it may ignore or discard it, or it can make the text content of comments available for other kinds of processing. The text in comments is not the same as the text found between element tags, that is, it is not character data. As such, comments can contain
<!-- This element holds the current date & time -->
The only legal XML characters that a comment must not contain are the sequence of two hyphen characters ( -- ), as this pair of characters signals the end of a comment. Other than that, you are free to use any legal XML character in a comment. (Again, to check on what characters are legal in XML, and where they are legal, see Sections 2.2 through 2.4 of the XML specification.) To insert a comment into a result tree, you can use the XSLT instruction element comment , as demonstrated in the comment.xsl stylesheet:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:comment> comment & msg element </xsl:comment>
<msg><xsl:apply-templates/></msg>
</xsl:template>
</xsl:stylesheet>
The output method is XML. If it were text, the comment would not show up in the output. Because comments in XML can contain markup characters, you can include an ampersand in a comment, among otherwise naughty characters, though it must first be represented by an entity reference ( & ) in the stylesheet. Process this stylesheet against comment.xml with Xalan: xalan comment.xml comment.xsl You will get the following results: <?xml version="1.0" encoding="UTF-8"?> <!-- comment & msg element --> <msg>You can insert comments in your output.</msg> |
2.6 Outputting Processing Instructions
It must come as no surprise that you can add processing instructions, or PIs, to the result tree with the
processing-instruction
element. This element is
<xsl:processing-instruction name="xml-stylesheet">href="new.css" type="text/css"</xsl:processing-instruction>
A
processing-instruction
element requires one attribute,
name
, which identifies the target name for the PI. The value of this attribute must be an NCName, and, as such, must not be a QName and cannot contain a
The content of the processing-instruction element contains the pair of pseudo-attributes href and type that are necessary to apply the CSS stylesheet processing.css to the resulting XML document:
paragraph {font-size: 24pt; font-family: serif}
code {font-family: monospace}
These rules will apply to the paragraph and code elements in the result tree. Provided that you view the result tree in a browser, any paragraph elements will be rendered with a best-fit serif font, in 24-point type, while any code elements will be rendered in a monospace font. (Courier is an example of a monospace font.) You'll get a chance to see the effects of these style rules later on in this section.
In the example that
<?xml version="1.0"?> <message>You can add processing instructions to a document with the <courier> processing-instruction</courier> element.</message> 2.6.1 Mixed Content
The
message
element in
processing.xml
contains
mixed content
. Mixed content
processing.xsl handles the mixed content in processing.xml :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:processing-instruction name="xml-stylesheet">href="processing.css" type="text/css"</xsl:processing-instruction>
<xsl:element name="doc">
<xsl:element name="paragraph"><xsl:apply-templates/></xsl:element>
</xsl:element>
</xsl:template>
<xsl:template match="courier">
<xsl:element name="code"><xsl:apply-templates/></xsl:element>
</xsl:template>
</xsl:stylesheet>
2.6.2 Using Multiple Template Rules
For the first time in this book, you are seeing a stylesheet (
processing.xsl
) that has more than one template rule. (Remember, a template rule consists of a pattern to match and a constructor telling the processor what to do when the pattern is matched.) The way you design your templates
In the stylesheet processing.xsl , the first template matches the root node in the document using / . When the processor encounters apply-templates in this template, it matches any children of the root node in the source. When applied to processing.xml , the built-in templates for elements and text match the message element and its child text content.
The
The original template, seeing nothing else to do, picks up where the other template left off and takes care of its other work. With the processor holding onto the work that the other template did in a temporary tree, the built-in template for text nodes yanks the character data out of message and surrounds it with paragraph tags.
Somewhere along the way, it surrounds all the elements with the new root element
doc
. It creates a new PI, too, based on the instructions given by the
processing-instruction
element. Once that work is done, and the XSLT processor sees that there is nothing left to do, it
2.6.2.1 What can go in a template rule?It's obvious that the template element can hold a template rule, but other XSLT elements can hold templates as well. Generally speaking, a template consists of one or more XSLT elements that can create a result tree. These templates are not template rules per se because they don't have to match a pattern—they just contain sequence constructors. Literal result elements and literal text, as well as the apply-templates , attribute , element , comment , processing-instruction , and text elements can all be contained in templates. The 15 elements that can contain templates (but don't match patterns) are:
A lot of elements in this list are probably new to you. It would
2.6.3 Creating the PI and
|