|
Home > Online Product Documentation > Table of Contents > Getting Started with XSLT Getting Started with XSLTThis section provides an introduction to using Extensible Stylesheet Language Transformations (XSLT). It discusses the following topics: What Is XSLT?The Extensible Stylesheet Language (XSL) is the World Wide Web Consortium's (W3C) language for manipulating XML data. XSLT is the component of XSL that allows you to write a stylesheet that you can apply to XML documents. The result of applying a stylesheet is that the XSLT processor creates a new XML, HTML, or text document based on the source document. The XSLT processor follows the instructions in the stylesheet. The instructions can copy, omit, and reorganize data in the source document, as well as add new data. XSL is an XML-based language. It was developed by the W3C XSL working group within the W3C Stylesheets Activity. The W3C activity group has organized its specification of XSL into three parts:
What Is a Stylesheet?A stylesheet is an XML document that contains instructions for generating a new document based on information in the source document. This can involve adding, removing, or rearranging nodes, as well as presenting the nodes in a new way. This following topics provide more information: Example of a StylesheetWhen you work with a stylesheet, three documents are involved: Source XML documentFor example, suppose you have the following XML document:
Resulting HTML fileYou can use a stylesheet to transform this XML document into an HTML document that appears as follows in a Web browser:
The Web page in Figure 189 is defined by the following HTML document:
The HTML document contains HTML markup that is not in the source document. In the HTML document, the data from the source document is not in the same order as it is in the XML source document. Also, this HTML document does not include some data that is in the XML source document. Specifically, the HTML document does not include information about the date of publication (the
Stylesheet usedTo create this HTML file, the stylesheet contains two templates that provide instructions for Following is a stylesheet that does this.
About Stylesheet ContentsStylesheets are XML documents. They contain a combination of
Each XSLT element is an instruction to the XSLT processor. For information about all XSLT instructions, see XSLT Instructions Quick Reference. The Root element
The root element of a stylesheet must declare a namespace that associates a prefix with the URI for an XSLT processor. The URI in the namespace declaration in the previous example identifies the W3C standard XSLT processor. This declaration, shown again below, instructs the XSLT processor to recognize the XSLT elements and attributes by their
In this stylesheet, you must use the
When you write a stylesheet, you specify the actions you want the XSLT processor to perform when it processes a particular source node. To do this, you define XSLT templates, which are described in the next section. What Is a Template?A template defines what the XSLT processor should do when it processes a particular node in the XML source document. The XSLT processor populates the result document by instantiating a sequence of templates. Instantiation of a template means that the XSLT processor The following topics further describe what a template is: Contents of a Template
The
stylesheet example in
Stylesheet used defines the following templates using the
In the
The template body defines actions you want the XSLT processor to perform each time it instantiates this template. It contains Determining Which Template to Instantiate
When the XSLT processor applies a stylesheet to an XML document, it begins processing with the root node of the XML source document. To process the root node, the XSLT processor searches the stylesheet for a template rule that matches the root node. A template rule matches the root node when the value of the template's
If you explicitly defined a template rule that matches the root node, the XSLT processor finds it and instantiates its template. If the XSLT processor does not find an explicitly defined template rule that matches the root node, the processor instantiates the default template that matches the root node. Every stylesheet includes this default template.
In the sample stylesheet, the template rule in the first template matches the root node:
The XSLT processor instantiates this template to start generating the result document. It copies the first few lines from the template to the result document. Then the XSLT processor reaches the following XSLT instruction:
When the XSLT processor reaches the
After instantiating this template for the first
After three instantiations of the
How the select and match Attributes Are DifferentConsider the following instructions:
The
When the XSLT processor reaches an expression that is the value of a
When the XSLT processor reaches a pattern that is the value of a
For example, suppose you have the following instruction:
This instruction selects the
This example shows that the expression that the XSLT processor uses to select nodes and the pattern it uses to match nodes are independent of each other. How the XSLT Processor Applies a StylesheetWhen the XSLT processor applies a stylesheet, it starts by automatically selecting the root node for processing and then searching for a template that matches the root node. The XSLT processor then iterates through the process of instantiating templates, selecting nodes in the source document for processing, and matching patterns, until no more templates need to be instantiated. This section uses the sample stylesheet on Stylesheet used to present this process in more detail in the following topics: Instantiating the First TemplateTo apply a stylesheet, the XSLT processor searches for a template that matches the source document root. The XSLT processor then instantiates the matching template and begins to process it line by line. The specific processing depends on the contents of the template that matches the root node. The parts of the template include It is important to understand that the contents of the XML source document do not dictate the order of XSLT processing. The XSLT processor performs only those actions that you specify, and operates on only the source nodes that you select. For example:
This template matches the root node. Consequently, the XSLT processor begins processing by instantiating this template. This means it processes each part of the template in the order in which it appears.
In the preceding example, the XSLT processor first copies the first four lines in the template body directly into the result document. Then it executes the
Selecting Source Nodes to Operate OnAside from the root node, the XSLT processor operates on only those nodes in the source document that are selected as the result of executing an XSLT instruction. In a stylesheet, there are two XSLT instructions that select nodes in the source document for processing:
The value of the
In an
When the XSLT processor reaches an
In an
Controlling the Order of Operation
Typically, the template that matches the root node includes an
1. The processor evaluates the expression specified for the
xsl:apply-templates
select attribute to create a list of the source nodes identified by the expression.
2. For each node in the list, the XSLT processor instantiates the best matching template. (Template properties such as priority and mode allow multiple templates to match the same node.)
3. The processor returns to the template that contains the
xsl:apply-templates instruction and continues processing that template at the next line.
It is important to note that in step 2, the matching template might itself contain one or more
Example
The
sample template on
Instantiating the First Template contains the following
The
1. The XSLT processor searches the stylesheet for a template that matches
"book".
2. When the XSLT processor finds the template that matches the
book element, it instantiates it. The following template matches the
book elements selected by the
xsl:apply-templates instruction:
3. The XSLT processor creates an HTML table row and executes the
xsl:value-of instructions. These instructions insert the values for the matching book's
title,
author, and
price elements into the table.
The XSLT processor repeats this process for each
It is important to note that the XSLT processor does not search for a matching template once and then instantiate that matching template for each selected element. Rather, the XSLT processor performs the search for a matching template for each node selected for processing. For each node selected for processing, the XSLT processor
Another way to control the order of operation is to specify the
Omitting Source Data from the Result DocumentThe XSLT processor operates on only those nodes that you specify. If a node in your XML source document is never referenced in a stylesheet, the XSLT processor never does anything with it. For example, the sample source XML document on Source XML document includes more than the title, author, and price for each book. It also includes the year of publication:
However, the template that matches the
When More Than One Template Is a Match
Sometimes, more than one template matches the node selected by an
For information on specifying these attributes, see xsl:template and xsl:apply-templates. When No Templates MatchWhen the XSLT processor cannot find a template that matches a selected node, it uses built-in templates. Every stylesheet includes built-in templates whether or not you explicitly define them. The XSLT processor supports these built-in templates: Although Stylus Studio does not explicitly insert these templates in stylesheets you create with Stylus Studio, they are always present. That is, as specified by the W3C XSLT Recommendation, these templates are always defined, whether or not they are explicitly defined. See Using Stylus Studio Default Templates. Controlling the Contents of the Result DocumentThis section highlights some of the XSLT instructions you can specify in a stylesheet to control the contents of the result document. This section discusses the following topics: Specifying Result FormattingIn a stylesheet, you can specify that the XSLT processor should format the result as XML, HTML, or text. Table 29 describes the XSLT processor output for each alternative:
See xsl:output for information about specifying formatting in a stylesheet. Creating New Nodes in the Result DocumentThe simplest way to create new nodes in a result document is to specify them as literal result elements or literal result text in a stylesheet template. For example:
This template creates many nodes in the result document that were not in the source document. You can also use XSLT instructions to create new nodes. Typically, you use XSLT instructions when you need to compute the name or value of the node. You can find information about using the following instructions in the XSLT Instructions Quick Reference: You can use the xsl:value-of instruction to provide the contents for a new node. You can also create a new node by copying the current node from the source document to the result document. The current node is the node for which the XSLT processor instantiates a template. See xsl:copy. Controlling White Space in the ResultFor readability, XML documents (both source documents and stylesheets) often include extra white space. White space in XML documents includes spaces, tabs, and new-line characters. Because this white space is for readability, it receives special treatment. Text nodes that contain only white space are Significant white space
Stylus Studio recommends that you specify
To obtain white space for readability during output formatting, specify the
Specifying XSLT Patterns and Expressions
In a stylesheet's
Depending on the context, an XSLT pattern or expression can mean one of the following: Patterns or expressions can match or select any type of node. The XSLT processor can match a pattern to a node based on the existence of the node, the name of the node, or the value of the node. You can combine patterns and expressions with Boolean operators. For detailed information about patterns and expressions, see Chapter 9Writing XPath Expressions. Examples of Patterns and ExpressionsFollowing are examples of patterns and expressions you can specify in stylesheet instructions:
Matches any
Matches any
Matches any
Matches any
Matches any
Matches any
Selects all
Selects all
Selects all
Selects all
Frequently Asked Questions About XSLTHow can I use quoted strings inside an attribute value?
If you need to include a quoted string inside an attribute value (in a
How do I choose when to use xsl:for-each and when to use xsl:apply-templates?
The way
With
Finding a template by matching requires more time than using the contained template. However, matching allows for more flexibility. Also, matching lets you avoid repeating templates that might be used in more than one place in a stylesheet.
Named templates are another option for invoking a template from more than one place in a stylesheet, when you know which template you want. It is a common mistake to use (and bear the overhead of) matching when it is not needed. But it allows you to do powerful things. Matching can take into account the following:
Most complex document-formatting stylesheets use
How can I insert JavaScript in my result document? If you want your result document to contain JavaScript commands, you must properly escape the JavaScript code. Use the following format in your XSLT template:
However, this method does not work when your JavaScript section contains a block of XSLT code. In this case, enclosing the JavaScript in a
In this situation, enclose the entity reference within an
You can use this wherever an entity reference needs to be handled specifically, as opposed to being handled as part of an entire JavaScript section. My browser does not understand the tag <br/>. How can I output just <br>? Although your XSLT stylesheet must contain valid XML (meaning all tags must be either empty or have a closing element), you can instruct the XSLT processor to generate output compliant with HTML. See Deleting Templates.
Alternative: To ensure that your stylesheet always generates correct HTML, specify the
Sources for Additional XSLT InformationFor additional information about XSL and XSLT, visit the following Web sites: Benefits of Using Stylus StudioNow that you have an understanding of what a stylesheet can do, you can appreciate the benefits of using Stylus Studio to create stylesheets. Stylus Studio is the first integrated environment for creating, managing, and maintaining an XSL-enabled Web presence. By combining the tools needed to create XSLT stylesheets in a visual editing environment, Stylus Studio speeds initial development and eases maintenance. Key features of Stylus Studio include Structural Data ViewStylus Studio graphically displays the structure, or schema, of the XML data to which you want to apply a stylesheet.
Using this tree view, you can apply formatting to your XML - double-clicking a node in the tree automatically adds an
Finally, you can also use the tree to move quickly among different XSLT templates - clicking a node in the tree places the cursor at the corresponding template in the XSLT source. Sophisticated Editing EnvironmentThe Stylus Studio editor allows you to edit both the XML source document and the XSLT stylesheet. There is no need to memorize complicated syntax. As you type, Stylus Studio Sense:X technology automatically suggests XSLT or HTML tag and attribute names, and ensures that all XML is well formed.
Sense:X also adapts to your document by suggesting more frequently used tags first. Valid XSLT and HTML tag names are color coded to improve readability. XSLT and Java Debugging FeaturesComplex stylesheets require robust debugging features. With Stylus Studio, you can do the following:
Also, you can click in the stylesheet and the backmapping feature highlights the text generated by that template.
Integrated XML Parser/XSLT ProcessorStylus Studio integrates an XML parser with an XSLT processor. This allows Stylus Studio to instantly show the output of your stylesheet. Each time you apply a stylesheet to an XML document, Stylus Studio detects and flags any errors in your stylesheet or XML data. Stylus Studio's default XSLT processor is compliant with the W3C XSLT Recommendation. You can also use the Xalan-J processor, or custom processors of your own. |

Cart


