Sei sulla pagina 1di 23

6

Transforming XML by Using XSL


Transformations

Copyright 2004, Oracle. All rights reserved.

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
Create, apply, and call templates
Sort and filter an XML document
Use parameters with templates
Use the oraxsl command-line utility

6-2

Copyright 2004, Oracle. All rights reserved.

What Is XSL?
Extensible Stylesheet Language (XSL) is made up of
two parts:
XSL Transformations (XSLT)
XSL Formatting Objects (XSL-FO)

XSL

6-3

Copyright 2004, Oracle. All rights reserved.

XSL Transformations

Input XML
document

XSL
processor

Output
XML
document

XSL
stylesheet

Matchin
g
rule
6-4

<?xml version="1.0"?>
<xsl:stylesheet ...>
<xsl:template match="XPath"/>
<!-- output information -->
</xsl:template>
</xsl:stylesheet>
Copyright 2004, Oracle. All rights reserved.

Output
data

The XSLT Stylesheet


An XSLT stylesheet is an XML document containing:
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"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
...
<xsl:template match="/"> ... </xsl:template>
<xsl:template match="..."> ... </xsl:template>
<xsl:stylesheet>
6-5

Copyright 2004, Oracle. All rights reserved.

XSLT Stylesheet: Example


<?xml version="1.0"?>
<xsl:stylesheet version ="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr><th>Id</th><th>Name</th><th>Salary</th></tr>
<xsl:apply-templates/>
5
</table>
4
</body>
</html>
</xsl:template>
<xsl:template match="employee">
<tr>
<td><xsl:value-of select="employee_id"/></td>
<td><xsl:value-of select="last_name"/></td>
<td><xsl:value-of select="salary"/></td>
6
</tr>
</xsl:template>
</xsl:stylesheet>
6-6

Copyright 2004, Oracle. All rights reserved.

1
2
3

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"?>
<?xml-stylesheet type="text/xsl" href="emp.xsl"?>
<employees>
...
</employees>
6-7

Copyright 2004, Oracle. All rights reserved.

Viewing the Transformed Document


Perform one of the following:
Open the XML document in the browser
View oraxsl command-line processor output

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="emp.xsl"?>
<employees>
<employee>
<employee_id>100</employee_id>
<last_name>King</last_name>
<salary>24000</salary>
</employee>
<employee>
<employee_id>101</employee_id>
<last_name>Kochhar</last_name>
<salary>18000</salary>
</employee>
</employees>

6-8

Copyright 2004, Oracle. All rights reserved.

Creating Template Rules


The template rule:
Is created using <xsl:template> elements with:
A match attribute with an XPath pattern value
The output template containing formatting
instructions to produce a result tree

<xsl:template match="pattern">
output-template
</xsl:template>

Is applied when a node in the input XML document


matches the XPath pattern in the rule

<xsl:template match="/">
A simple text string
</xsl:template>

6-9

Match pattern determines its context node


Copyright 2004, Oracle. All rights reserved.

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:
<xsl:template match="region">
<xsl:value-of select="."/>
</xsl:template>
<region num="1">
<id>1</id>
<name>Europe</name>
</region>
Input XML document
6-10

1 Europe
Output document

Copyright 2004, Oracle. All rights reserved.

Applying Template Rules

Use <xsl:apply-templates/> to recursively


process children of the context (current) node.
<xsl:template match="/">
Document root
</xsl:template>

B
B
C

Input XML
document
6-11

Document root

<xsl:template match="/">
Document root
<xsl:apply-templates/>
</xsl:template>

A
D

<xsl:template match="A">
Root element A
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="B">
Element B
</xsl:template>
Copyright 2004, Oracle. All rights reserved.

Output
Document

Document root
Root element A
Element B
Element B

Controlling Template Activation Order


The <xsl:apply-templates> activates one of the
following:
Templates matching child nodes by default
Templates matching its optional select attribute
Example:
<?xml version='1.0' encoding='windows-1252'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates select="//region"/>
</xsl:template>
<xsl:template match="regions">
<h1>Regions</h1><xsl:apply-templates/>
</xsl:template>
<xsl:template match="region">
<p><xsl:value-of select="."/></p>
</xsl:template>
</xsl:stylesheet>
6-12

Copyright 2004, Oracle. All rights reserved.

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"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
Document root
</xsl:template>
<xsl:template match="/" priority="1">
Apply this document Root
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="department">
Department Data <!-- default priority -0.5 -->
</xsl:template>
<xsl:template match="department">
I WIN!
<!-- default priority -0.5 -->
</xsl:template>
</xsl:stylesheet>

6-13

Copyright 2004, Oracle. All rights reserved.

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:
<xsl:template match="*|/">
<xsl:apply-templates/>
</xsl:template>

The text and attribute nodes:


<xsl:template match="text()|@*">
<xsl:value-of select="."/>
</xsl:template>

6-14

Copyright 2004, Oracle. All rights reserved.

Effects of Default Template Rules


<?xml version="1.0"?>
<xsl:stylesheet version ="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr><th>Id</th><th>Name</th><th>Salary</th></tr>
<xsl:apply-templates/>
<!--employees-->
</table>
<xsl:template match="*|/">
</body>
<xsl:apply-templates/>
</html>
</xsl:template>
</xsl:template>
<xsl:template match="employee">
<tr>
<td><xsl:value-of select="employee_id"/></td>
<td><xsl:value-of select="last_name"/></td>
<td><xsl:value-of select="salary"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>
6-15

Copyright 2004, Oracle. All rights reserved.

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"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="departments">
<xsl:for-each select="department">
<p><xsl:value-of select="."/></p>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
6-16

Copyright 2004, Oracle. All rights reserved.

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>
1
<region_id>1</region_id>
<region_name>Europe</region_name>
</region>
<xsl:template match="/">
2
<regions><xsl:apply-templates/></regions>
</xsl:template>
<xsl:template match="region">
<region id="{region_id}" name="{region_name}"/>
</xsl:template>
<regions><region id="1" name="Europe"/></regions> 3
6-17

Copyright 2004, Oracle. All rights reserved.

Creating Elements with Attributes

To create an element, use <xsl:element>:


<xsl:element name="region">Japan</xsl:element>
<region>Japan</region>

To create an attribute, use <xsl:attribute>:


Inside an <xsl:element>:

2
<xsl:element name="region">
<xsl:attribute name="id">5</xsl:attribute>Japan
</xsl:element>
<region id="5">Japan</region>

Inside <xsl:attribute-set> element used in the


use-attribute-set attribute of <xsl:element>:

3
<xsl:attribute-set name="region-info">
<xsl:attribute name="id">5</xsl:attribute>
<xsl:attribute name="name">Japan</xsl:attribute>
</xsl:attribute-set>
<xsl:template match="region[1]">
<xsl:element name="{local-name()}"
use-attribute-sets="region-info">
</xsl:element>
</xsl:template>
<region id="5" name="Japan"/>
6-18

Copyright 2004, Oracle. All rights reserved.

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

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/employees">
<xsl:for-each select="employee">
<xsl:sort select="last_name"/>
<p><xsl:value-of select="."/></p>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

6-19

Multiple times to specify additional sort criteria


Copyright 2004, Oracle. All rights reserved.

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"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<h2>Departments with a manager</h2>
<xsl:for-each select="//department">
<xsl:if test="manager_id">
<p><xsl:value-of select="."/></p>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

6-20

Copyright 2004, Oracle. All rights reserved.

Conditional Processing with


<xsl:choose>
The <xsl:choose> has one or more <xsl:when>
elements, and optionally one <xsl:otherwise>.
<xsl:template match="/">
<xsl:for-each select="//employee">
<p><xsl:value-of select="last_name"/>,
<xsl:choose>
<xsl:when test="salary &lt; 10000">
<font color="red">
<xsl:value-of select="salary"/>
</font>
</xsl:when>
<xsl:otherwise>
<font color="blue">
<xsl:value-of select="salary"/>
</font>
</xsl:otherwise>
</xsl:choose></p>
</xsl:for-each>
</xsl:template>
6-21

Copyright 2004, Oracle. All rights reserved.

Calling Templates by Name

Create a template specifying the name attribute:


...
<xsl:template name="deptlist">
<!-- instructions to process -->
</xsl:template>
...

Call template by name:


...
<xsl:template match="/departments">
<html>
<body>
<h1>Department Report</h1>
<ol><xsl:call-template name="deptlist"/></ol>
<xsl:apply-templates mode="body"/>
</body>
...

6-22

Copyright 2004, Oracle. All rights reserved.

Summary
In this lesson, you should have learned how to:
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
6-23

Copyright 2004, Oracle. All rights reserved.

Potrebbero piacerti anche