Home > Articles > Programming > Java

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

This chapter is from the book

Getting the Content of an Element

As you scan down Listing 2.3, you'll see the first XSLT element, <xsl:value-of>. The purpose of the <value-of> element is to retrieve the content from an element or an attribute. The <value-of> element takes an optional select attribute and a pattern (or location path) that allows you to specify a node or collection of nodes from which to get content. In this case, Listing 2.3 uses <xsl:value-of select="invoice/clientName"> to grab the content from the <clientName> element in the source tree and insert it into the result tree.

In constructing the pattern, you'll notice that the step pattern does not contain a beginning slash to tell the processor to begin its search at the root node. The reason is that the template rule's match attribute has already done this for you. That is, <xsl:template match="/"> sets the context. Therefore, to get to the clientName, you only need to write invoice/clientName. Of course, writing /invoice/clientName would work equally well in this case. However, it's important to remember in other template rules, the template may be named with another element-type name. That name sets the context for further pattern searches within that template rule. You'll see an example shortly.

The remaining code in the template rule in Listing 2.3 creates additional HTML labels, then uses <value-of> to retrieve content for the <contact>, <descriptionOfServices>, and <costOfServices> elements. The process is exactly as I have just described for the <clientName> element. Finally, the closing HTML tags are inserted and the template rule is done. If you refer back to Listing 2.2, this is also the end of the style sheet, as noted by the closing </xsl:stylesheet>.

  • + Share This
  • 🔖 Save To Your Account