Declarations

Declarations are the top-level elements that may appear at the start of a stylesheet module, before any template rules.

<xsl:preserve-space>, <xsl:strip-space>

<xsl:strip-space> strips whitespace-only text nodes from the listed elements; <xsl:preserve-space> keeps them. Both take a space-separated list of element names (or "*") in an elements attribute. An element's own xml:space="preserve" attribute overrides these declarations for that element.

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <a>    <e/>     </a>
   <b>    <e/>     </b>
   <c>    <e/>     </c>
</root>

<?xml version="1.0"?>
<xsl:stylesheet version="3.0"
          xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:preserve-space elements="a c"/>
   <xsl:strip-space elements="b"/>
   <xsl:template match="root">
      <xsl:text>&#10;</xsl:text>
      <xsl:copy-of select="a,b,c"/>
      <xsl:text>&#10;</xsl:text>
      <d xml:space="preserve">     <e/>     </d>
      <f>        <g/>        </f>
   </xsl:template>
</xsl:stylesheet>

b is stripped (per <xsl:strip-space>) while a and c keep their whitespace (per <xsl:preserve-space>); d keeps its whitespace because of its own xml:space="preserve", while f, covered by neither declaration, is stripped by the processor's own default whitespace handling.

<xsl:decimal-format>

<xsl:decimal-format> defines the symbols used by format-number(). The example below defines a named decimal format df and uses it to format the result of a division by zero.

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="decimalformat.xsl"?>
<xsl:stylesheet version="1.0"
         xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
   <xsl:decimal-format name="df"
                                 decimal-separator="."
                                 grouping-separator=","
                                 infinity="INFINITY"
                                 NaN="Not a Number"
                                 minus-sign="-"
                                 percent="%"
                                 per-mille="m"
                                 zero-digit="0"
                                 digit="#"
                                 pattern-separator=";" />
   <xsl:template match="/">
      <xsl:value-of
         select="format-number(1 div 0, '###,###.00', 'df')"/>
   </xsl:template>
</xsl:stylesheet>

INFINITY

<xsl:namespace-alias>, <xsl:mode>, <xsl:attribute-set>, <xsl:key>, <xsl:character-map>, <xsl:output-character>

The example below combines several declarations: <xsl:namespace-alias> substitutes the literal prefix x in the stylesheet for the result prefix xsl in the output (letting a stylesheet generate <xsl:...> elements as data rather than as instructions); <xsl:mode on-multiple-match="use-last"/> makes the last-declared of two equally-matching template rules win, instead of raising an error; <xsl:attribute-set> names a reusable group of attributes, applied with xsl:use-attribute-sets; <xsl:key> indexes nodes for lookup via the key() function (two declarations sharing the name k combine into a single index); and <xsl:character-map>, activated with <xsl:output use-character-maps="cm"/>, substitutes literal output strings for individual characters via nested <xsl:output-character> elements.

<?xml version="1.0" encoding="UTF-8"?>
<r att="hello">
   <a att="one_hundred">100</a>
   <a att="two_hundred">200</a>
   <b att="three_hundred">300</b>
</r>

<xsl:stylesheet version="3.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:fo="http://www.w3.org/1999/XSL/Format"
     xmlns:x="http://www.w3.org/1999/XSL/Transform">
   <xsl:namespace-alias
                           stylesheet-prefix="x" result-prefix="xsl"/>
   <xsl:mode on-multiple-match="use-last"/>
   <xsl:attribute-set name="aGrp">
      <xsl:attribute name="a1" select="1"/>
      <xsl:attribute name="a2" select="2"/>
      <xsl:attribute name="a3" select="3"/>
   </xsl:attribute-set>
   <xsl:character-map name="cm">
      <xsl:output-character character="&#171;" string="[[[" />
      <xsl:output-character character="&#187;" string="]]]" />
      <xsl:output-character character="&#167;" string='"' />
   </xsl:character-map>
   <xsl:output use-character-maps="cm" />
   <xsl:key name="k" match="a" use="@att"/>
   <xsl:key name="k" match="b" use="@att"/>

   <xsl:template match="r">
      This will not be printed because of the mode set.
   </xsl:template>
   <xsl:template match="r">
      <xsl:text>&#10;1</xsl:text>
      <fo:block xsl:use-attribute-sets="aGrp"/>
      <x:text>&#10;2</x:text>
      <xsl:copy-of select="key('k',a/@att)"/>
      <x:text>&#10;3</x:text>
      <xsl:copy-of select="key('k',b/@att)"/>
      <x:text>&#10;4</x:text>
      <xsl:copy-of select="key('k',a)"/>
      <x:text>&#10;5</x:text>
      &#171;<dummy att='&#167;'/>&#187;
   </xsl:template>
</xsl:stylesheet>

1 2100200 3300 4 5 [[[]]]
Only the second <xsl:template match="r"> rule fires, because on-multiple-match="use-last" resolves the conflict between the two equal-priority rules in favor of the one declared last.

<xsl:mode> can have the following attributes: name (eqname), streamable ("yes" | "no"), on-no-match ("deep-copy" | "shallow-copy" | "deep-skip" | "shallow-skip" | "text-only-copy" | "fail"), on-multiple-match ("use-last" | "fail"), warning-on-no-match ("yes" | "no"), warning-on-multiple-match ("yes" | "no"), typed ("yes" | "no" | "strict" | "lax" | "unspecified"), visibility ("public" | "private" | "final").
<xsl:character-map> can have the following attribute: use-character-maps (eqnames).
<xsl:key> can have the following attributes: composite ("yes" | "no"), collation (uri).