SlideShare uma empresa Scribd logo
1 de 33
XML For Dummies Book Author : Lucinda Dykes & Ed Tittle Slides Prepared By: Son TN Part 4 : Transforming and Processing XML Chapter 12 : Handling Transformations with XSL
Contents What is XSL ? The Two Faces of XSL XSL Stylesheets Are XML Documents A Simple Transformation Using XSLT An XSLT Stylesheet for Converting XML to HTML Creating an XSLT Stylesheet with XSLT Editors
12.1 What is XSL ? XSL is a language for expressing style sheets. An XSL style sheet is, like with CSS, a file that describes how to display an XML document of a given type. XSL is also a powerful tool for converting your XML document from one set of markup (vocabulary) to another it’s really just XML. XSL shares the functionality and is compatible with CSS2 (although it uses a different syntax) . It also adds : A transformation language  for XML documents: XSLT  Advanced styling features, expressed by an XML document type which defines a set of elements called Formatting Objects, and attributes
12.2 The Two Faces of XSL While XSL originally started out as a single language, development quickly split it into two independent components. A language for "transforming" XML documents by reorganizing and restructuring XML data trees (known as XSLT). An XML vocabulary to handle the formatting and layout of the result (now known as XSL or, sometimes, XSL-FO).
12.2.1 XSLT
12.2.1.1 What is XSLT ?  eXtensibleStylesheet Language for Transformations Language for transforming XML documents A programming language for XML documents A functional language, based on value substitution Augmented with pattern matching And also template substitution to construct output (based on namespaces Uses XML syntax
12.2.1.2 The ways to use XSLT You can use XSLT in a couple of especially intriguing ways: To transform documents described using XML elements into HTML or XHTML for display in a Web browser. In XML-based data exchange systems.
12.2.1.3 The ins and outs of data exchange It’s entirely possible that two (or more) systems exchanging data don’t use the exact same schema or DTD. The following are two solution to deal with this problem : First , for one system to change its internal programming to work with the other’s DTD or schema  (this approach isn’t practical) Second , this approach is more practical solution, for each system to support data transformation so that each can continue to use the format it needs.
12.2.1.4 The role of XPath Without XPath, XSLT simply wouldn’t work. XPath is the mechanism XSLT uses to point to a piece of an XML document so that it can be transformed. If you can’t find it, you can’t change it XPath is a very sophisticated set of rules for identifying the most specific pieces of an XML document. Example : If you want to do something special to every third instance of a list item, you have to be able to point to it and that’s what XPath makes possible.
12.2.2 XSL-FO
12.2.2.1 Overview The names of most XML elements describe the semantic meaning of the content they contain The content needs to be formatted and displayed to users There must be a step where formatting information is applied to the XML document and the semantic markup is transformed into presentation markup Cascading Style Sheets (CSS) XSL Formatting Objects (XSL-FO)
12.2.2.2 What is XSL-FO ? XSL-FO is a complete XML application for describing the precise layout of text on a page. XSL-FO has elements that represent pages, blocks of text on the pages, graphics, horizontal rules, and more XSLT + XSL-FO Most of the time, you write an XSLT stylesheet that transforms your document's native markup into XSL-FO There is normally a third step in which another processor transforms the XSL-FO into a third format, such as PDF and TeX
12.2.2.3 Categories of XSL-FO The different XSL-FO formatting objects are organized into these eight categories: Declaration, Pagination, and Layout Block Inline Table List Links and Multi Out-of-line Other Every formatting object has properties that can be applied to it. For example, the object that creates a table (fo:table) can take properties that specify how thick its borders are, what color they are, and so on.
12.3 XSL Stylesheets Are XML Documents XSLT stylesheet is an XML document, so: The whole stylesheet should be well-formed, the following rule is invalid: Like any XML document, contains XML declaration at the top Its documentelement is <xsl:stylesheet> which contains all the template rules <xsl:template match="/"> 		<trifle> 			<piffle> 				<flim> 			</piffle> 		</trifle> 	</xsl:template>
12.4 A Simple Transformation Using XSLT An XSLT stylesheet consists of instructions that tell the computer : How to convert a document described by a particular schema or DTD to a document described by a second, different schema or DTD. The best way to show you how XSLT works is by example. In the following section, we show a simple XSLT stylesheet that transforms books2.xml into an HTML document.
12.5 An XSLT Stylesheet for Converting XML to HTML If we open books2.xml in Internet Explorer, the program ignores all the elements it doesn’t recognize — which is all of them — and simply displays the text within the markup, as shown in Figure 12-1.
12.5 An XSLT Stylesheet for Converting XML to HTML (Cont) If you use the following XSLT stylesheet with books2.xml and then open it in Internet Explorer, as shown in Figure 12-2, you have a much more userfriendly and functional document for the Web
12.5 An XSLT Stylesheet for Converting XML to HTML (Cont) <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">     <xsl:template match="/">         <html>             <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />             <head>                 <title>Books</title>                  <link rel="stylesheet" type="text/css" href="books2_xslt.css"/>             </head>             <body>                 <xsl:for-each select="books">                     <table>                        <tr>                            <th>Title</th>                            <th>Author</th>                            <th>Publisher</th>                             <th>Price</th>                             <th>ISBN</th> 	             <th>Content Type</th>                              <th>Format</th>                             </tr>                             <xsl:for-each select="book">                                 <tr>                                     <td>                                         <xsl:for-each select="title">                                                 <xsl:apply-templates />                                         </xsl:for-each>                                     </td> 	    <td>                                         <xsl:for-each select="author">                                                 <xsl:apply-templates /> 	</xsl:for-each>                                     </td>                                     <td>                                         <xsl:for-each select="publisher">                                                 <xsl:apply-templates />                                         </xsl:for-each>                                     </td>                                     <td>                                         <xsl:for-each select="price">                                                 <xsl:apply-templates />                                         </xsl:for-each>                                     </td>                                     <td>                                         <xsl:for-each select="isbn">                                                 <xsl:apply-templates />                                         </xsl:for-each>                                     </td>                                     <td>                                         <xsl:for-each select="@contentType">                                                 <xsl:value-of select="." />                                         </xsl:for-each>                                     </td>                                     <td>                                         <xsl:for-each select="@format">                                                 <xsl:value-of select="." />                                         </xsl:for-each>                                     </td>                                 </tr>                             </xsl:for-each>                     </table>                 </xsl:for-each>             </body>         </html>     </xsl:template> </xsl:stylesheet> Listing 12-1: The books2.xslt Stylesheet
12.5.1 The pieces of the stylesheet puzzle  XSLT is a robust conversion tool and has many facets. In this section, we break up an XSLT stylesheet into its component parts to see what makes it do its stuff. We focus on the most basic structures that make up an XSLT stylesheet . Getting all the pieces of the XSLT puzzle right means creating and using XSLT templates to transform one set of markup (vocabulary) to another.
12.5.1.1 Using patterns Template rules identify the nodes to which they apply by using a pattern.  To identify the node to which the template applies, XSLT uses the match attribute with the xsl:template element to point to a specific node. The value of match is called a pattern. The following is structure of XSLT Document  This is followed by a template: <?xml version=”1.0” encoding=”UTF-8”?> <xsl:stylesheet version=”2.0” xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”> This template matches the XML document root (/) to the HTML document root and (as spelled out in Listing 12-1) adds the following HTML markup: <xsl:template match=”/”> <html> <meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8” /> <head> <title>Books</title> <link rel=”stylesheet” type=”text/css” href=”books2_xslt.css”/> </head> <body>
12.5.1.1 Using patterns (Cont) In our XSLT document, this xsl:template element contains the entire HTML document — but we could also use an XSLT stylesheet that uses a separate template element to match each XML element. Because our XML document can include any number of book elements ,[object Object],<xsl:template match=”publisher”>         <td>                 <xsl:apply templates/>         </td> </xsl:template>
12.5.1.2 Using instructions to get results In addition to using a pattern to identify which node(s) in the source document to transform, the templatespecifies how to transform each node. These instructions guide the XSLT processor through content transformation. We use a value-of instruction with the book attributes to tell the processor to convert the result to text and place it in an HTML table cell <td>         <xsl:for-each select=”@contentType”>                 <xsl:value-of select=”.” />         </xsl:for-each> </td>
12.5.2 Processing element content You use the xsl:apply-templates instruction when you want to return the content and text nodes of the current element and its children — but not the surrounding element tags. <td>         <xsl:for-each select=”price”>                <xsl:apply-templates />         </xsl:for-each> </td>
12.5.2.1 Choosing templates What template gets applied here? It depends; the XSLT processor looks for the best template to apply and makes a choice: If a select attribute is used with apply-templates, then the processor looks for the best template for that node. If no select attribute is used, the template rule that contains the apply-templates instruction is chosen as the current node. So when you create XSLT style rules, you have to think about not only the way you’re transforming an individual node, but also how to deal with its content. <td>         <xsl:for-each select=”price”>                <xsl:apply-templates />         </xsl:for-each> </td> In this case, the XML price element.(No select attribute is used  for apply-template instruction)
12.5.2.2 Adding external CSS As in listing 11.2, A link to an external CSS stylesheet in our XSLT markup <link rel=”stylesheet” type=”text/css” href=”books2_xslt.css”/> ,[object Object],<td>       <xsl:for-each select=”author”>             <xsl:apply-templates />       </xsl:for-each> </td> <td style=”background-color:#FFFF80; color:#800000;border-color:#800000; border-width:1px;border-style:solid; font-family:Trebuchet MS, Verdana, sans-serif; font-size:small; padding:5px; text-align:left; vertical-align:text-top;”>       <xsl:for-each select=”author”>             <xsl:apply-templates />       </xsl:for-each> </td> td {       background-color: #ffff80;       color: #800000;       text-align: left;       vertical-align: text-top;       padding: 5px;       … }
12.5.2.3 Dealing with repeating elements Our XSLT stylesheet (books2.xslt) deals with repeating elements by using a single template element that contains all the instructions for transforming each instance (xsl:for-each) of these elements in our XML document. The following is a another way  : Here the xsl:for-each element points at a specific element that is repeated — and applies the same transformation to each instance of that element. <xsl:template match=”books/book”>       <ul>             <xsl:for-each select=”publisher”>                    <li>                            <xsl:apply-templates/>                    </li>             </xsl:for-each>       </ul> </xsl:template> In this template, every publisher element nested within <books><book> . . . </book></books> is changed to an HTML list item (li) within an unordered list (ul).
12.6 Creating an XSLT Stylesheet with XSLT Editors If hand-coding is not your favorite art form, you can also use an XSLT editor to create your XSLT stylesheetsinstead.As example : AltovaStyleVision The following is steps how to use Altova Style Vision to deal with books2.xsd file. (Please download books2.xsd file from www.dummies.com/go/xmlfd4e )
12.6 Creating an XSLT Stylesheet with XSLT Editors (Cont) Open books2.xsd (or any schema document) in StyleVision.
12.6 Creating an XSLT Stylesheet with XSLT Editors (Cont) Select the book element from the document diagram, and drag it onto the pane on the right, onto the area labeled (contents). A menu appears, as shown in Figure 12-4.
12.6 Creating an XSLT Stylesheet with XSLT Editors (Cont) Choose Create Table from the displayed menu.
12.6 Creating an XSLT Stylesheet with XSLT Editors (Cont) In the Create Dynamic Table dialog box, deselect any attributes or children you don’t want to include in the table display and then click OK. This action creates table cells for each of the selected attributes and child elements of the book element. You can specify additional table formatting features in this dialog box: Table Growth Disply Cells As Header / Footer
12.6 Creating an XSLT Stylesheet with XSLT Editors (Cont) To add formatting properties, place the cursor inside the table, row, column, or cell that you want to format. Select additional formatting properties for individual cells on the StyleVisioncanvas.  Click the XSLT-HTML tab at the bottom of the canvas on the right side to view the source code for the XSLT document. Choose File➪Save Generated Files➪Save Generated XSLT-HTML File.
12.7 Summary Picking your style of XSL Transforming XML documents with XSLT Using templates for style Creating an XSLT stylesheet with an XSLT editor

Mais conteúdo relacionado

Semelhante a Xml For Dummies Chapter 12 Handling Transformations With Xsl it-slideshares.blogspot.com

XML processing with perl
XML processing with perlXML processing with perl
XML processing with perl
Joe Jiang
 
C:\fakepath\xsl final
C:\fakepath\xsl finalC:\fakepath\xsl final
C:\fakepath\xsl final
shivpriya
 

Semelhante a Xml For Dummies Chapter 12 Handling Transformations With Xsl it-slideshares.blogspot.com (20)

XML processing with perl
XML processing with perlXML processing with perl
XML processing with perl
 
Week 12 xml and xsl
Week 12 xml and xslWeek 12 xml and xsl
Week 12 xml and xsl
 
C:\fakepath\xsl final
C:\fakepath\xsl finalC:\fakepath\xsl final
C:\fakepath\xsl final
 
3 xml namespaces and xml schema
3   xml namespaces and xml schema3   xml namespaces and xml schema
3 xml namespaces and xml schema
 
XML and Web Services with PHP5 and PEAR
XML and Web Services with PHP5 and PEARXML and Web Services with PHP5 and PEAR
XML and Web Services with PHP5 and PEAR
 
Inroduction to XSLT with PHP4
Inroduction to XSLT with PHP4Inroduction to XSLT with PHP4
Inroduction to XSLT with PHP4
 
XMLT
XMLTXMLT
XMLT
 
Xml Schema
Xml SchemaXml Schema
Xml Schema
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
Transforming Xml Data Into Html
Transforming Xml Data Into HtmlTransforming Xml Data Into Html
Transforming Xml Data Into Html
 
Xslt by asfak mahamud
Xslt by asfak mahamudXslt by asfak mahamud
Xslt by asfak mahamud
 
Introduction To Xml
Introduction To XmlIntroduction To Xml
Introduction To Xml
 
XML and XSLT
XML and XSLTXML and XSLT
XML and XSLT
 
5 xsl (formatting xml documents)
5   xsl (formatting xml documents)5   xsl (formatting xml documents)
5 xsl (formatting xml documents)
 
Xml Demystified
Xml DemystifiedXml Demystified
Xml Demystified
 
XML::Liberal
XML::LiberalXML::Liberal
XML::Liberal
 
XSD
XSDXSD
XSD
 
Xml
XmlXml
Xml
 
XML Transformations With PHP
XML Transformations With PHPXML Transformations With PHP
XML Transformations With PHP
 
Sakai09 Osp Preconference Afternoon Forms
Sakai09 Osp Preconference Afternoon FormsSakai09 Osp Preconference Afternoon Forms
Sakai09 Osp Preconference Afternoon Forms
 

Mais de phanleson

Lecture 1 - Getting to know XML
Lecture 1 - Getting to know XMLLecture 1 - Getting to know XML
Lecture 1 - Getting to know XML
phanleson
 

Mais de phanleson (20)

Learning spark ch01 - Introduction to Data Analysis with Spark
Learning spark ch01 - Introduction to Data Analysis with SparkLearning spark ch01 - Introduction to Data Analysis with Spark
Learning spark ch01 - Introduction to Data Analysis with Spark
 
Firewall - Network Defense in Depth Firewalls
Firewall - Network Defense in Depth FirewallsFirewall - Network Defense in Depth Firewalls
Firewall - Network Defense in Depth Firewalls
 
Mobile Security - Wireless hacking
Mobile Security - Wireless hackingMobile Security - Wireless hacking
Mobile Security - Wireless hacking
 
Authentication in wireless - Security in Wireless Protocols
Authentication in wireless - Security in Wireless ProtocolsAuthentication in wireless - Security in Wireless Protocols
Authentication in wireless - Security in Wireless Protocols
 
E-Commerce Security - Application attacks - Server Attacks
E-Commerce Security - Application attacks - Server AttacksE-Commerce Security - Application attacks - Server Attacks
E-Commerce Security - Application attacks - Server Attacks
 
Hacking web applications
Hacking web applicationsHacking web applications
Hacking web applications
 
HBase In Action - Chapter 04: HBase table design
HBase In Action - Chapter 04: HBase table designHBase In Action - Chapter 04: HBase table design
HBase In Action - Chapter 04: HBase table design
 
HBase In Action - Chapter 10 - Operations
HBase In Action - Chapter 10 - OperationsHBase In Action - Chapter 10 - Operations
HBase In Action - Chapter 10 - Operations
 
Hbase in action - Chapter 09: Deploying HBase
Hbase in action - Chapter 09: Deploying HBaseHbase in action - Chapter 09: Deploying HBase
Hbase in action - Chapter 09: Deploying HBase
 
Learning spark ch11 - Machine Learning with MLlib
Learning spark ch11 - Machine Learning with MLlibLearning spark ch11 - Machine Learning with MLlib
Learning spark ch11 - Machine Learning with MLlib
 
Learning spark ch10 - Spark Streaming
Learning spark ch10 - Spark StreamingLearning spark ch10 - Spark Streaming
Learning spark ch10 - Spark Streaming
 
Learning spark ch09 - Spark SQL
Learning spark ch09 - Spark SQLLearning spark ch09 - Spark SQL
Learning spark ch09 - Spark SQL
 
Learning spark ch07 - Running on a Cluster
Learning spark ch07 - Running on a ClusterLearning spark ch07 - Running on a Cluster
Learning spark ch07 - Running on a Cluster
 
Learning spark ch06 - Advanced Spark Programming
Learning spark ch06 - Advanced Spark ProgrammingLearning spark ch06 - Advanced Spark Programming
Learning spark ch06 - Advanced Spark Programming
 
Learning spark ch05 - Loading and Saving Your Data
Learning spark ch05 - Loading and Saving Your DataLearning spark ch05 - Loading and Saving Your Data
Learning spark ch05 - Loading and Saving Your Data
 
Learning spark ch04 - Working with Key/Value Pairs
Learning spark ch04 - Working with Key/Value PairsLearning spark ch04 - Working with Key/Value Pairs
Learning spark ch04 - Working with Key/Value Pairs
 
Learning spark ch01 - Introduction to Data Analysis with Spark
Learning spark ch01 - Introduction to Data Analysis with SparkLearning spark ch01 - Introduction to Data Analysis with Spark
Learning spark ch01 - Introduction to Data Analysis with Spark
 
Hướng Dẫn Đăng Ký LibertaGia - A guide and introduciton about Libertagia
Hướng Dẫn Đăng Ký LibertaGia - A guide and introduciton about LibertagiaHướng Dẫn Đăng Ký LibertaGia - A guide and introduciton about Libertagia
Hướng Dẫn Đăng Ký LibertaGia - A guide and introduciton about Libertagia
 
Lecture 1 - Getting to know XML
Lecture 1 - Getting to know XMLLecture 1 - Getting to know XML
Lecture 1 - Getting to know XML
 
Lecture 4 - Adding XTHML for the Web
Lecture  4 - Adding XTHML for the WebLecture  4 - Adding XTHML for the Web
Lecture 4 - Adding XTHML for the Web
 

Último

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Último (20)

GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 

Xml For Dummies Chapter 12 Handling Transformations With Xsl it-slideshares.blogspot.com

  • 1. XML For Dummies Book Author : Lucinda Dykes & Ed Tittle Slides Prepared By: Son TN Part 4 : Transforming and Processing XML Chapter 12 : Handling Transformations with XSL
  • 2. Contents What is XSL ? The Two Faces of XSL XSL Stylesheets Are XML Documents A Simple Transformation Using XSLT An XSLT Stylesheet for Converting XML to HTML Creating an XSLT Stylesheet with XSLT Editors
  • 3. 12.1 What is XSL ? XSL is a language for expressing style sheets. An XSL style sheet is, like with CSS, a file that describes how to display an XML document of a given type. XSL is also a powerful tool for converting your XML document from one set of markup (vocabulary) to another it’s really just XML. XSL shares the functionality and is compatible with CSS2 (although it uses a different syntax) . It also adds : A transformation language for XML documents: XSLT Advanced styling features, expressed by an XML document type which defines a set of elements called Formatting Objects, and attributes
  • 4. 12.2 The Two Faces of XSL While XSL originally started out as a single language, development quickly split it into two independent components. A language for "transforming" XML documents by reorganizing and restructuring XML data trees (known as XSLT). An XML vocabulary to handle the formatting and layout of the result (now known as XSL or, sometimes, XSL-FO).
  • 6. 12.2.1.1 What is XSLT ? eXtensibleStylesheet Language for Transformations Language for transforming XML documents A programming language for XML documents A functional language, based on value substitution Augmented with pattern matching And also template substitution to construct output (based on namespaces Uses XML syntax
  • 7. 12.2.1.2 The ways to use XSLT You can use XSLT in a couple of especially intriguing ways: To transform documents described using XML elements into HTML or XHTML for display in a Web browser. In XML-based data exchange systems.
  • 8. 12.2.1.3 The ins and outs of data exchange It’s entirely possible that two (or more) systems exchanging data don’t use the exact same schema or DTD. The following are two solution to deal with this problem : First , for one system to change its internal programming to work with the other’s DTD or schema (this approach isn’t practical) Second , this approach is more practical solution, for each system to support data transformation so that each can continue to use the format it needs.
  • 9. 12.2.1.4 The role of XPath Without XPath, XSLT simply wouldn’t work. XPath is the mechanism XSLT uses to point to a piece of an XML document so that it can be transformed. If you can’t find it, you can’t change it XPath is a very sophisticated set of rules for identifying the most specific pieces of an XML document. Example : If you want to do something special to every third instance of a list item, you have to be able to point to it and that’s what XPath makes possible.
  • 11. 12.2.2.1 Overview The names of most XML elements describe the semantic meaning of the content they contain The content needs to be formatted and displayed to users There must be a step where formatting information is applied to the XML document and the semantic markup is transformed into presentation markup Cascading Style Sheets (CSS) XSL Formatting Objects (XSL-FO)
  • 12. 12.2.2.2 What is XSL-FO ? XSL-FO is a complete XML application for describing the precise layout of text on a page. XSL-FO has elements that represent pages, blocks of text on the pages, graphics, horizontal rules, and more XSLT + XSL-FO Most of the time, you write an XSLT stylesheet that transforms your document's native markup into XSL-FO There is normally a third step in which another processor transforms the XSL-FO into a third format, such as PDF and TeX
  • 13. 12.2.2.3 Categories of XSL-FO The different XSL-FO formatting objects are organized into these eight categories: Declaration, Pagination, and Layout Block Inline Table List Links and Multi Out-of-line Other Every formatting object has properties that can be applied to it. For example, the object that creates a table (fo:table) can take properties that specify how thick its borders are, what color they are, and so on.
  • 14. 12.3 XSL Stylesheets Are XML Documents XSLT stylesheet is an XML document, so: The whole stylesheet should be well-formed, the following rule is invalid: Like any XML document, contains XML declaration at the top Its documentelement is <xsl:stylesheet> which contains all the template rules <xsl:template match="/"> <trifle> <piffle> <flim> </piffle> </trifle> </xsl:template>
  • 15. 12.4 A Simple Transformation Using XSLT An XSLT stylesheet consists of instructions that tell the computer : How to convert a document described by a particular schema or DTD to a document described by a second, different schema or DTD. The best way to show you how XSLT works is by example. In the following section, we show a simple XSLT stylesheet that transforms books2.xml into an HTML document.
  • 16. 12.5 An XSLT Stylesheet for Converting XML to HTML If we open books2.xml in Internet Explorer, the program ignores all the elements it doesn’t recognize — which is all of them — and simply displays the text within the markup, as shown in Figure 12-1.
  • 17. 12.5 An XSLT Stylesheet for Converting XML to HTML (Cont) If you use the following XSLT stylesheet with books2.xml and then open it in Internet Explorer, as shown in Figure 12-2, you have a much more userfriendly and functional document for the Web
  • 18. 12.5 An XSLT Stylesheet for Converting XML to HTML (Cont) <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <head> <title>Books</title> <link rel="stylesheet" type="text/css" href="books2_xslt.css"/> </head> <body> <xsl:for-each select="books"> <table> <tr> <th>Title</th> <th>Author</th> <th>Publisher</th> <th>Price</th> <th>ISBN</th> <th>Content Type</th> <th>Format</th> </tr> <xsl:for-each select="book"> <tr> <td> <xsl:for-each select="title"> <xsl:apply-templates /> </xsl:for-each> </td> <td> <xsl:for-each select="author"> <xsl:apply-templates /> </xsl:for-each> </td> <td> <xsl:for-each select="publisher"> <xsl:apply-templates /> </xsl:for-each> </td> <td> <xsl:for-each select="price"> <xsl:apply-templates /> </xsl:for-each> </td> <td> <xsl:for-each select="isbn"> <xsl:apply-templates /> </xsl:for-each> </td> <td> <xsl:for-each select="@contentType"> <xsl:value-of select="." /> </xsl:for-each> </td> <td> <xsl:for-each select="@format"> <xsl:value-of select="." /> </xsl:for-each> </td> </tr> </xsl:for-each> </table> </xsl:for-each> </body> </html> </xsl:template> </xsl:stylesheet> Listing 12-1: The books2.xslt Stylesheet
  • 19. 12.5.1 The pieces of the stylesheet puzzle XSLT is a robust conversion tool and has many facets. In this section, we break up an XSLT stylesheet into its component parts to see what makes it do its stuff. We focus on the most basic structures that make up an XSLT stylesheet . Getting all the pieces of the XSLT puzzle right means creating and using XSLT templates to transform one set of markup (vocabulary) to another.
  • 20. 12.5.1.1 Using patterns Template rules identify the nodes to which they apply by using a pattern. To identify the node to which the template applies, XSLT uses the match attribute with the xsl:template element to point to a specific node. The value of match is called a pattern. The following is structure of XSLT Document This is followed by a template: <?xml version=”1.0” encoding=”UTF-8”?> <xsl:stylesheet version=”2.0” xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”> This template matches the XML document root (/) to the HTML document root and (as spelled out in Listing 12-1) adds the following HTML markup: <xsl:template match=”/”> <html> <meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8” /> <head> <title>Books</title> <link rel=”stylesheet” type=”text/css” href=”books2_xslt.css”/> </head> <body>
  • 21.
  • 22. 12.5.1.2 Using instructions to get results In addition to using a pattern to identify which node(s) in the source document to transform, the templatespecifies how to transform each node. These instructions guide the XSLT processor through content transformation. We use a value-of instruction with the book attributes to tell the processor to convert the result to text and place it in an HTML table cell <td> <xsl:for-each select=”@contentType”> <xsl:value-of select=”.” /> </xsl:for-each> </td>
  • 23. 12.5.2 Processing element content You use the xsl:apply-templates instruction when you want to return the content and text nodes of the current element and its children — but not the surrounding element tags. <td> <xsl:for-each select=”price”> <xsl:apply-templates /> </xsl:for-each> </td>
  • 24. 12.5.2.1 Choosing templates What template gets applied here? It depends; the XSLT processor looks for the best template to apply and makes a choice: If a select attribute is used with apply-templates, then the processor looks for the best template for that node. If no select attribute is used, the template rule that contains the apply-templates instruction is chosen as the current node. So when you create XSLT style rules, you have to think about not only the way you’re transforming an individual node, but also how to deal with its content. <td> <xsl:for-each select=”price”> <xsl:apply-templates /> </xsl:for-each> </td> In this case, the XML price element.(No select attribute is used for apply-template instruction)
  • 25.
  • 26. 12.5.2.3 Dealing with repeating elements Our XSLT stylesheet (books2.xslt) deals with repeating elements by using a single template element that contains all the instructions for transforming each instance (xsl:for-each) of these elements in our XML document. The following is a another way : Here the xsl:for-each element points at a specific element that is repeated — and applies the same transformation to each instance of that element. <xsl:template match=”books/book”> <ul> <xsl:for-each select=”publisher”> <li> <xsl:apply-templates/> </li> </xsl:for-each> </ul> </xsl:template> In this template, every publisher element nested within <books><book> . . . </book></books> is changed to an HTML list item (li) within an unordered list (ul).
  • 27. 12.6 Creating an XSLT Stylesheet with XSLT Editors If hand-coding is not your favorite art form, you can also use an XSLT editor to create your XSLT stylesheetsinstead.As example : AltovaStyleVision The following is steps how to use Altova Style Vision to deal with books2.xsd file. (Please download books2.xsd file from www.dummies.com/go/xmlfd4e )
  • 28. 12.6 Creating an XSLT Stylesheet with XSLT Editors (Cont) Open books2.xsd (or any schema document) in StyleVision.
  • 29. 12.6 Creating an XSLT Stylesheet with XSLT Editors (Cont) Select the book element from the document diagram, and drag it onto the pane on the right, onto the area labeled (contents). A menu appears, as shown in Figure 12-4.
  • 30. 12.6 Creating an XSLT Stylesheet with XSLT Editors (Cont) Choose Create Table from the displayed menu.
  • 31. 12.6 Creating an XSLT Stylesheet with XSLT Editors (Cont) In the Create Dynamic Table dialog box, deselect any attributes or children you don’t want to include in the table display and then click OK. This action creates table cells for each of the selected attributes and child elements of the book element. You can specify additional table formatting features in this dialog box: Table Growth Disply Cells As Header / Footer
  • 32. 12.6 Creating an XSLT Stylesheet with XSLT Editors (Cont) To add formatting properties, place the cursor inside the table, row, column, or cell that you want to format. Select additional formatting properties for individual cells on the StyleVisioncanvas. Click the XSLT-HTML tab at the bottom of the canvas on the right side to view the source code for the XSLT document. Choose File➪Save Generated Files➪Save Generated XSLT-HTML File.
  • 33. 12.7 Summary Picking your style of XSL Transforming XML documents with XSLT Using templates for style Creating an XSLT stylesheet with an XSLT editor