XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / namespace-alias
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 6.)
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z
Declaration xsl:namespace-alias replaces a namespace bound to output elements with another namespace when writing the result document.
Classification | Declaration |
Function group | Masking the XSLT namespace |
Introduction | XSLT 1.0 |
Position in the stylesheet and permitted contents:
Declaration xsl:namespace-alias is top-level and occurs only as an immediate child of root elements xsl:stylesheet or xsl:transform. The element is always empty.
Attributes:
Standard attributes apply. In addition, xsl:namespace-alias has two required attributes stylesheet-prefix and result-prefix identifying the prefixes of the namespace to replace and its alias.
result-prefix
Value | prefix | "#default" |
Usage | Required |
Introduction | XSLT 1.0 |
The result-prefix attribute gives the prefix of the namespace to use in the result instead of the replaced namespace. To map Literal Result Elements to the XSLT namespace, for example, the value must be "xsl".
stylesheet-prefix
Value | prefix | "#default" |
Usage | Required |
Introduction | XSLT 1.0 |
The stylesheet-prefix attribute names the prefix bound to the namespace to replace.
To map the namespace declared by xmlns (default namespace ) of unprefixed elements to a new namespace, use value "#default". If no default namespace is declared, the null namespace is assumed.
Note: If the same value of attribute stylesheet-prefix occurs in multiple xsl:namespace-alias declarations, for example through imports, the highest import precedence wins.
Purpose:
Instruction xsl:namespace-alias replaces a stylesheet namespace identified by prefix, or its default namespace, with another in the result document. Each replaced namespace requires a separate xsl:namespace-alias declaration.
Its main use is indirectly outputting XSLT elements when an XSLT transformation generates another stylesheet. Directly outputting XSLT instructions or declarations as Literal Result Elements is not possible, since processing would execute them.
An arbitrary dummy namespace and prefix disguise the intended XSLT instructions as ordinary Literal Result Elements, assigning the XSLT namespace only on output. Remember: only the bound namespace URI identifies an XSLT instruction; even prefix “xsl” is optional.
The XSLT processor determines whether the dummy prefix, in the example axsl:, is actually replaced by result prefix xsl:. If not, the old prefix is still assigned the new namespace URI, here XSLT. (Note: The generated stylesheet would then use “axsl” rather than “xsl”. To avoid this, use another prefix for actual XSLT instructions in the generating stylesheet and reserve “xsl” for the dummy namespace.)
Examples:
Example 1 – Mapping a namespace to XSLT:
Stylesheet:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:axsl="http://dummy-namespace">
<xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/>
<xsl:template match="/">
<axsl:stylesheet version="1.0">
<axsl:template match="/">
<ausgabe>Namespace-Mapping erfolgreich!</ausgabe>
</axsl:template>
</axsl:stylesheet>
</xsl:template>
</xsl:stylesheet>
Result document (Saxon):
<?xml version="1.0" encoding="utf-8"?>
<axsl:stylesheet xmlns:axsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<axsl:template match="/">
<ausgabe>Namespace-Mapping erfolgreich!</ausgabe>
</axsl:template>
</axsl:stylesheet>
Saxon retains prefix axsl: but assigns it the XSLT namespace. Prefix xsl: is additionally declared with the same namespace.
Result document (Xalan):
<?xml version="1.0" encoding="UTF-8"?>
<axsl:stylesheet xmlns:axsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<axsl:template match="/">
<ausgabe>Namespace-Mapping erfolgreich!</ausgabe>
</axsl:template>
</axsl:stylesheet>
Xalan also retains prefix axsl: and assigns it the XSLT namespace. Prefix xsl: is not additionally declared.
Result document (msxml.NET):
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<ausgabe>Namespace-Mapping erfolgreich!</ausgabe>
</xsl:template>
</xsl:stylesheet>
The msxml.NET processor discards the original prefix and replaces it with prefix xsl: with the correct XSLT namespace.
Example 2 – Mapping the default namespace to XSLT:
Stylesheet:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:a="http://literal-result-namespace" xmlns="http://dummy-namespace">
<xsl:namespace-alias stylesheet-prefix="#default" result-prefix="xsl"/>
<xsl:template match="/">
<stylesheet version="1.0">
<template match="/">
<a:ausgabe>Namespace-Mapping erfolgreich!</a:ausgabe>
</template>
</stylesheet>
</xsl:template>
</xsl:stylesheet>
Here the real Literal Result Elements need a namespace distinguishing them from the future XSLT elements in the generated stylesheet.
Result document (Xalan):
<?xml version="1.0" encoding="UTF-8"?>
<stylesheet xmlns="http://www.w3.org/1999/XSL/Transform" xmlns:a="http://literal-result-namespace" version="1.0">
<template match="/">
<a:ausgabe>Namespace-Mapping erfolgreich!</a:ausgabe>
</template>
</stylesheet>
Xalan makes XSLT the default namespace in the result. The other namespace, used for Literal Result Elements, is copied unchanged.
Element definition:
XSLT 1.0:
<!-- Category: top-level-element -->
<xsl:namespace-alias
stylesheet-prefix = prefix | "#default"
result-prefix = prefix | "#default"
/>
XSLT 2.0:
<!-- Category: declaration -->
<xsl:namespace-alias
stylesheet-prefix = prefix | "#default"
result-prefix = prefix | "#default"
/>
Copyright © Galileo Press, Bonn 2008
You may print the online version for your private use.
Otherwise, this chapter from “XSLT 2.0 & XPath 2.0 ― Das umfassende Handbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn