Line-ending issues

Line endings in ASCII text files have no relevance in XML, but in many formats they are very important. For example, in the TROFF (Text RunOFF) format, each formatting command must appear at the start of a text line. Line-end characters can be inserted into the output using XML character entity references. On Macintosh systems, the character Carriage Return (ASCII value 13) is used. On Unix sytems, it is the character Line Feed (ASCII value 10). On MS-DOS-based systems, both characters are used, in the sequence Carriage Return followed by Line Feed. But, depending on the parser underlying the stylesheet processor, either character alone may produce the platform-specific outcome required (so this needs testing). The following example outputs TROFF commands, with each command beginning a new line on a UNIX system (and '.PP' specifies a new paragraph):

<xsl:template match="warning">
.PP&#13;&#10;
WARNING:&#13;&#10;
.PP&#13;&#10;
<xsl:apply-templates/>
</xsl:template>


   .PP
   WARNING:
   .PP
   ......

Whitespace in the stylesheet can cause problems here. As previously described, nodes that consist only of whitespace characters, including the line-feed and carriage-return characters, are removed from the stylesheet when it is processed. The following example therefore does not work as intended:

   <name>John Smith</name>
   <name>Peter Jones</name>

<xsl:template match="name">
<xsl:apply-templates/>&#13;&#10;
</xsl:template>


   John SmithPeter Jones

The obvious solution is to use the Text element to enclose the codes. This preserves them as required:

<xsl:template match="name">
<xsl:apply-templates/><xsl:text>&#13;&#10;</xsl:text>
</xsl:template>

Whitespace in the source document can also cause problems. A line-feed or carriage-return character in a source element is normally just copied through to the output:

   <code>10 PRINT 'HELLO'</code>
   <code>20 GOTO
					10</code>


<xsl:template match="code">
CODE LINE: <xsl:apply-templates/>
</xsl:template>


   CODE LINE: 10 PRINT 'HELLO'
   CODE LINE: 20 GOTO
					10
				

This problem can be solved by replacing the Apply Templates element with the Value Of element, ensuring that the string it selects (the content of the current element) is normalized before it is inserted into the output:

<xsl:template match="code">
CODE LINE: <xsl:value-of select="normalize-space(.)" />
</xsl:template>


   CODE LINE: 10 PRINT 'HELLO'
   CODE LINE: 20 GOTO
					10
				

Finally, whitespace characters inserted into the source XML document to make the document easier to read could have unfortunate consequences if included in the output. Using the Strip Space element (within the Stylesheet element) ensures that this whitespace is removed prior to template processing:

<strip-space elements="*" />

However, this instruction must be used with caution, as described in Chapter 4.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.193.232