xsl

23
6 Copyright © 2004, Oracle. All rights reserved. Transforming XML by Using XSL Transformations

Upload: krishna

Post on 13-Sep-2015

212 views

Category:

Documents


0 download

DESCRIPTION

XSL

TRANSCRIPT

PowerPoint Presentation6-*
Objectives
After completing this lesson, you should be able to do the following:
Describe XSL and XSL Transformations (XSLT)
Transform an XML document by using XSLT
Use key XSLT elements
Sort and filter an XML document
Use parameters with templates
Objectives
In this lesson, you learn about Extensible Stylesheet Language (XSL), and its rules and syntax for transforming XML documents.
References:
http://www.topxml.com/xsl/XSLTRef.asp
XSLT, Doug Tidwell, O’Reilly Press
6-*
What Is XSL?
Extensible Stylesheet Language (XSL) is made up of two parts:
XSL Transformations (XSLT)
What Is XSL?
XML documents have structure and, unlike HTML, do not provide formatting or display instructions. Extensible Stylesheet Language (XSL) adds the capability of transforming an XML document into another document containing XML with a different structure, or an HTML document with formatting tags.
XSL has the following two parts:
XSL Transformations (XSLT) is an XML application that processes rules contained in an XSLT stylesheet. The rules in an XSLT stylesheet can be applied to any XML document.
The XSL Formatting Objects (XSL-FO) is an XML application used for describing the precise layout of text on a page. XSLT is used to generate XSL-FO elements that are output to another XML document. Additional processing is needed to create the final output form such as a portable document format (PDF) document.
This lesson focuses on XSLT.
The graphic in the slide depicts an XML document and an XSLT stylesheet document being processed by an XSL processor. The XSL processor is an XML application that applies the transformation rules to the XML document as specified in the XSL document and produces a new document as the result.
6-*
XSL Transformations
Output data
XSL Transformations
XSL Transformations (XSLT) is an XML application, or XSL Processor, that processes rules specifying how to transform one XML document into another XML document. The rules are contained in an XSLT document called an XSLT stylesheet, which itself is an XML document and must be well-formed.
The slide shows that an XSLT stylesheet contains one or more template rules; each template rule has two parts:
A match pattern, specified as an XPath expression in an attribute of an <xsl:template> element
Template data, appearing between the start and end tags of the <xsl:template> element, that contains the output information. The template data typically contains XML/HTML elements mixed with the XSLT rules.
The XSL processor compares the elements in the input XML document with the template rule pattern in the XSLT stylesheet, and writes the template data for matching rules to the output tree, typically, another XML document. Essentially, the transformation process operates on the tree data structure of a source (input) XML document and produces a tree of nodes as its output.
Note: XSL-FO elements are output elements found inside the <xsl:template> rules.
6-*
The XSLT Stylesheet
A <xsl:stylesheet> root element declaring:
The xsl namespace prefix
The http://www.w3.org/1999/XSL/Transform mandatory namespace URI
One or more <xsl:template> elements and other XSL elements defining transformation rules
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
The XSLT Stylesheet
An XSLT stylesheet is an XML document that uses elements from the XSLT vocabulary to describe transformation rules. The document element of every XSLT stylesheet is the <xsl:stylesheet> element, whose content is a set of one or more XSL elements defining template rules describing the transformation to be performed.
Note: <xsl:transform> is a synonym for <xsl:stylesheet>.
The <xsl:stylesheet> element declares the mandatory namespace attribute xmlns:xsl="http://www.w3.org/1999/XSL/Transform" used to qualify the elements in an XSLT stylesheet. If you do not provide this exact namespace prefix and URI, the XSLT Processor simply ignores the rules in <xsl:template>, <xsl:for-each>, <xsl:value-of>, and other XSL elements that are qualified with the xsl prefix. Therefore, it does not recognize them as XSLT instructions.
Each stylesheet rule, called a template, contains a match pattern specified as an XPath expression that is compared against the nodes in the source XML document. An XSL template rule is represented by an <xsl:template> element with a match attribute that is assigned a "pattern" string containing an XPath expression.
6-*
XSLT Stylesheet: Example
<?xml version="1.0"?>
<xsl:stylesheet version ="1.0"
<table border="1">
<tr><th>Id</th><th>Name</th><th>Salary</th></tr>
<xsl:apply-templates/>
</table>
</body>
</html>
</xsl:template>
XSLT Stylesheet: Example
The XSLT document in the example, called a stylesheet, shows the following components:
1. The standard XML document declaration, because the stylesheet is an XML document
2. Mandatory XML Namespace declaration using the xsl namespace prefix, and the URI http://www.w3.org/1999/XSL/Transform namespace
3. The <xsl:template> rule matching the root of the source XML document
4. The template data containing HTML tags to be written to the output document
5. The <xsl:apply-templates> element instructing the XSLT Processor to apply template rules, if any, for child elements of root in the source document. The <xsl:apply-templates> element is replaced by any output generated for matching child element template rules.
6. The <xsl:template> rule for matching <employee> elements in the source document, with template data containing HTML table row (<tr>...</tr>) tags enclosing three table data (<td>...</td>) tag pairs. Each table data tag pair encloses an <xsl:value-of select="..."/> rule that inserts the value of the source document element selected by the XPath expression, which is typically the name of the source document element.
The XML document that uses this XSLT stylesheet is shown in the page titled “Viewing the Transformed Document.”
6-*
Using an XSLT Stylesheet with an XML Document
An XML document uses an <?xml-stylesheet...?> processing instruction:
After the XML declaration and before the root element of the XML document.
Containing two pseudo-attributes:
The type value is the MIME type for the stylesheet
The href value is the URL of the source XSLT stylesheet document
<?xml version="1.0"?>
<employees>
Using an XSLT Stylesheet with an XML Document
An XSLT stylesheet is applied to an XML document by adding the xml-stylesheet processing instruction in the XML document prologue that is before the document root element. The xml-stylesheet processing instruction provides two pseudoattributes:
The type value is the MIME media type for the type of stylesheet. For XSL stylesheets, the type value must either be application/xml or text/xml. However, the XSL processor in Internet Explorer does not recognize application/xml as the type value but accepts a value of text/xsl, as shown in the example in the slide. A MIME media type value of text/css is applicable to a Cascading Style Sheet (CSS).
The href value is a URI string referencing the name of the stylesheet document.
The xml-stylesheet processing instruction is required for a browser to invoke its XSLT Processor to apply the stylesheet to the XML document tree.
Note: The type pseudoattribute value of text/xsl, which is a fabrication of Microsoft, is not a formally registered MIME type. However, this course uses the text/xsl MIME type to make it easier to observe the results of XSLT. The processing instruction is not required by the Oracle command-line XSLT Processor. Therefore, the MIME type used is irrelevant to the Oracle command-line XSLT Processor.
6-*
Viewing the Transformed Document
Open the XML document in the browser
View oraxsl command-line processor output
<?xml version="1.0"?>
<employees>
<employee>
Viewing the Transformed Document
The slide shows the source XML document and the result of applying the XSLT stylesheet from the section titled “XSLT Stylesheet: Example.” When you open the XML document with Microsoft Internet Explorer, it automatically applies the XSLT stylesheet specified in the XML document processing instruction and renders the result shown.
Alternatively, you can use the oraxsl command-line interface to apply an XSLT stylesheet to an XML document. To invoke oraxsl use:
set CLASSPATH=E:\JDeveloper\lib\xmlparserv2.jar
java oracle.xml.parser.v2.oraxsl f.xml f.xsl f.html
The oraxsl parameters in order are:
1. The source XML document (f.xml)
2. The XSLT stylesheet document (f.xsl)
3. The output file name (f.html). If omitted, the output appears on the standard output.
Note: If the result file contains HTML, then you can view it in a browser. The Oracle 10g version of the oraxsl utility is configured as an External Tool in JDeveloper because it supports some XSLT 2.0 features. The oraxsl utility is discussed in the section titled “Using the oraxsl Utility.”
6-*
Creating Template Rules
The template rule:
A match attribute with an XPath pattern value
The output template containing formatting instructions to produce a result tree
Is applied when a node in the input XML document matches the XPath pattern in the rule
Match pattern determines its context node
<xsl:template match="pattern">
Creating Template Rules
Template rules determine what the XSLT Processor will output when an input node matches the template. Each template rule is defined by the <xsl:template> element. The <xsl:template> element contains:
A match attribute that specifies the rule pattern, as an XPath expression, to recognize nodes from the input XML document
The output template between the start and end <xsl:template> tags. The output template contains instructions for formatting the result document tree. The output template can be empty to suppress the output for a node, or include any, or a combination, of text, XML, HTML, and XSLT elements with additional instructions for the XSLT Processor, and XSL-FO elements.
The example in the slide transforms the entire XML input document into a simple text string with the template rule matching the document root, and the output document will contain the text: A simple text string. The rule in the slide processes the root element and not any other nodes from the input document. To process the child nodes of the document root, its template rule must include an <xsl:apply-templates/> rule.
The general principle used by an XSLT Processor is that when a template rule matches an input node, the output template instructions are copied to the resulting output tree.
Note: The matching node becomes the context node for the template rule.
6-*
Getting Input Text with <xsl:value-of>
The <xsl:value-of> element:
Has the select attribute containing an expression, typically an XPath expression
Inserts the string (text) value of the expression
Is used inside an <xsl:template> element
For example:
Getting Input Text with <xsl:value-of>
Most of the time, you want to insert the content from the input document into the output results, unlike some of the previous examples that replace the input with a literal string. The <xsl:value-of> element inserts into the output tree a string or text value calculated from the XPath expression specified in the select attribute. For example, to output:
Element text: <xsl:value-of select="name"/> produces Europe
Attribute values: <xsl:value-of select="@num"/> produces a 1
Numeric or Boolean expressions:
<xsl:value-of select="boolean(id)"/> produces the string true
The <xsl:value-of> element syntax is:
<xsl:value-of select=“expression”
disable-output-escaping=“yes|no”/>
The example in the slide uses the current node (.) XPath expression in the template matching nodes with the name region from the input XML document. The result written to the output document contains the concatenated text value of the <region> element and its children. The disable-output-escaping attribute values are:
yes that outputs an & character for an & in the input, and > for an <.
no (the default), that outputs & and < as themselves, if in the input text
6-*
Applying Template Rules
<xsl:template match="/">
Document root
Applying Template Rules
As discussed, each rule is called a template because the literal text, elements, and attributes inside the body of the rule act as a blueprint for constructing a part of the result tree. The XSLT Processor constructs the content of a rule’s template in the result tree whenever it processes a source node matching the rule’s pattern.
To begin, the XSLT Processor reads the source document from top to bottom, starting at the document root, working down to the children using a preorder traversal. Templates are activated in the order in which they match elements encountered during the traversal. Therefore, a template for a parent node is activated before a template matching its children. However, to activate templates for child elements, the <xsl:apply-templates/> XSLT element must be included in the template of the parent node.
Note: The <xsl:apply-templates/> element is an instruction to the XSLT Processor to recursively process all the children of the context node. Without this rule, child elements may not be processed unless the default template rule is invoked. Default template rules are discussed in the section titled “Default Template Rules.”
Note: The data output from child node templates is inserted into the location where the <xsl:apply-templates/> appears.
6-*
Controlling Template Activation Order
Templates matching child nodes by default
Templates matching its optional select attribute
Example:
<xsl:stylesheet version="1.0"
Controlling Template Activation Order
As discussed previously, the default behavior of <xsl:apply-templates> is to process the child nodes of the context node. Alternatively, you can add a select attribute to the <xsl:apply-templates/> element to control the specific template to be activated relative to the context node.
The example in the slide makes uses of the select attribute in the <xsl:apply-templates/> element. The select attribute specifies an expression to activate a template rule whose expression, in the match attribute, is activated for the same elements.
Note: The select attribute of the <xsl:apply-templates/> is typically the same as, but does not have to be identical to, the match attribute in the activated template rule. However, the two expressions must identify the same node-set for the correct template rule to be activated.
The results of the transformation contains a line of text for each region ID and name, excluding the Regions header, because the template matching regions is not activated.
If the select="//region" is absent from the <xsl:apply-templates/> in the template rule for the document root, then the transformation will include a level one heading with the title Regions before the lines for each region ID and name pairs.
6-*
Template Rules and Priorities
The rule with the highest priority is applied.
Or, use the rule that appears last if the priority equals another.
<?xml version="1.0"?>
<xsl:stylesheet version ="1.0"
</xsl:template>
</xsl:template>
</xsl:stylesheet>
Multiple Template Rules
An XSLT stylesheet normally has multiple template rules declared. As shown in the slide, the match pattern for more than one template rule can be applicable to the same node. To deal with this potential conflict, an XSLT Processor assigns a default priority to each template rule, unless the author of the XSLT stylesheet explicitly specifies a priority attribute in the start tag of the <xsl:template> declaration. The default priority is:
A -0.5 value, if the match pattern is a simple node-test, such as the element name
A 0 value, if the pattern uses a node name qualified by a namespace prefix, or the processing-instruction() function, for example, dept:department
A 0.25 value for patterns using a namespace prefix with a wildcard node-test, for example, dept:*
A 0.5 value for all other patterns, for example, /departments, which uses an absolute XPath pattern
Note: If more than one template rule has a pattern that matches the same node, then the rule with the highest priority is applied. If multiple patterns share the highest priority, then an error occurs, or the rule appearing last in their order of appearance is used. For example, the rule for pattern department containing the text literal “I WIN!” in the slide is selected because it appears last even though it has the same priority as the previous one.
6-*
Default Template Rules
XSLT provides built-in template rules:
That are applied for nodes without a matching template rule in the XSLT stylesheet
For different types of nodes in an XML document, such as:
The element and root nodes:
The text and attribute nodes:
<xsl:template match="*|/">
<?xml version="1.0"?>
<xsl:stylesheet version ="1.0"
</xsl:stylesheet>
An empty XSLT stylesheet creates an output document that contains the text values for all the nodes in the source document. This occurs because most XSLT processors implement default template rules called built-in templates. The slide shows the first built-in template rule matching the document root (/) and all element nodes (*) of the source document tree. The second template rule matches all the text nodes (text()) and attribute nodes (@*).
The XSLT Processor uses the first built-in template rule to process the document root and all its descendant nodes because of the presence of the <xsl:apply–templates/> element. When a text (or attribute) node is encountered, the second template invokes the <xsl:value-of> element to output its string value.
The resulting output document contains all the text values from the input document.
Note: Explicit template rules added to the XSLT stylesheet override the default built-in rules. However, nodes without explicit template rules can match the default template rules, which often cause unwanted text to appear in the output document.
6-*
Effects of Default Template Rules
<?xml version="1.0"?>
<xsl:stylesheet version ="1.0"
<table border="1">
<tr><th>Id</th><th>Name</th><th>Salary</th></tr>
<xsl:apply-templates/>
</table>
</body>
</html>
</xsl:template>
Effects of Default Template Rules
To illustrate the effects of default template rules, the example assumes that the document root contains the employees root element with one or more employee children. The XSLT stylesheet declares template rules matching the document root and employee nodes, but not the employees element. In this case, the processing steps are the following:
1. The document root template outputs the HTML table header row and invokes the <xsl:apply-templates/> element inside the HTML table after the header row.
2. The default template is invoked for the employees root element, which does not have an explicit template rule, as seen by the dotted arrows. The default template outputs white space and invokes the <xsl:apply-templates/> element to process the employee child elements.
3. Each employee node matches its template rule that outputs the text value for the employee_id, last_name, and salary in the cells of a HTML table row.
Note: After the source document and stylesheet has been constructed and before it is processed by an XSLT Processor, the text nodes are stripped if it contains only white space characters. Stripping a text node removes it from the tree. The XSLT stylesheet can specify:
The <xsl:strip-space elements="tokens"/> element to define a space-separated list of elements for which the spaces are stripped
The <xsl:preserve-space elements="tokens"/> element to define a list of space-separated elements in which the spaces are preserved
6-*
Looping with <xsl:for-each>
The <xsl:for-each> element:
Provides a select attribute containing an expression to identify a node-set
Applies its template for each node in the node-set
Is nested inside an <xsl:template> element
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
Looping with <xsl:for-each>
The <xsl:for-each> element iterates through all the nodes in the node-set matching the expression specified in its select attribute. The template rule is applied once for each node in the node-set. The XSLT stylesheet in the slide can be applied to the following XML:
<?xml version="1.0"?>
When applying the XSLT stylesheet, the processing steps are:
1. The default template rule matches the document root and executes an <xsl:apply-templates/> causing the departments root element template to be activated.
2. The departments root element template rule is activated with the <xsl:for-each> element matching all the department child elements.
3. The <xsl:for-each> element is applied for each department node.
The resulting output document contains one line for each department element, with the text values of its child elements concatenated, for example:
<p>10 Administration</p> ...
Attribute Value Templates
An attribute value template is:
An XPath expression, enclosed in braces, whose result is used as an attribute value
Used to include input document information in the attribute values of XSLT elements, and the output
<region>
</xsl:template>
1
2
3
Attribute Value Templates
The attribute value template is a powerful technique to include node names (element and attribute names) or their values in the attribute values of XSLT elements in the stylesheet. Using the attribute value template creates dynamic output results determined by information from the input document, such as attribute values for elements in the result document, as shown in the example in the slide.You can use the <xsl:element> element in the template to create output elements based on the element names from the input document, as discussed in the section titled “Creating Elements with Attributes.”
The example in the slide shows three related documents:
1. Part of an input XML document showing one <region> element and its children
2. The XSLT stylesheet with templates to transform the input document converting child elements of the <region> element into attribute values in the output document. The template rules hard-code the output element names for <regions> and <region>, but use the attribute value template technique to obtain the text values of the <region_id> and <region_name> elements from input document to set the values for their corresponding attributes id and name in the <region> element of the output document.
3. The resulting output document containing the results after transformation, with the element text from the input document formatted as attribute values in the output
6-*
Creating Elements with Attributes
Inside an <xsl:element>:
<xsl:element name="region">Japan</xsl:element>
<xsl:attribute-set name="region-info">
</xsl:attribute-set>
</xsl:element>
<region>Japan</region>
1
2
3
Creating Elements with Attributes
If you are creating XML output documents, use the <xsl:element> to dynamically create new elements. As shown previously and in the slide, you can perform one of the following:
Explicitly provide element and attribute names
Define rules such that element and attribute names are constructed from nodes in your input XML document at processing time
The slide shows three ways to create an element and the results, with or without attributes:
1. A <region> element without attributes that contains a text value of Japan.
2. A <region> element containing the text Japan, and a single attribute called id with a value of 5.
3. A <region> element with an id and name attribute. This example uses an <xsl:attribute-set> element that declares the id and name attributes and their values. The template creating the output element, derives the element name from the context node using the local-name() XPath function, and generates the attributes by referencing the region-info attribute in use-attribute-sets.
Note: The attribute set hard-codes its attribute names and values in the example. The design can be made to be more flexible with the use of <xsl:parameter> elements.
6-*
Sorting an XML Document
The <xsl:sort> element can be used:
To sort nodes in the input document before the nodes are processed
Inside the <xsl:apply-templates> or <xsl:for-each> elements
Multiple times to specify additional sort criteria
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
Sorting an XML Document
When the example in the slide is applied to the following XML document:
<employees>
</employee>
</employees>
The output shows employee data sorted by their last names in the following sequence:
102 De Haan
100 King
101 Kochhar
Note: Multiple <xsl:sort> elements can be specified for additional sort criteria. You can sort the child elements before the template is applied by using the following syntax:
<xsl:apply-templates><xsl:sort ...></xsl:apply-templates>
Conditional Processing with <xsl:if>
The <xsl:if> is processed when the test attribute evaluates to true.
The test attribute is a Boolean expression.
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
<xsl:for-each select="//department">
<xsl:if test="manager_id">
Conditional Processing with <xsl:if>
Templates rules can perform conditional processing using the <xsl:if> or the <xsl:choose> elements. Both of these elements provide a test evaluating an XPath expression as a Boolean result of true or false. The XPath expression is converted to a true or false value. If the XPath expression evaluates to:
A numeric result of 0, it is converted to false; otherwise, it is considered true
A string result with a length greater than 0, it is converted to true; otherwise, it is false
A node with a value, it is considered true; otherwise, an empty or non-existent node is false
The example in the slide creates a HTML paragraph for each <department> element containing a <manager_id> child element with a value. Any <department> element without a child or empty <manager_id> element are excluded.
Note: There are no <xsl:else> or <xsl:else-if> constructs in XSLT. To emulate an “else” scenario, use the <xsl:choose> element, as discussed in the section titled “Conditional Processing with <xsl:choose>.”
6-*
Conditional Processing with <xsl:choose>
The <xsl:choose> has one or more <xsl:when> elements, and optionally one <xsl:otherwise>.
<xsl:template match="/">
<font color="red">
<xsl:value-of select="salary"/>
The <xsl:choose> element may contain:
One or more <xsl:when> elements, each with a condition in their own test attribute
An optional <xsl:otherwise> element
The <xsl:when> elements are evaluated in the order of appearance; the first <xsl:when> with a true condition is instantiated, and the remaining elements are not processed. If all <xsl:when> conditions yield a false result, then none are processed, unless an <xsl:otherwise> element is provided. The <xsl:otherwise> element must appear last and is processed if no <xsl:when> condition returns a true result.
The example in the slide uses a single <xsl:when> element to check the salary value of an employee. If the salary is less than 10000, the salary is displayed in a red colored font; otherwise, the salary is displayed with a blue colored font. The example emulates an “if-then-else” scenario.
Note: The example uses the < built-in entity reference for the less than operator to ensure that the XSLT stylesheet is well-formed.
6-*
Calling Templates by Name
Call template by name:
Calling Templates by Name
A template rule with a name attribute can be instantiated by referencing its name in an <xsl:call-template> rule. The slide shows:
Assigning the name attribute value of deptlist to the template rule
Note: A template without a match attribute cannot have a mode attribute.
Calling the template by using the <xsl:call-template> element with the name attribute value deptlist identifying the desired template rule to be instantiated
Note: The called template does not have a match attribute;therefore, its processing instructions must use XSLT commands that set the context. For example:
<xsl:template name="deptlist">
<xsl:for-each select="//department">
</a>
</xsl:for-each>
</xsl:template>
Using named templates is an effective way for creating a stylesheet that is used with multiple XML documents. Each XML document applies named templates for default formatting appearance as per company standards, but produces different content.
6-*
Summary
Create an XSL stylesheet containing XSLT elements
Use XSLT to transform an XML document by using a browser and the oraxsl command-line utility
Use <xsl:template> to create a template rule
Use <xsl:apply-templates> for recursion through child template rules
Call named templates with <xsl:call-template> with and without parameters
Apply sort criteria to nodes in an XML document by using the <xsl:sort> element
Summary
XSL Transformations (XSLT) occur when a node in the source matches a template rule pattern. Then, the content of that rule is written to the result tree. Given a source tree and a stylesheet, the XSLT Processor carries out the transformation described by rules in the stylesheet. An XSL stylesheet contains XSLT elements, such as:
<xsl:template> to create a template rule, without and with parameters defined by using an <xsl:param> element
<xsl:apply-templates> for recursion through child template rules
<xsl:call-template> to call named templates
<xsl:value-of> to obtain the value of a node in the XML document
<xsl:for-each> to loop through a node-set
<xsl:if>, or <xsl:choose>, <xsl:when>, and <xsl:otherwise> for conditional processing