Home > Articles > Programming > Java

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

This chapter is from the book

Creating Text

Returning to Listing 2.2, you'll notice that following the <xsl:template match="/"> tag, there are several HTML tags. To recap, the template rule from Listing 2.2 is repeated here, in Listing 2.3.

Listing 2.3 The Root Template Rule from Listing 2.2

<!-- Root template rule -->
<xsl:template match="/">
  <HTML>
   <HEAD>
     <TITLE>First XSLT Example</TITLE>
   </HEAD>
   <BODY>
     <P><B>Company: </B>
      <xsl:value-of select="invoice/clientName" />
     </P>
     <P><B>Contact: </B>
      <xsl:value-of select="invoice/contact" />
     </P>
     <P><B>Services Rendered: </B>
      <xsl:value-of select="invoice/descriptionOfServices" />
     </P>
     <P><B>Total Due: </B>
      $<xsl:value-of select="invoice/costOfServices" />
     </P>
   </BODY>
  </HTML>
</xsl:template>

Anything contained in a template rule that is not a recognized style-sheet element is considered to be text and is inserted into the result tree as a text node. Put another way, you can write text, free form, directly into your template rules and it will be inserted exactly the way it was written (including whitespace) in the result tree. Thus, when the processor sees the Contact: label in Listing 2.3, it creates a text node and inserts that directly into the result tree.

The root template rule in Listing 2.3 is creating the preamble for an HTML document, inserting the appropriate <HTML>, <HEAD>, <TITLE>, and <BODY> tags just as you have written them in the template rule.

NOTE

Entering text into a style sheet is not the only way to create text nodes in the result tree. In Chapter 4, you'll learn how to use the <xsl:text> element to create text nodes. It turns out, however, that creating text in transformations is so common, XSLT allows you to write text in this freeform style described above.

  • + Share This
  • 🔖 Save To Your Account