Chapter 2. Fundamental Concepts of XSLT Stylesheets

CONTENTS
  •  2.1 Boilerplates for XSLT Stylesheets
  •  2.2 Embedding Stylesheets in XML Documents
  •  2.3 XSLT Stylesheet Terminology
  •  2.4 XML Components of XSLT Stylesheets
  • Boilerplate stylesheets

  • <xsl:stylesheet> and <xsl:transform> document element

  • Literal result element

  • Literal result stylesheet

  • Stylesheets embedded in an XML document

  • XML declaration

  • Document type declaration

In Chapter 1 we covered several XSLT elements and basic concepts that allowed stylesheets to be immediately useable. This chapter is devoted to fully explaining the nature or structure of XSLT stylesheets and the core concepts underlying them. We will discuss the <xsl:stylesheet> and <xsl:transform> document elements and their structure, along with a basic overview of their child elements, including <xsl:template>. We will also present boilerplate stylesheets that can be used as the base of any XSLT transformation, as well as an example of using stylesheets directly in an XML document.

2.1 Boilerplates for XSLT Stylesheets

Building upon the information presented in Chapter 1, a boilerplate XSLT stylesheet, using <xsl:stylesheet> as the document element, would result in the basic structure required for any stylesheet, as shown in Example 2-1 .

The document element is used to contain the rest of the stylesheet, as well as providing a convenient place to declare the version and namespace for XSL. The XSLT version is shown here as version="1.0" and the XSL namespace, defined using the xmlns namespace declaration attribute, is shown here as:

Example 2-1 Basic XSLT stylesheet structure.
<?xml version="1.0"?>       <xsl:stylesheet             xmlns:xsl="http://www.w3.org/1999/XSL/Transform"             version="1.0" >             <!--The various top-level elements                    go here -->       </xsl:stylesheet> 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 

The document element of an XSLT stylesheet can be either <xsl:stylesheet> or <xsl:transform>, or, as shown in Section 2.1.2, can also be a literal result element.

2.1.1 Document Element: <xsl:stylesheet> or xsl:transform>

The document element, whether <xsl:stylesheet> or <xsl:transform>, contains the other elements in a stylesheet, and is used to declare the version of XSLT that the stylesheet conforms to. The document element is also used to tell the processor that the stylesheet is an XSLT stylesheet, using the xmlns attribute. Both elements have the same structure and content, as shown in the definitions below:

<xsl:stylesheet   id = id   extension-element-prefixes = tokens   exclude-result-prefixes = tokens   version = number>   <!-- Content: (xsl:import*, top-level-elements) --> </xsl:stylesheet> 

Besides the required version attribute, both elements have an optional id attribute, which is used to provide an ID for the stylesheet when it is embedded directly in an XML document (see Section 2.2), and two attributes related to namespace prefixes (discussed in Chapter 12).

<xsl:transform   id = id  extension-element-prefixes = tokens  exclude-result-prefixes = tokens  version = number>  <!-- Content: (xsl:import*, top-level-elements) --> </xsl:transform> 

Apart from its syntactic role as the document element of an XSLT stylesheet, the <xsl:stylesheet> or <xsl:transform> element serves to establish the fact that the document is an XSTL stylesheet by declaring it as one with the xmlns attribute. The xmlns attribute, while not listed in the element model definitions from the XSLT specification, is nevertheless allowed on this element. The xmlns attribute on the document element is used to declare the XSL namespace (see Section 12.3.4).

The following sections provide a review of the document element's other attributes, as well as the xml:space attribute.

2.1.1.1 The version Attribute

The version attribute is a required attribute for the document element. It provides information to the XSLT processor to determine which version of the XSLT specification the stylesheet requires for correct processing. The following attribute model definition shows its structure:

ATTRIBUTE: version NMTOKEN #REQUIRED VALUE = number 

An example of using the version attribute is as follows:

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

The value of the version attribute will be 1.0 until a new version of the XSLT specification becomes a recommendation.

2.1.1.2 The extension-element-prefixes Attribute

The extension-element-prefixes attribute is used to let the processor know if any extension elements are being used, and what their prefixes are. Prefixes for extension elements are established by adding namespaces (discussed in Chapter 12) to the document element. Any element in a template that uses a prefix other than xsl will be treated as a literal result element (LRE), sent to the output as is, unless the prefix is designated as an extension prefix using the extension-element-prefixes attribute. Once a prefix is designated as an extension prefix, elements in a template that have that prefix are treated as instruction elements and processed according to the rules of the namespace that governs that prefix. The following attribute model definition shows the proper structure of the extension-element-prefixes attribute:

ATTRIBUTE: extension-element-prefixes CDATA #IMPLIED VALUE: = tokens 

Declaring the XSL namespace using xmlns gives you access to the functions and top-level elements defined in the namespace, but it does not let you use its prefix in an element inside a template without also adding the prefix to the list of prefixes in the value of the attribute. (Note that some processors do not require this for their own extensions.)

The scope of any extension namespace is limited to the branch of the stylesheet starting with the element that contains either the extension-element-prefixes or xsl:extension-element-prefixes attribute. This does not include any stylesheets that are imported or included within the stylesheet. If the namespace is to be allowed within another stylesheet, it must be declared and designated in that stylesheet. In other words, namespaces are only available to elements that fall inside the branch of the element that declares the namespace.

Note

The attribute extension-element-prefixes is allowed on the document element only; however, the attribute xsl:extension-element-prefixes is allowed on an LRE or an extension element.

When declaring XT and Saxon namespaces in our stylesheet, we add the extension-element-prefixes attribute as follows:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"       version="1.0"       xmlns:xt="http://www.jclark.com/xt"       xmlns:saxon="http://icl.com/saxon"       extension-element-prefixes="xt saxon" > 
2.1.1.3 The exclude-result-prefixes Attribute

The attribute exclude-result-prefixes allows the removal of namespace prefixes from the result tree elements. This attribute is optional, and its value is a whitespace-separated list of namespace prefixes. The namespaces listed must have been declared in the stylesheet. The attribute model definition below shows the required structure of this attribute:

ATTRIBUTE: exclude-result-prefixes CDATA #IMPLIED VALUE = = tokens 

This attribute is primarily provided to clean up any namespace prefixes from the result tree that are inconsequential for the output. It is used to exclude any namespace prefixes from the LREs included in a stylesheet.

For the following example, we might declare the namespace "http://vedavid.org/x-nology" with a prefix of "veda," as shown below.

<xsl:stylesheet xmlns:xsl="http://www.w3org/1999/XSL/Transform"       version="1.0"       xmlns:xt="http://www.jclark.com/xt"       xmlns:saxon="http://icl.com/saxon"       xmlns:veda="http://vedavid.org/x-nology"       extension-element-prefixes="xt saxon veda"       exclude-result-prefixes="veda" > 

To suppress the output of any prefix for the veda namespace used on an LRE, we add the exclude-result-prefixes attribute. Note that including the prefix with the extension-element-prefixes attribute is not a direct inverse action for excluding it with exclude-result-prefixes. The attributes extension-element-prefixes and exclude-result-prefixes are not direct opposites, because exclude-result-prefixes only affects the output, while extension-element-prefixes affects how the processor reads the elements in the stylesheet.

2.1.1.4 The id Attribute

The id attribute is defined as a type ID, as shown in the following attribute model definition. It is used to add functionality for cross-referencing a document element.

ATTRIBUTE: id ID #IMPLIED VALUE = id 

This optional attribute is very uncommon when using the <xsl:stylesheet> or <xsl:transform> document elements. It is more common when using an LRE as the document element, when the resulting document requires IDs on elements. It is also used when embedding a stylesheet directly in an XML document later (as shown in Example 2-4).

2.1.1.5 The xml:space Attribute

The xml:space attribute comes from the XML specification and provides functionality for preserving whitespace text nodes that are found in the stylesheet. It is allowed on the document element to provide a default action for preserving or removing whitespace nodes from all the descendant elements of the document element. The value for the xml:space attribute is a choice of either default or preserve, as shown in the following attribute model definition:

ATTRIBUTE:  xml:space (default|preserve) #IMPLIED VALUE = (default|preserve) 

If other elements in the stylesheet use the xml:space attribute, they will override the value declared in the document element. This introduces the concept of inheritance. The descendant elements of the document element will inherit the value of the attribute, unless they explicitly override the value with an xml:space attribute of their own.

It is important to note that the xml:space attribute applies only to the structure of the stylesheet, not to the structure of the input source document. Although they perform the same function, the preserving or stripping of nodes from the source tree is controlled with a set of top-level elements, <xsl:strip-space> and <xsl:preserve-space>.

The xml:space attribute is not specifically shown in the content model for this element in the XSLT specification, but it is referenced later in the appendixes.

Note

If this attribute is used on an LRE, the attribute will be passed on to the resulting output as an attribute of that LRE.

2.1.2 Literal Result Element (LRE) Stylesheet

There is a unique kind of stylesheet with a document element that is itself a literal result element (LRE). A literal result element is any element in the stylesheet that does not have a prefix, like xsl, defining it to be part of a namespace. LREs are sent to the output as is, and are discussed in their capacity of generating new XML in Chapter 6. A literal result stylesheet is any stylesheet that has an LRE as the document element.

If an LRE is the first element, or the document element, in a stylesheet, then that LRE must use the same required attributes as either <xsl:stylesheet> or <xsl:transform>, discussed in the previous section. For instance, if the document element is <html>, then the <html> element, as the first element in the stylesheet, requires the XSL namespace declaration and the version attribute for XSLT, as shown in Example 2-2. However, since this is an LRE, the version attribute must be preceded by the xsl prefix. The xmlns attribute is already prefixed, so remains the same.

LREs that reference XSLT attributes use the prefix xsl in front of the attribute to let the processor know that the attribute comes from XSLT, and should be processed according to the rules for that XSLT attribute.

Stylesheets that take the form shown in Example 2-2 are handled by the processor as if the entire stylesheet was inside an <xsl:template> element (part of the template). The XSLT stylesheet in Example 2-3 is the equivalent of Example 2-2, using <xsl:stylesheet> as the document element.

Example 2-2 An LRE stylesheet showing the version and xsl namespace declaration.
<html xsl:version="1.0"       xmlns:xsl="http://www.w3.org/1999/XSL/Transform">       <head>             <title>My document title.</title>       </head>       <body>             <p>My document content.</p>       </body> </html> 
Example 2-3 An XSLT stylesheet that is equivalent to the LRE stylesheet in Example 2-2 .
<xsl:stylesheet             version="1.0"             xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/">       <html>             <head>                   <title>My document title.</title>             </head>             <body>                   <p>My document content.</p>             </body>       </html> </xsl:template> </xsl:stylesheet> 

Because a literal result stylesheet is seen by the processor as a template inside an <xsl:template> element where top-level elements are not allowed LRE stylesheets will never contain top-level XSLT elements.

The document element of an LRE stylesheet also has the option to declare other namespaces that may be used in the stylesheet (see Chapter 12).

2.1.3 Children of the Document Element

There are twelve possible element children of the XSLT stylesheet's document element. These children are called top-level elements because they are allowed at the top level within a document element. There is little or no requisite syntactic or logical order to the elements in an XSLT stylesheet. They may occur within any order, though you will find that writing the various template rules in a manner that reflects either the order of the source tree or the intended result tree is often the most natural procedure.

There is one significant exception regarding the prescribed or otherwise unprescribed order for top-level elements in an XSLT stylesheet. If the <xsl:include> element is to be used, it must without exception come first within the document element. All other top-level elements must follow the <xsl:include> element, if present.

2.1.3.1 Top-Level Elements

Top-level elements are a specific set of XSLT elements that are children of the document element of an XSLT stylesheet. Top-level elements must be direct children of either the <xsl:stylesheet> element or the <xsl:transform> element. They are not allowed inside instruction elements or any other elements that are children of the document element. They are not allowed as children of an LRE element, even if that element is used as the document element. In other words, you cannot nest top-level elements inside instructions, other top-level elements, or LREs. Note that there are two exceptions to this rule, the <xsl:variable> and <xsl:param> elements are allowed at both the top level and at the same level as instruction elements.

Top-level elements are not instructions per se, as they do not instruct the processor to handle nodes from the source tree in a certain way to generate an output result tree fragment. They can be considered more like assistants to or wrappers for the instruction elements and stylesheet as a whole. The <xsl:template> element in particular only selects a set of nodes to be handed to a template. Each top-level element has a specific function, whether passing information to and from instructions or performing administrative roles.

Each of the top-level elements is discussed in more detail in the following chapters, but it is conceptually useful to provide a short list of them here. In addition, this list gives a sampling of the scope of richness in functionality available through XSLT.

  1. <xsl:include> enables an XSLT stylesheet to include templates and instructions from more than one stylesheet. The <xsl:include> element is the only top-level element with a specific syntactic place in the document order, and must always be the first child of the XSLT stylesheet document element.

  2. <xsl:import> enables an XSLT stylesheet to import templates and instructions from external stylesheets.

  3. <xsl:strip-space> removes any text nodes that contain only whitespace from the source tree prior to any further processing.

  4. <xsl:preserve-space> does not remove text nodes that contain only whitespace from the source tree.

  5. <xsl:output> serves to specify the kind of output from the XSLT stylesheet, other than XML. XSLT stylesheets can generate XML, as well as text or HTML.

  6. <xsl:key> declares a set of keys for each document and works with the key() function for indexing elements.

  7. <xsl:decimal-format> is used to declare a specific format for decimal numbers and works with the format-number() function by controlling the interpretation of a pattern.

  8. <xsl:namespace-alias> provides functionality to declare one namespace URI as a replacement for another, which allows the use of different namespaces in the output.

  9. <xsl:attribute-set> defines a named set of attributes that can be used as a group in an output element.

  10. <xsl:variable> is used to define a specific value that can be used in other XSLT elements in the stylesheet.

  11. <xsl:param> allows the definition of a variable that can be modified by the XSLT element using it.

  12. <xsl:template> is the fundamental pattern matching construct for selecting portions of the input source that are to be treated by the instructions contained in the template.

2.2 Embedding Stylesheets in XML Documents

XSLT stylesheets are often used in a batch environment as separate files, but it is also possible to embed a stylesheet directly in an XML document, possibly to deliver the document directly to a browser for rendering. This means that the XML document must be aware of the stylesheet.

There is a separate recommendation from the W3C that identifies the process for embedding stylesheets in XML documents, called Associating Style Sheets with XML Documents, Version 1.0.[1] This recommendation describes a predefined XML processing instruction, or PI, that can be used at the top of any XML document to allow the XML to find the top of the XSLT stylesheet, wherever it is in the XML.

The PI uses an href attribute with a URI to point to the ID of the <xsl:stylesheet> element. This URI must be a fragment identifier (signalled by the # prefix) because it is a reference to a part of the document containing the PI. It is not used to reference an external stylesheet. The following PI model definition shows the two required attributes and four optional attributes for the <?xml-stylesheet?> PI:

<?xml-stylesheet   href = string   type = string   title = string   media = string   charset = string   alternate = "yes" | "no" ?> 

Example 2-4, taken directly from the XSLT specification, shows an embedded stylesheet in an XML document.

Notice that this example contains a few elements from the XSL formatting objects specification. The stylesheet is called into play with the <?xml-stylesheet?> PI. The template rule matching on id('foo') in the example finds an element in the XML with an ID of "foo" and uses the instructions in the template to format it, in this case with a bold font.

Example 2-4 Embedding a stylesheet in an XML document.
<?xml-stylesheet type="text/xml" href="#style1"?> <!DOCTYPE doc SYSTEM "doc.dtd"> <doc> <head> <xsl:stylesheet id="style1"                 version="1.0"                 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"                 xmlns:fo="http://www.w3.org/1999/XSL/Format"> <xsl:import href="doc.xsl"/> <xsl:template match="id('foo')">   <fo:block font-weight="bold"><xsl:apply-templates/></fo:block> </xsl:template> <xsl:template match="xsl:stylesheet">   <!-- ignore --> </xsl:template> </xsl:stylesheet> </head> <body> <para id="foo"> ... </para> </body> </doc> 

The template rule matching on "xsl:stylesheet" is required in all embedded stylesheets so that <xsl:stylesheet> elements are processed as elements.

2.3 XSLT Stylesheet Terminology

It is often possible to do a great deal of work with a given technology without ever knowing exactly what some of its terms mean. Although XSLT is basically XML, there are very specific uses for its elements and attributes. The basic concepts of stylesheet structure go beyond the basic XML tagging structure to form a logical "programming" application. It is especially important, then, that we detail the terminology of XSLT.

2.3.1 Stylesheet

The term stylesheet is for the most part self-explanatory, but in the context of XSLT, it requires a certain qualification. Literally, an XSLT stylesheet is an XML document instance that is processed by an XSLT engine to perform a transformation on other XML documents. A stylesheet follows XML well-formedness rules, uses XSLT-specific namespace declarations, and can contain one or more XSLT templates that select and process elements from the source XML document.

A common use for XSLT stylesheets is to transform the logical structure of an XML document instance from the semantics and syntax of one DTD or schema to another. This is more of what one might traditionally consider a transformation, as it is more an XSLT logical structure transformation than a specific "styling," but it is nonetheless an act of styling that is taking place.

2.3.2 Stylesheet Element and Transform Element

The stylesheet element (<xsl:stylesheet>) is a valid XML element that is the first element (i.e., document element) in a stylesheet. It is the parent or ancestor of all the other elements in a stylesheet. The transform element (<xsl:transform>) is allowed as a replacement for the stylesheet element. Only one of either the <xsl:stylesheet> or <xsl:transform> element is allowed in a stylesheet.

Note

<xsl:stylesheet> and <xsl:transform> are synonymous. For convenience in classifying XSLT stylesheets as opposed to XSL Formatting Object stylesheets, you may wish to use <xsl:transform> for all XSLT stylesheets and <xsl:stylesheet> for all XSL stylesheets.

2.3.3 Result Tree

The result tree is simply the output that is produced from the processing of an XSLT stylesheet. Matching the root node of the input XML document instance and generating an output based on the rules found in the stylesheet create this tree. Once the entire stylesheet has been processed, the output the result tree is generated. Note that the result tree does not necessarily reflect the structure of the input, or source tree. The transformation process, using the rules in the stylesheet, may restructure the information prior to generating the result tree.

The output, or result tree, from an XSLT stylesheet is always a well-formed XML document, unless another output format like HTML or text is specifically selected. (See Chapter 10 for more information on selecting different kinds of output.)

2.3.4 Source Tree

The source tree is a representation of a well-formed XML document that is created during the parsing of the XML document. The source tree is created by the XSLT processor and stored in "memory" for reference during the processing of the XSLT stylesheet.

A valid source tree will always have a root node. The source tree is the entire XML document instance, but it is a somewhat abstracted roadmap of it, as shown with the book example in Figure 2-1.

Figure 2-1. Creating a source tree.

graphics/02fig01.gif

All nodes from the input XML document, including element nodes, attribute nodes, etc., are included in the source tree, which makes the representation of the structure somewhat unintuitive, as shown in Figure 2-2. This structure was explained in detail in Chapter 1.

Figure 2-2. Logical structure of a source tree.

graphics/02fig02.gif

2.3.5 Whitespace

Whitespace is simply space between characters, whether tabs, line returns, or simply spaces. In most cases, whitespace does not matter to the XML processor unless we tell it to pay attention to it. Sometimes whitespace can be added to make it easier to read a complexly coded XML document.

A good way to think of whitespace is in terms of HTML. An HTML browser automatically collapses (or removes) multiple spaces between words to a single space. Hard returns in elements are also collapsed into a single space, unless the line is tagged with a line break tag (<br>). XML works similarly with whitespace. In HTML, you can force every bit of whitespace to be attended to and rendered by using the preformatted, or <pre>, tag. In XML, you can do this by adding an attribute (xml:space='preserve') to an element or one of its ancestors.

In an XSLT stylesheet, whitespace is handled similarly to how it is handled in XML or HTML. For the most part, it is ignored. Within the quote marks surrounding the value for an attribute, whitespace is "compressed" or "condensed" the way it is in HTML, down to a default of one space. More spaces can be added with the &nbsp;, or nonbreaking space, entity. Space within the output result tree can be added or preserved with an XSLT instruction element, <xsl:text>, discussed in detail in Chapter 6. It serves a role analogous to the HTML <pre></pre> tags (though it does not affect font or style in that way, only preservation of whitespace).

In XSLT stylesheets, whitespace within an attribute value is condensed to a single space. Within and between template rules, whitespace has no effect on XSLT stylesheet processing. Within the <xsl:text> instruction element, whitespace is preserved.

2.3.6 Well-Formedness

Well-formedness is the fundamental syntactic integrity required of every XML document. Whether there is a pre-defined markup specification using a DTD or schema or not, every XML document must be well-formed. The same is true of XSLT stylesheets. All rules apply, from proper matching of lowercase characters to the closing tag required to match every opening tag, etc. In essence, XSLT stylesheets must be well-formed because they are XML document instances.

The proper nesting of elements actually makes understanding each part of the XSLT stylesheet easier, as shown in Example 2-5.

Example 2-5 Basic XSLT template showing nesting of XSLT and literal elements.
<xsl:template match="body">       <xsl:for-each select="p">             <i>                    <xsl:apply-templates />             </i>       </xsl:for-each> </xsl:template> 

Each tag that opens also closes, though in one case (<xsl:apply-templates>) the element is empty, so the closing of the tag is signified with the end "/" within the tag. The elements are also nested hierarchically within one another.

Even without knowing the full function of <xsl:for-each>, it is possible to see that something is going to be made italic using the HTML <i> tag. That something assuming these are standard HTML tags is a paragraph, selected by the <xsl:for-each> with the select="p" attribute. Not only one paragraph is selected, but each one (for-each) that may be found in the body (matched with match="body" on <xsl:template>) of the XML document instance.

Attribute values in XSLT elements must be contained in quote marks and the quote marks must match either two single quotes (' ') or two double quotes (" "). Both of the following are incorrect:

<season period="spring'> <season period=summer> 

Note

It is good practice to use double quotes for attribute values, as some constructs in XSLT use expressions inside of attribute values, and therefore must use single quotes within the double quotes to avoid confusion and parsing errors.

2.4 XML Components of XSLT Stylesheets

Explanations of comments and processing-instructions in general may be unnecessary for those of you familiar with their use in XML document instances. However, these components can also be accessed by XSLT stylesheets, or even be part of XSLT stylesheets, so their application in XSLT is important to understand. There are two concepts in particular that are worth reviewing, the XML declaration and the document type declaration.

2.4.1 The XML Declaration

One of the key components of any XML document instance is the XML declaration, which is a processing-instruction, or PI, that is always the first item in the document it even comes before the document element. The XML declaration identifies the contents of the document as XML and provides other relevant information like version and character encoding. This particular PI has a unique structure and a certain mandated content.

PIs in general are used by external applications to process the data in certain ways. PIs are empty elements that are delimited with the open tag marker <, followed by a ?, and the close tag marker >, preceded by another ?. The first word in a PI is the target of the instruction, or the software that is supposed to handle the instruction. The remaining content of the PI is the parameter string that the target software will need to complete the instruction. Example 2-6 shows a typical XML declaration, which is also a PI. The target of the PI in this case is xml, and the version="1.0" is a parameter that the XML processor understands.

Example 2-6 XML document with an XML declaration.
<?xml version="1.0"?> <year xmlns:iowa="http://www.iowa_climate.org/almanac/">       <iowa:planting>             <iowa:season period="spring">   ...and so on... 

It is important to note in the syntax above that there are no spaces before or after the leading and trailing ?. This is as important to the XSLT stylesheet as it is to any XML document instance, as the XSLT processor will not process the XSLT stylesheet if there are errors. The XML declaration must always include the version attribute, and can have an encoding attribute to pass information about the character set of the document (for instance, something other than the XML default UTF-8, such as UTF-16).

The XML declaration can also tell you whether the document has dependencies with the standalone attribute. If there are other documents that go with the XML instance, the document cannot be "standalone."

2.4.2 The Document Type Declaration

XML provides a special mechanism to associate a DTD with an XML document, called a Document Type Declaration (also known as a DOCTYPE declaration). XSLT processors are not required to read a DTD; however, if a DTD is specified using a document type declaration, most XSLT processors will read and process the XML document according to the DTD. The DOCTYPE declaration begins with the open tag delimiter, followed by a "!," and the key word DOCTYPE, as shown in Example 2-7 .

The DOCTYPE declaration tells in specific syntactic order what the document element is (year, in this case), whether the DTD is publicly declared or if it is found on the file system (SYSTEM), and where the DTD is located, using a specific address for the file (c:\dtds\calendar.dtd).[2]

Example 2-7 XML document with DOCTYPE declaration.
<?xml version="1.0"?> <!DOCTYPE year SYSTEM "c:\dtds\calendar.dtd"> <year xmlns:iowa="http://www.iowa_climate.org/almanac/">       <iowa:planting>              <iowa:season period="spring">   ...and so on... 

The DOCTYPE declaration, if used, must always come before the document element.

Note

The DOCTYPE declaration can point to a DTD on a file system, or alternatively contain the DTD as part of the content of the declaration. It can also be a combination of both, allowing additions to the DTD that are specific to the document being processed.

XSLT lets you perform transformations upon and manipulate the DOCTYPE statement in addition to all the component nodes that are descendants of the document element in the logical structure of the markup. Therefore, it is important to know that the root contains the declaration, the document element does not. Otherwise, it would be very hard to access the children of the root in an XML document instance the XML declaration, the DOCTYPE declaration (if there is one), and the document element with an XSLT stylesheet.

[1] See http://www.w3.org/TR/xml-stylesheet.

[2] The location of the DTD can also be just the DTDs filename in quotes if the DTD is located in the same directory as the document instance.

CONTENTS


XSLT and XPATH(c) A Guide to XML Transformations
XSLT and XPATH: A Guide to XML Transformations
ISBN: 0130404462
EAN: 2147483647
Year: 2005
Pages: 18

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