XML technologies / XSLT / XSLT Kochbuch / Making the most of XSLT 2.0 / Solving difficult serialization problems with character maps

(Excerpt from "XSLT Kochbuch" by Sal Mangano)

Problem

You need precise control over document serialization. The XSLT 1.0 feature disable-output-escaping is too limited for your needs.

Solution

XSLT 2.0 offers a new feature called a character map (Character Map), providing precise control over serialization. A character map is intended for use with the instruction xsl:output.

The instruction xsl:character-map accepts the following attributes:

name

Defines the character map’s name.

use-character-maps

A list of other character maps to include in this one.

 

The content of xsl:character-map is a sequence of xsl:output-character elements. These elements define a mapping between a single Unicode character and a string to output in its place during serialization. The following map can output various special characters as Entities:

<xsl:character-map name="spaces">
  <xsl:output-character char="&#xA0;" string=" "/>
  <xsl:output-character char="&#x2003;" string="&emsp;"/>
  <xsl:output-character char="&#x2007;" string="&numsp;"/>
  <xsl:output-character char="&#x2008;" string="&puncsp;"/>
  <xsl:output-character char="&#x2009;" string="&thincsp;"/>
  <xsl:output-character char="&#x200A;" string="&hairsp;"/>
</xsl:character-map>

Another, more sophisticated use of character maps is to output non-standard documents that would be difficult to produce because they violate XML or XSLT rules. In his book XSLT Programmer's Reference (3rd Edition), Michael Kay gives an example of outputting commented-out elements. Here is a variation of his example. The basic idea is to generate a copy of the input document in which the contents of certain elements are commented out using XML comments:

<?xml version="1.0"?>
<!-- Definieren eigener Entities mit Hilfe der Unicode-Zeichen für den privaten Gebrauch -->
<!DOCTYPE xsl:stylesheet [
  <!ENTITY start-comment "&#xE501;">
  <!ENTITY end-comment "&#xE502;">
]>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <!-- Importieren der Transformation -->
  <xsl:import href="copy.xslt"/>
  <!-- Dem Serialisierer mitteilen, dass er unsere unten definierte Zeichenkarte verwenden soll -->
  <xsl:output use-character-maps="comment-delimiters"/>
  <!-- Definieren eines Schlüssels, der verwendet wird, um Elemente zu identifizieren, die auskommentiert werden sollen. -->
  <xsl:key name="omit-key" match="omit" use="@id"/>
  <!-- Abbilden unserer eigenen Entities auf Strings, die die Syntax der XML-Start- und -Endkommentare formen -->
  <xsl:character-map name="comment-delimiters">
    <xsl:output-character character="&start-comment;" string="&lt;!--"/>
    <xsl:output-character character="&end-comment;" string="--&gt;"/>
  </xsl:character-map>
  <!-- Auskommentieren solcher Elemente, die ein id-Attribut besitzen, das der id eines omit-Elements aus dem externen Dokument omit.xml entspricht. -->
  <xsl:template match="*[key('omit-key',@id,doc('omit.xml'))]">
    <xsl:text>&start-comment;</xsl:text>
    <xsl:copy>
      <xsl:apply select="@* | *"/>
    </xsl:copy>
    <xsl:text>&end-comment;</xsl:text>
  </xsl:template>
</xsl:stylesheet>

See also

Evan Lenz developed an XML-to-string converter, providing an alternative approach to difficult serialization problems.

  

 

 

 

back to top