XML technologies / XSLT / XSLT Kochbuch / Making the most of XSLT 2.0 / Using types for safety and precision
(Excerpt from "XSLT Kochbuch" by Sal Mangano)
Problem
The limited type checking in XSLT 1.0 restricted the robustness of your stylesheets.
Solution
Use the extended type system of XSLT 2.0 to create precise, type-safe functions and templates.
Use the as attribute on elements that contain or return data.
These elements include xsl:function, xsl:param, xsl:template, xsl:variable and xsl:with-param.
Use the type attribute on elements that generate data.
These elements include xsl:attribute, xsl:copy, xsl:copy-of, xsl:document, xsl:element and xsl:result-document.
Discussion
All standards-compliant XSLT 2.0 processors allow you to use simple data types such as xs:integer or xs:string to describe variables or parameters. These types can also be used with the symbols *, + and ? to describe sequences of these simple types:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<!-- x ist eine Folge aus null oder mehreren Strings -->
<xsl:variable name="x" select="para" as="xs:string*"/>
<!-- y ist eine Folge aus einem oder mehreren Strings. Wir kodieren die Auswahl auf eine Weise, die dies garantiert, obwohl Sie den if-Ausdruck weglassen könnten, falls Sie wüssten, dass es wenigstens ein para-Element gibt -->
<xsl:variable name="y" select="if (para) then para else ''" as="xs:string+"/>
<!-- z ist eine Folge aus einem oder mehreren Strings. -->
<xsl:variable name="z" select="para[1]" as="xs:string?"/>
</xsl:stylesheet>With a schema-aware processor, you can go further and refer to both simple and complex types from a custom schema. A schema-aware processor must be informed of custom types through the top-level instruction xsl:import-schema:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:my="http://www.mydomain.com/ns/my">
<xsl:import-schema schema-location="my-schema.xsd"/>
<xsl:template match="/">
<!--Validieren, dass das resultierende Element zu my:BookType konform geht -->
<xsl:element name="Book" type="my:BookType">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<!-- ... -->
</xsl:stylesheet>You should not use xsl:import-schema if you do not have access to a schema-aware processor. If you use one but want your stylesheets to remain compatible with processors that are not schema-aware, all elements requiring a schema-aware processor must use the attribute use-when="system-property('xsl:schema-aware') = 'yes'".
Copyright © 2006 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from "XSLT Kochbuch" is subject to the same terms as the printed edition: the work and all its parts are protected by copyright. All rights are reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O'Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de