Download presentation
Presentation is loading. Please wait.
1
New Perspectives on XML
TUTORIAL 6 WORKING WITH XSLT New Perspectives on XML Tutorial 6
2
New Perspectives on XML
OBJECTIVES In this chapter, you will: Learn about the history and theory of XSL Create an XSLT style sheet Be introduced to syntax of the XPath language Transform an XML document into an HTML file Create templates to format sections of the XML document New Perspectives on XML Tutorial 6
3
New Perspectives on XML
OBJECTIVES In this chapter, you will: Sort the contents of an XML document Create conditional nodes to generate different HTML code Use predicates to select subsets of an XML document Insert new elements and attributes in the transformed document New Perspectives on XML Tutorial 6
4
New Perspectives on XML
The Limitation of CSS CSS has several limitations, including: CSS can apply a style to element, but can’t change the format of the content itself (e.g., can’t change date from June 28, 2004 to 6/28/2004) CSS doesn’t provide easy methods to display images or insert hypertext links CSS displays only element content, not element attributes It is difficult to display content from several documents An element can be formatted only one way in a document New Perspectives on XML Tutorial 6
5
New Perspectives on XML
WORKING WITH XSL In 1998, the W3C developed the Extensible Style sheet Language, or XSL XSL is composed of three parts: XSL-FO (Extensible Style sheet Language – Formatting Objects) XSLT (Extensible Style sheet Language Transformations) XPath New Perspectives on XML Tutorial 6
6
INTRODUCING XSL-FO, XSLT, AND XPATH
XSL-FO is used to implement page layout and design XSLT is used to transform XML content into another presentation format XPath is used to locate information from an XML document and perform operations and calculations upon that content New Perspectives on XML Tutorial 6
7
INTRODUCING XSLT STYLE SHEETS AND PROCESSORS
An XSLT style sheet contains instructions for transforming the contents of an XML document into another format An XSLT style sheet document is itself an XML document An XSLT style sheet document has an extension .xsl New Perspectives on XML Tutorial 6
8
GENERATING A RESULT DOCUMENT
An XSLT style sheet converts a source document of XML content into a result document by using the XSLT processor New Perspectives on XML Tutorial 6
9
INTRODUCING XSLT STYLE SHEETS AND PROCESSORS
The transformation can be performed by a server or a client In a server-side transformation, the server receives a request from a client, applies the style sheet to the source document, and returns the result document to the client In a client-side transformation, a client requests retrieval of both the source document and the style sheet from the server, then performs the transformation, and generates the result document New Perspectives on XML Tutorial 6
10
CREATING AN XSLT STYLE SHEET
To create an XSLT style sheet, the general structure: <?xml version =“1.0”> <xsl:stylesheet version = 1.0 xmlns:xsl =“ Content of the style sheet </xsl:stylesheet> To link the XML file to the style sheet, the syntax is: <?xml-stylesheet type=“text/xsl” href=“URI” ?> <XSL:tranform...> New Perspectives on XML Tutorial 6
11
WORKING WITH DOCUMENT NODES
Under XPath, each component in the document is referred to as a node, and the entire structure of the document is a node tree The node tree consists of the following objects: the XML document itself Comments processing instructions Namespaces elements, element text element attributes New Perspectives on XML Tutorial 6
12
New Perspectives on XML
NODE TREE EXAMPLE New Perspectives on XML Tutorial 6
13
WORKING WITH DOCUMENT NODES
At the top of the node is the root node A node that contains other nodes is called a parent node, and the nodes contained in the parent are called child nodes Nodes that share a common parent are called sibling nodes Any node below another node is referred to as a descendant of that node The node at the top of the branch is referred to as the ancestor of all nodes that lie beneath it New Perspectives on XML Tutorial 6
14
WORKING WITH DOCUMENT NODES
Nodes are distinguished based on the object they refer to in the document A node for an element is called an element node The node that stores element attributes is called an attribute node New Perspectives on XML Tutorial 6
15
USING XPATH TO REFERENCE A NODE
XPath provides the syntax to refer to the various nodes in the node tree The syntax is used by operation system to specify file pathnames The location of a node can be expressed in either absolute or relative terms XPath also does data extraction New Perspectives on XML Tutorial 6
16
USING XPATH TO REFERENCE A NODE
For absolute path, XPath begins with the root node, identified by a forward slash and proceeds down the levels of the node tree An absolute path: /child1/child2/child3/… Ex., /portfolio/stock/description To reference an element without regard to its location in the node tree, use a double forward slash with the name of the descendant node A relative path : //descendant (e.g.,//yield ) New Perspectives on XML Tutorial 6
17
New Perspectives on XML
RELATIVE PATHS With a relative path, the location of the node is indicated relative to a specific node in the tree called the context node New Perspectives on XML Tutorial 6
18
New Perspectives on XML
RELATIVE PATHS <portfolio> <author>Kevin Summers</author> <date>11/14/2004</date> <time>14:54</time> <stock> <name symbol="AA">Aluminum Company of America</name> <description>Alcoa Inc. is a producer…</description> <category>Industrials</category> <link> ….. <today open="29.60" high="29.94" low="28.59" current="29.20" vol="2.94" /> <five_day> <day open="31.20" high="32.61" low="30.15" close="30.51" vol="6.70" date="11/17/2004">1</day> …. </five_day> </stock> เพิ่ม stock.xml New Perspectives on XML Tutorial 6
19
New Perspectives on XML
five_day day New Perspectives on XML Tutorial 6
20
New Perspectives on XML
RELATIVE PATHS *Assumes that the stock element is the context node New Perspectives on XML Tutorial 6
21
REFERENCING GROUPS OF ELEMENTS
XPath allows you to refer to groups of nodes by using the wildcard character (*) To select all of the nodes in the node tree, you can use the path: //* The (*) symbol matches any node, and the (//)symbol matches any level of the node tree Example: /portfolio/stock/* To combine different paths into a single expression Example: /portfolio/date | /portfolio/time //name | //description New Perspectives on XML Tutorial 6
22
REFERENCING ATTRIBUTE NODES
XPath uses different notation to refer to attribute nodes The syntax for attribute node is: @attribute where attribute is the name of the attribute Example: If the stock element is the context node, this would be: If name is the context node, the relative path is @symbol New Perspectives on XML Tutorial 6
23
WORKING WITH TEXT NODES
The text contained in an element node is treated as a text node The syntax for selecting a text node is: @text() Example: /portfolio/stock/name/text() To match all text nodes in the document, use: //text() New Perspectives on XML Tutorial 6
24
CREATING THE ROOT TEMPLATE
The first task in designing the stock.xsl style sheet is to create a template for the root node A template is a collection of elements that define how a particular section of the source document should be transformed in the result document The root template sets up the initial code for the result document New Perspectives on XML Tutorial 6
25
CREATING A ROOTTEMPLATE
To create a root template, the syntax is: <xsl:template match=“/”> XSLT and Literal Result Elements </xsl:template> where node is either the the name of a node from the source document’s node tree, or an XPath expression that points node in the tree New Perspectives on XML Tutorial 6
26
New Perspectives on XML
CREATING A TEMPLATE To create a root template, the syntax is: <xsl:template match=“node”> XSLT and Literal Result Elements </xsl:template> New Perspectives on XML Tutorial 6
27
CREATING THE ROOT TEMPLATE
A template contains two types of content: XSLT elements and literal result elements XSLT elements are those elements that are part of the XSLT namespace and are used to send commands to the XSLT processor A literal result element is text sent to the result document, but not acted upon by the XSLT processor New Perspectives on XML Tutorial 6
28
CREATING THE ROOT TEMPLATE EXAMPLE
<?xml version='1.0' ?> <xsl:stylesheet version='1.0' xmlns:xsl=" <xsl:output method="html" version="4.0" /> <xsl:template match="/"> <html> <head><title>Stock Information</title> <link href="stock.css" rel="stylesheet" type="text/css" /> </head> <body> <h1 class="title">Hardin Financial</h1> <h2 class="title">Stock Information</h2> <xsl:value-of select="portfolio/author" /> </body> </html> </xsl:template> </xsl:stylesheet> XSLT element Listeral result elements XSLT element h1, h2 {font-family: Arial, Helvetica, sans-serif} h1.title, h2.title {color:red} h2.title {font-style:italic; margin-top:-25px} New Perspectives on XML Tutorial 6
29
Stock.xml (invoking the xslt file)
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="stock.xsl" ?> <portfolio> <author>Kevin Summers</author> <date>11/14/2004</date> <time>14:54</time> <stock> <name symbol="AA">Aluminum Company of America</name> …. </stock> </portfolio> stock.xml เพิ่ม New Perspectives on XML Tutorial 6
30
SPECIFYING THE OUTPUT METHOD
By default, the XSLT processor will render the result document as an XML file To control how the processor formats the source document, you can specify the output method using the <xsl:output/> element New Perspectives on XML Tutorial 6
31
ATTRIBUTS OF THE <XSL:OUTPUT/> ELEMENT
New Perspectives on XML Tutorial 6
32
New Perspectives on XML
To create an HTML 4.0 file, the output tag is <xsl:output method=“html” version=“4.0” /> To create a piece of XML document and remove an opening XML declaration, the output tag is <xsl:output method=“xml” version=“1.0” omit-xmldeclaration=“yes” /> To create a plain text file, the output tag is <xsl:output method=“text” /> New Perspectives on XML Tutorial 6
33
TRANSFORMING A DOCUMENT
A browser with a built-in XSLT processor allows you to view the result document Alternatively, you can use XML Spy to create the result document as a separate file, and then view that file in your browser Most XSLT processors provide the capability to create the result document as a separate file New Perspectives on XML Tutorial 6
34
VIEWING THE RESULT DOCUMENT IN A BROWSER
Internet Explorer 6.0 contains built-in XSLT processor You can view the results of the transformation by opening the result document in the browser New Perspectives on XML Tutorial 6
35
CREATING AN HTML FILE IN XML SPY
One advantage of creating a separate HTML file is that it can be viewed in any Web browser You have to regenerate the HTML file every time you make a change to the source document, or the style sheet The XSLT processor adds one extra line to the document that provides additional information to the browser about the content of the document and its encoding <META http-equiv=“Content-Type” content=“text/html; charset=UTF-16”> New Perspectives on XML Tutorial 6
36
TRANSFORMING THE SOURCE DOCUMENT IN XML SPY
New Perspectives on XML Tutorial 6
37
New Perspectives on XML
INSERTING A NODE VALUE To insert a node’s value into the result document, the syntax is: <xsl:value-of> select=“XPath Expression” /> where XPath Expression is an expression that identifies the node from the source document’s node tree If the node contains child elements in addition to text content, the text in those child nodes appears as well New Perspectives on XML Tutorial 6
38
New Perspectives on XML
INSERTING A NODE VALUE For example, if the element date and time need to be display as follows: Last Updated: 11/24/2004 at 14:54 The names of the stocks are also displayed as h3 headings Add the following code to the root template of stock.xsl New Perspectives on XML Tutorial 6
39
NODE TREE EXAMPLE (Stock.xml)
Root(/) portfolio author date time stock name Five_day day New Perspectives on XML Tutorial 6
40
INSERTING A NODE VALUE EXAMPLE
stock.xsl …. <xsl:template match="/"> <html> … <body> <div id="datetime"><b>Last Updated: </b> <xsl:value-of select="portfolio/date" /> at <xsl:value-of select="portfolio/time" /> </div> <h1 class="title">Hardin Financial</h1> <h2 class="title">Stock Information</h2> <h3 class="name"> <xsl:value-of select="portfolio/stock/name" /> </h3> </body> เพิ่ม =portfolio/date/text() New Perspectives on XML Tutorial 6
41
INSERTING A NODE VALUE XP EXAMPLE
#datetime {font-size:8pt; position:absolute; top:0px; left:480px} h1, h2 {font-family: Arial, Helvetica, sans-serif} h1.title, h2.title {color:red} h2.title {font-style:italic; margin-top:-25px} h3.name {color: brown; clear: right} stock.css Date and time stock name New Perspectives on XML Tutorial 6
42
PROCESSING A BATCH OF NODES
To process a batch of nodes, the syntax is: <xsl:for-each select=“XPath Expression” /> XSLT and Literal Elements </xsl:for-each> where XPath Expression is an expression that defines the group of nodes to which the XSLT and literal result elements are applied New Perspectives on XML Tutorial 6
43
PROCESSING A BATCH OF NODES EXAMPLE
root/ portfolio stock name stock name New Perspectives on XML Tutorial 6
44
PROCESSING A BATCH OF NODES EXAMPLE
Stock name New Perspectives on XML Tutorial 6
45
WORKING WITH TEMPLATES
To apply a template in the result document, use the XSLT element <xsl:apply-templates select=“XPath Expression” /> where XPath Expression indicates the node template to be applied New Perspectives on XML Tutorial 6
46
CREATING TEMPLATE EXAMPLE
“portfolio/stock” <xsl:template match=“stock”> <xsl:value-of select=“name” /> </xsl:template> New Perspectives on XML Tutorial 6
47
CREATING TEMPLATE EXAMPLE
Stock name New Perspectives on XML Tutorial 6
48
USING THE BUILT-IN TEMPLATES
Each node has its own built-in template. The built-in template for element nodes matches the document root and all elements in the node tree The built-in template for text nodes matches all text nodes and causes their values to appear in the result document <xsl:template match=“*|/” > <xsl:apply-templates /> </xsl:template> New Perspectives on XML Tutorial 6
49
CREATING THE STOCK TEMPLATE EXAMPLE
<xsl:template match="/"> <html> … <h2 class="title">Stock Information</h2> <xsl:apply-templates select="portfolio/stock" /> </body> </html> </xsl:template> <xsl:template match="stock"> <div class="stock_info"> <xsl:apply-templates select="name" /> <p><xsl:value-of select="description" /></p> </div> <xsl:template match="name"> <h3 class="name"> <xsl:value-of select="." /> </h3> </xsl:stylesheet> root/ portfolio stock name description stock name name New Perspectives on XML Tutorial 6
50
CREATING THE STOCK TEMPLATE EXAMPLE
apply-templates select="name" apply-templates select="portfolio/stock" (For the first time invocation) apply-templates select="name“ (two names) New Perspectives on XML Tutorial 6
51
CREATING THE STOCK TEMPLATE EXAMPLE
<h2 class="title">Stock Information</h2> <xsl:apply-templates select="portfolio/stock" /> </body> </html> </xsl:template> <xsl:template match="stock"> <h3 class="name"> <xsl:value-of select="name" /> </h3> <div class="stock_info"> <p> <xsl:value-of select="description" /> </p> </div> </xsl:stylesheet> root/ portfolio stock name description stock What is the results? name name New Perspectives on XML Tutorial 6
52
CREATING THE STOCK TEMPLATE EXAMPLE
portfolio <h2 class="title">Stock Information</h2> <xsl:apply-templates select="portfolio/stock" /> </body> </html> </xsl:template> <xsl:template match="stock"> <table class="summary" border="1"> <tr> <th colspan= "2" class="summtitle">Summary Information </th> </tr> <th class="summary">Year High</th> <td class="number"><xsl:value-of select="year_high" /></td> <th class="summary">Year Low</th> <td class="number"><xsl:value-of select="year_low"/></td> stock table.summary {font-size: 10pt; float:right; margin-top:30px} name year_high year_low stock name year_high year_low New Perspectives on XML Tutorial 6
53
CREATING THE STOCK TEMPLATE EXAMPLE
New Perspectives on XML Tutorial 6
54
Working with Attribute Nodes
If the stock name needs to be appended with value of symbol attribute <stock> <name symbol="AA">Aluminum Company of America</name> The result of displaying the stock name is Aluminum Company of America (AA) New Perspectives on XML Tutorial 6
55
Working with Attribute Nodes
The name template can be modified into <xsl:template match="name"> <h3 class="name"> <xsl:value-of select="." /> (<xsl:value-of />) </h3> </xsl:template> New Perspectives on XML Tutorial 6
56
New Perspectives on XML
SORTING NODES By default, nodes are processed in document order, by their appearance in the document To specify a different order, XSLT provides the <xsl:sort> element This element can be used with either the <xsl:apply-templates> or the <xsl:for-each> element New Perspectives on XML Tutorial 6
57
New Perspectives on XML
SORTING NODES The <xsl:sort> element contains several attributes to control how the XSLT process sorts the nodes in the source document.The syntax is <xsl:sort select=“expression” data-type=“type” order=“type” case-order=“type” /> The select attribute determines the criteria under which the context node is sorted The data-type attribute indicates the type of data The order attribute indicates the direction of the sorting (ascending or descending) New Perspectives on XML Tutorial 6
58
EXAMPLE OF SORTING NODES
<xsl:apply-templates select=“portfolio/stock”> <xsl:sort select=“name” /> </xsl:apply-templates> <xsl:apply-templates select=“day”> <xsl:sort data-type=“number” order=“descending” /> <xsl:sort select=“category” /> <xsl:apply-templates select=“portfolio/stock/name”> <xsl:sort /> </xsl:apply-templates> New Perspectives on XML Tutorial 6
59
EXAMPLE OF SORTING NODES
<xsl:template match="/"> <html> <head><title>Stock Information</title> <link href="stock.css" rel="stylesheet" type="text/css" /> </head> <body> <h1 class="title">Hardin Financial</h1> <h2 class="title">Stock Information</h2> <xsl:apply-templates select="portfolio/stock/name" > <xsl:sort /> </xsl:apply-templates> </body> </html> </xsl:template> <xsl:template match="name"> <h3 class="name"> <xsl:value-of select="." /> </h3> New Perspectives on XML Tutorial 6
60
EXAMPLE OF SORTING NODES
<xsl:template match="/"> <html> <head><title>Stock Information</title> <link href="stock.css" rel="stylesheet" type="text/css" /> </head> <body> <h1 class="title">Hardin Financial</h1> <h2 class="title">Stock Information</h2> <xsl:apply-templates select="portfolio/stock" > <xsl:sort select="name"/> </xsl:apply-templates> </body> </html> </xsl:template> <xsl:template match="stock"> <h3 class="name"> <xsl:value-of select="name" /> </h3> New Perspectives on XML Tutorial 6
61
CREATING CONDITIONAL NODES
XSLT supports two kinds of conditional elements: <xsl:if> <xsl:choose> To apply a format only if a particular condition is met , use the <xsl:if> element To test for multiple conditions and display different outcomes, use the <xsl:choose> element New Perspectives on XML Tutorial 6
62
CREATING CONDITIONAL NODES
The syntax for the <xsl:if> element is: <xsl:if test=“expression”> XSLT and Literal Result Elements </xsl:if> Example: <xsl:if = ‘AA’ ”> <h3><xsl:value-of select=“name” /></h3> New Perspectives on XML Tutorial 6
63
CREATING CONDITIONAL NODES EXAMPLE
The syntax for the <xsl:choose> element is: <xsl:choose> <xsl:when test=“expression1”> XSLT and Literal Result Elements </xsl:when> <xsl:when test=“expression2”> … <xsl:otherwise> </xsl:otherwise> </xsl:choose> New Perspectives on XML Tutorial 6
64
CREATING CONDITIONAL NODES EXAMPLE
Example of using <xsl:choose> element: <xsl:choose> <xsl:when <img src=“down.gif” /> </xsl:when> <xsl:when <img src=“up.gif” /> <xsl:otherwise> <img src=“same.gif” /> </xsl:otherwise> </xsl:choose> New Perspectives on XML Tutorial 6
65
USING COMPARISON OPERATORS AND FUNCTIONS
New Perspectives on XML Tutorial 6
66
CREATING CONDITIONAL NODES EXAMPLE
stock <name symbol=…> … <today open=… High=…low=… Current=…vol=…> … New Perspectives on XML Tutorial 6
67
USING COMPARISON XP OPERATORS AND FUNCTIONS
New Perspectives on XML Tutorial 6
68
WORKING WITH PREDICATES
Predicates are XPath expressions that test for a condition and create subsets of nodes that fulfill that condition The predicate can also indicate the position of the node in the node tree The syntax is node [expression] New Perspectives on XML Tutorial 6
69
WORKING WITH PREDICATES
To select the name elements whose symbol attribute is “AA”, the predicate is: = “AA”] To select name elements whose have a symbol attribute => To select the first stock element => stock[1] To select the last stock element => stock[last()] To select the second stock element =>stock[position() = 2] To combine predicate with XPath expression <xsl:value-of select=“stock[position()=2]/name” /> New Perspectives on XML Tutorial 6
70
ADDING PREDICATES TO THE ROOT TEMPLATE EXAMPLE
portfolio date time stock name category link … stock New Perspectives on XML Tutorial 6
71
ADDING PREDICATES TO THE ROOT TEMPLATE EXAMPLE
Category=“Industrials” New Perspectives on XML Tutorial 6
72
CREATING ELEMENTS AND ATTRIBUTES
To produce the result document as follows: <a href=“ <h3 class=“name”> Aluminum Company of America </h3> </a> The problem will occur when the template is created as: <xsl:template match=“name”> <a href=“ <xsl:value-of select=“../link” /> ”> <xsl:value-of select=“.” /> </xsl:template> New Perspectives on XML Tutorial 6
73
CREATING ELEMENTS AND ATTRIBUTES
One way of solving problem is to create the <a> element in the result document. To create an element, XSLT uses the <xsl:element> tag as the following syntax: <xsl:element name=“name” namespace=“URI” use-attribute-sets=“namelist”> XSLT and Literal Result Elements </xsl:element> New Perspectives on XML Tutorial 6
74
New Perspectives on XML
CREATING AN ELEMENT To create the <a> element in the result document, use the <xsl:element> tag The result is => <a> <h3 class=“name”> Aluminum Company of America (AA) </h3> </a> New Perspectives on XML Tutorial 6
75
New Perspectives on XML
CREATING AN ATTRIBUTE Attributes are created in XSLT by using the <xsl:attribute> element The name attribute specifies the name of the attribute The namespace attribute indicates the namespace You can create inline images in the result document by using the attribute tag New Perspectives on XML Tutorial 6
76
New Perspectives on XML
CREATING AN ATTRIBUTE To add the href attribute to the <a> tag, use the <xsl:attribute> element New Perspectives on XML Tutorial 6
77
New Perspectives on XML
CREATING AN ATTRIBUTE The result is : <a href=“ <h3 class=“name”> Aluminum Company of America (AA) </h3> </a> New Perspectives on XML Tutorial 6
78
CREATING COMMENTS AND PROCESSING INSTRUCTIONS
The <xsl:comment> element creates the comment You can create a processing instruction by using the <xsl:processing-instruction> element If you want to add a processing instruction to attach the result document to the style.css sheet, use the following code: <?xml-stylesheet href=“styles.css” type=“text.css” ?> New Perspectives on XML Tutorial 6
79
New Perspectives on XML
SUMMARY Extensible Style sheet Language,or XSL, is composed of three parts: XSL-FO, XSLT, and XPath XPath language is used to reference a node Templates are used to format sections of the XML document and transform XML data into a variety of formats New Perspectives on XML Tutorial 6
80
New Perspectives on XML
SUMMARY Nodes can be sorted in either alphabetical or numerical order Comparison elements allow changing the contents of the result document based on the values of the nodes in the source document Predicates are used to create subsets of the source document’s node tree You can insert new elements and attributes in the transformed document New Perspectives on XML Tutorial 6
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.