The xsl:stylesheet Element

The <xsl:stylesheet> Element

XSL stylesheets begin with the XML declaration because they are well- formed XML documents, so planets.xsl starts with this same declaration:

 <?xml version="1.0"?>      .      .      . 

The XML declaration is stripped off by the XSLT processor immediately, however, and wont figure in our XSLT discussion. The first element of an XSL stylesheet that is pure XSL is the <xsl:stylesheet> element (not to be confused with the <?xml-stylesheet?> processing instruction, which goes in the XML document). Some people have objected to the name of this element, because its usually used in XSLT transformations, so W3C also allows you to refer to this element as <xsl:transform> .

Heres how you use this element:

 <?xml version="1.0"?>  <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">      .      .      . 

The following list includes the attributes of <xsl:stylesheet> :

  • id (optional). Used to identify the stylesheet. Set to an XML name.

  • version (mandatory). Sets the version of XSLT needed to work with the stylesheet. This value is normally set to 1.0 currently. You can also set this value to 1.1; although, because XSLT 1.1 is not going to go past the working draft stage, this will probably not be a legal value as far as W3C is concerned .

  • extension-element-prefixes (optional). Defines the extensions in the stylesheet used to identify extension elements. Set to a whitespace-separated list of NCNames .

  • exclude-result-prefixes (optional). Specifies the namespaces in the stylesheet that should not be copied to the output (unless they are intentionally used in the output document). Set to a whitespace-separated list of NCNames .

The content of this element can consist of any of the following top-level XSL elements: <xsl:attribute-set> , <xsl:decimal-format> , <xsl:import> , <xsl:include> , <xsl:key> , <xsl: namespace-alias > , <xsl:output> , <xsl:param> , <xsl: preserve-space > , <xsl: strip-space > , <xsl:template> , or <xsl:variable> . XSLT 1.1 added <xsl:script> to this list.

Note that the id attribute of this element can be used when you want to refer to a particular stylesheet (and note that you should have an XSLT processor that also reads DTDs or XML schemas in this case).

The version attribute is mandatory, and currently can be set to 1.0. You can also set this value to 1.1 for the XSLT 1.1 working draft; however, because XSLT 1.1 is not going past the working draft stage, 1.1 will probably not be considered a legal value for this attribute in the long run. Im going to set the version attribute to 1.0 in this book, because thats the current W3C XSLT recommendation, and version 1.1 is not going to progress beyond working draft stage. As mentioned in Chapter 1, the W3C has also released the requirements for XSLT 2.0, so that will be the next version.

XSLT Processors and Forward Compatibility

If the XSLT version is not one that the XSLT processor recognizes, W3C says the processor must assume any new elements are part of the new XSLT version and not just quit, which is what W3C calls forward compatibility . Any elements the processor does not recognize must not be rejected unless the stylesheet tries to instantiate that element and finds no <xsl:fallback> child element (see Chapter 5). The upshot is that you can set the XSLT version to 2.0, even in XSLT processors that were written for XSLT 1.0, and there should be no problem unless you use XSLT 2.0-specific features. (An exception here seems to be MSXML3, which currently does generate errors if you set the version to values other than 1.0.)

The XSL Namespace

Note that XSLT elements such as <xsl:stylesheet> use the namespace prefix xsl , which, now that XSLT has been standardized, is always set to http://www.w3.org/1999/XSL/Transform. That namespace is usually set with the xmlns attribute (which is not an XSL attribute, but rather an attribute of any XML element), in the stylesheets root element, <xsl:stylesheet> :

 <xsl:stylesheet version="1.0" xmlns::xsl="http://www.w3.org/1999/XSL/Transform">      .      .      . 

XSL stylesheets use their own namespace this way to avoid conflict with the other elements you use in your stylesheet. For example, you might want to indicate what stylesheet was used to transform a document, in which case you might create your own <stylesheet> elementwhich is no problem, because it wont conflict with the XSL element <xsl:stylesheet> .

The namespace prefix customarily used in XSLT stylesheets for XSLT elements is xsl . You can, in fact, use any namespace prefix you want for XSLT elements (or even none at all), but xsl is used almost universally because thats the namespace used throughout the XSLT recommendation.

More on Namespaces

For more on namespaces, see the companion New Riders book, Inside XML , which has an in-depth treatment and plenty of examples.

In practice, this means that all XSLT elements well be using start with the namespace prefix xsl , as in <xsl:stylesheet> . That, then, is how you start an XSLT stylesheet, with the <xsl:stylesheet> element. (There is one exception: simplified stylesheets omit this element, as youll see later in this chapter).

Like any other XML application, XSLT has a well-defined set of rules for what makes an XSLT stylesheet valid. The W3C even defines a pseudo-DTD for XSLT, listing all the syntax rules, and you can find that DTD in Appendix A, which is a good resource if youre ever at a loss to understand some part of XSLT syntax. As Appendix A shows, the <xsl:stylesheet> element can legally contain several other XSLT elements, and those elements are called top-level elements .

Handling Default Namespaces

The question of namespaces can be tricky. For example, some people assign a default namespace to the whole <xsl:stylesheet> element as in <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="mydefault"> , and then expect <xsl:template match="mysymbol"> to match mysymbol in the mydefault namespace of the source document. It wont, though. Handling default namespaces such as this is one of the issues that XSLT 2.0 is supposed to address.



Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
EAN: N/A
Year: 2005
Pages: 196

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net