XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – getting started / A closer look at Literal Result Elements

(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 1.)

The template rule in our first example contained only Literal Result Elements, which formed the result document:

<xsl:template match="/">
  <html>
    <head>
      <title>Ergebnisdokument</title>
    </head>
    <body>
      <h1 align="center">Die erste XSLT-Transformation</h1>
      <p>Ein Textabsatz. Später mehr.</p>
    </body>
  </html>
</xsl:template>
  

They appear in the result exactly as in the template, including line breaks and indentation: whitespace characters.

We have quietly been saying Literal Result Elements even though elements are not the whole story. The example also contains text, the string Die erste XSLT-Transformation, which is copied too.

Literal Result Element is not quite accurate here; the proper term would be Literal Data Characters. In practice the two are not distinguished.

Attributes in Literal Result Elements

Attributes in Literal Result Elements are copied unchanged too. This includes presentation attribute align="center" on HTML heading <h1>. Yes, attribute align is obsolete, but serves as an example. Multiple copied attributes retain their order.

Introducing attribute value templates

An attribute is not an element: it has a name and a variable value. Instead of "center", attribute align could contain "right" or "left". Attribute values cannot always be fixed when writing the stylesheet.

Literal Result Elements allow a copied attribute’s value to be determined dynamically when the template is instantiated, making the concept much more flexible.

We will not yet discuss where the value comes from. The general syntax is:

<h1 align="{XPath-Ausdruck}"> ... </h1>

This is called an attribute value template, AVT, Attribute Value Template. It replaces the attribute value with a placeholder resolved at runtime. (Note: Do not confuse this official use of “template” with a template rule; “placeholder” would be more accurate.) An XPath expression determines the value. Curly braces delimit the AVT region inside an attribute value.

   

back to top