Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Troubleshooting

Displaying Text with MSXML

No text displays in the browser when I try to run the examples in this chapter.

Check to see whether you are using Microsoft's MSXML parser. Because Microsoft implemented its first parser before the XSLT standard was completed, its implementation of <apply-templates> did not automatically retrieve the content from a selected element. Therefore, you will not see text displayed if you are using MSXML 2.6 or older.

To solve this problem, you must add the following template rule to your style sheet:

<xsl:template match="text()">
  <xsl:value-of />
</xsl:template>

This template rule may be placed anywhere in the style sheet after the root template rule. One quirk with this solution is that the current specification requires <value-of> to include a select attribute. Therefore, the <xsl:value-of /> syntax in this solution is not considered legal syntax by modern XSLT processors and will generate an error.

Locating Problems with Missing Text

My code isn't rendering all the text I expect to see.

While Chapter 12 covers the debugging of style sheets, a common problem developers run into is that the style sheet does not always display the expected text from an element. If your code produces an incomplete rendering or the browser window is blank, check your style sheet to ensure you have a template rule for that element. For example, if Listing 2.7 did not include a "bold" template rule, the text displayed would not include any content contained in bold elements from the source document. If the aBody template rule were left out, the entire text would be missing. The bottom line is, be sure you have accounted for every element type you wish to render, either by creating a template rule that names the element in the select attribute, or by including an <xsl:value-of> somewhere in the style sheet.

Handling HTML That Is Not Well-Formed

How do I handle HTML elements that are not well-formed in my style sheet?

Because an XSLT style sheet is a well-formed XML document, all elements in the document must conform to the rules of well-formedness. This includes the HTML elements you create. The problem is, not all HTML elements are well formed. For instance, HTML's <BR> element contains an opening tag, but not a closing tag. If you include the <BR> element in your style sheet, the XML parser will generate an error along the lines of

End tag 'xxx' does not match the start tag 'BR'. 

The proper way to solve this is to create a new result element using the <xsl:element> element. This technique is described in Chapter 4. Most processors will generate the proper transformation.

A second approach you can use is to force your style sheet to generate XHTML instead of plain HTML. XHTML is a reformulation of the language that makes it well formed according to the rules of XML. You can do this by using the following namespace in your <stylesheet> declaration:

xmlns="http://www.w3.org/1999/xhtml"

However, there will be times when the browser you're targeting can't handle XHTML. In that case, you can get away with a kludge. By including a closing </BR> tag, your style sheet will be well formed according to the rules of XML. Your style sheet will generate the following transformation:

<BR></BR>

This is, of course, illegal syntax according the HTML specification. However, virtually all browsers accept such inconsistencies and will render the line break.

  • + Share This
  • 🔖 Save To Your Account