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:dtdscalendar.dtd).[2]

[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.

Example 2-7. XML document with DOCTYPE declaration.
<?xml version="1.0"?>
<!DOCTYPE year SYSTEM "c:dtdscalendar.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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset