Element content to attribute

Almost as common as attribute value copy-through is the need to convert an element value to an attribute value. For example, in the source document the text to display if the image cannot be presented may be stored in the Image element, as element content:

<image name="boat.gif" y="60mm" x="40mm">
Picture of a boat.
</image>

In the HTML IMG element, this text needs to appear in the ALT attribute:

<IMG SRC="boat.gif" ... ALT="Picture of a boat."/>

The expression language makes this transformation almost as simple as copying-through attribute values. In this case, the value required is the text content of the element. The text node within the current element is represented by the 'text()' function:

 <xsl:template ...

Get XSL companion, The now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.