WebReference.com - Chapter 7 of XML for ASP.NET Developers, from Sams Publishing (4/13)
[previous] [next] |
XML for ASP.NET Developers
The XSLT Language
Now that you've seen the transformation process and have been introduced to what an XSLT document looks like, let's break the different parts used in the document into individual pieces. First up: the XSLT document root element.
The XSLT Document Root Element
Looking back at Listing 7.2, you'll notice that the document follows all the rules specified in the XML specification described in Chapter 2, "XML for ASP.NET Basics." The case of each opening tag matches the case of the closing tag, all attributes are quoted, all tags are closed, and so on. XSLT documents are, in fact, well-formed XML documents. As a result, the first line of each document should contain the XML declaration. Although this line is optional, it's essential that you get into the practice of using it, especially because new versions of the XML specification will certainly be coming in the future.
Following the XML declaration, one of two elements specific to the XSLT language can be used for the document's root node. These elements are the following:
<xsl:stylesheet>
-
<xsl:transform>
Although you can use either element as the root of an XSLT document, the
samples that follow throughout this chapter use the xsl:stylesheet
element. You can certainly substitute the xsl:transform
element instead
if you feel more comfortable using it.
Two different items must also be included for an XSLT document to follow the
guidelines found in the XSLT specification. These are a local namespace
declaration as well as an attribute named version
. The inclusion of the
xsl:stylesheet
element, the namespace declaration, and the
version
attribute are shown next:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="https://www.w3.org/1999/XSL/Transform"
version="1.0"
>
<!-- The bulk of the XSLT document goes here -->
</xsl:stylesheet>
The namespace URI
(https://www.w3.org/1999/XSL/Transform)
must be listed exactly as shown, and the version
attribute must have a
value of 1.0
for the document to be conformant with the November 1999
XSLT specification. As different versions of the specification are released,
this version number can be changed, depending on what features the XSLT document
uses. Failure to list these parts correctly will result in an error being
returned by the XSLT processor.
Note
XSLT version 1.1 was in Working Draft at the time this section was written. XSLT style sheets that use new features found in this version will need to let the XSLT processor know by changing the version attribute to 1.1.
XSLT Elements
If you've had the opportunity to work with HTML in the past, you're
already aware of how elements are used to perform specific tasks. For example,
the <table>
element can be used along with the
<tr>
and <td>
elements to construct a table for
display in a browser. The <img>
element can be used when an image
needs to be displayed, and the <form>
element can be used as a
container for different form elements such as text boxes and radio buttons. Each
of these elements have a specific purpose and when appropriate, can contain
supporting child elements.
The XSLT version 1.0 specification lists several elements that can be used to
transform XML documents. These elements can be used in a variety of ways,
including determining the output format, performing if/then type logic, looping,
and writing out data within a node contained in the XML document to the result
tree structure. An XSLT element is distinguished from other elements that may be
within an XSLT document by its association with a namespace that defines a URI
of
https://www.w3.org/1999/XSL/Transform.
Declaring this namespace on the XSLT root element (xsl:stylesheet
or
xsl:transform
) was shown in the previous section.
Table 7.1 contains a listing of all potential elements in version 1.0 of the
XSLT specification. Notice that each element is prefixed by the xsl
namespace.
Table 7.1 XSLT Elements
XSLT Element |
Description |
|
Used in conjunction with imported style sheets to override templates within
the source style sheet. Calls to |
|
When |
|
Creates an attribute node that is attached to an element that appears in the output structure. |
|
Used when a commonly defined set of attributes will be applied to different elements in the style sheet. This is similar to named styles in CSS. |
|
Used when processing is directed to a specific template. The template is identified by name. |
|
Used along with the |
|
Writes a comment to the output structure. |
|
Copies the current node from the source document to the result tree. The current node's children are not copied. |
|
Used to copy a result-tree fragment or node-set into the result tree. This performs a "deep copy," meaning that all descendants of the current node are copied to the result tree. |
|
Declares a decimal-format that is used when converting numbers into strings
with the |
|
Creates an element with the specified name in the output structure. |
|
Provides an alternative (or fallback) template when specific functionality is not supported by the XSLT processor being used for the transformation. This element provides greater flexibility during transformations as new XSLT versions come out in the future. |
|
Iterates over nodes in a selected node-set and applies a template repeatedly. |
|
Used to wrap a template body that will be used only when the |
|
Allows an external XSLT style sheet to be imported into the current style sheet. The XSLT processor will give a lower precedence to imported templates as compared to templates in the original XSLT style sheet. |
|
Allows for the inclusion of another XSLT style sheet into the current style sheet. The XSLT processor gives the same precedence to the included templates as templates in the original XSLT style sheet. |
|
Declares a named key and is used in conjunction with the |
|
Used to output a text message and optionally terminate style sheet execution. |
|
Used to map a prefix associated with a given namespace to another prefix. This can be useful when a style sheet generates another style sheet. |
|
Used to format a number before adding it to the result tree or to provide a sequential number to the current node. |
|
Used with the |
|
Specifies options for use in serializing the result tree. |
|
Used to declare a parameter with a local or global scope. Local parameters are scoped to the template in which they are declared. |
|
Preserves whitespace in a document. Works in conjunction with the
|
|
Writes a processing instruction to the result tree. |
|
Used with |
|
Causes whitespace to be stripped from a document. Works in conjunction with
the |
|
This element must be the outermost element in an XSLT document and must contain a namespace associated with the XSLT specification and a version attribute. |
|
Defines a reusable template for producing output for nodes that match a particular pattern. |
|
Writes out the specified text to the result tree. |
|
Used in the same manner as the |
|
Writes out the value of the selected node to the result tree. |
|
Used to declare and assign variable values that can be either local or global in scope. |
|
Used as a child element of |
|
Used in passing a parameter to a template that is called via
|
Although not every element listed in Table 7.1 is discussed in this section, you will be exposed to the more common elements and see how they can be used to transform XML into formats such as HTML, WML, and even EDI.
Note
The HTML generated by an XSLT document must conform to the rules outlined in the XML specification. All elements must be closed, including
<img>
,<input>
, and all the other elements that normally do not need to be closed in HTML. Attributes used on elements must be quoted, a beginning and ending element tag's case must match, and so on. Keep in mind that the XSLT processor knows how to work only with well-formed XML and knows nothing about the tags used in HTML, WML, and so on. As a result, everything within the XSLT document must follow the XML rules.
[previous] [next] |
© Copyright Pearson Education and
Created: April 22, 2002
Revised: April 22, 2002
URL: https://webreference.com/authoring/languages/xml/aspnet/chap7/4.html