O'Reilly logo

XML Hacks by Michael Fitzgerald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Build Results with Literal Result and Instruction Elements

Use literal result elements, literal text, and instruction elements in an XSLT stylesheet.

This is a hack for XSLT beginners. If you are adding or changing markup in a result tree with XSLT, this hack will help you do it. You will learn how to use literal result elements (along with literal text) and XSLT instruction elements to build your output with new or additional markup.

Literal Result Elements and Literal Text

A literal result element in XSLT is an XML element written literally in a template. Literal result elements can include attributes and must produce well-formed output. Literal text appears as plain, literal text in templates, for stylesheets that have text output.

The stylesheet timedate.xsl, shown in Example 3-35, augments time.xml with additional markup using literal result elements.

Example 3-35. timedate.xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" encoding="ISO-8859-1" indent="yes"/>

   

<xsl:template match="/">

 <instant>

  <xsl:apply-templates select="time"/>

  <date>

   <year>2004</year>

   <month>-06</month>

   <day>-30</day>

  </date>

 </instant>

</xsl:template>

   

<xsl:template match="time">

 <xsl:copy>

  <xsl:attribute name="timezone">

   <xsl:value-of select="@timezone"/>

  </xsl:attribute>

   <xsl:copy-of select="hour|minute|second|atomic"/>

 </xsl:copy>

</xsl:template>

   

</xsl:stylesheet>

On line 2, the output is encoded as Latin1 or ISO-8859-1 (http://en.wikipedia.org/wiki/ISO_8859-1 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required