Introduction to XSL Hudson Ummem Veloso huv Luciano

  • Slides: 37
Download presentation
Introduction to XSL Hudson Ummem Veloso (huv) Luciano de Moura Silva (lms 4)

Introduction to XSL Hudson Ummem Veloso (huv) Luciano de Moura Silva (lms 4)

Introduction • What is XSL? – XSL stands for EXtensible Stylesheet Language – XSL

Introduction • What is XSL? – XSL stands for EXtensible Stylesheet Language – XSL is a style sheet language applied in archives XML • But what is a Style Sheet? – A list of page format specifications, including typographic and layout specifications, that, when applied to structured information, provides a particular rendering of that information. – Different style sheets may be applied to the same piece of structured information to produce different presentations of the information. 12/02/2005 2

Introduction • How XSL will be used in the Brazil. IP? – Formatting of

Introduction • How XSL will be used in the Brazil. IP? – Formatting of archives XML – Create the PDF files for documentation – Create the HTML files to make use in the Internet • Why to use XML and XSL? – The XML validation can be automatized – The XSL is recommended for W 3 C formatting the XML files – The XSL is more powerfull than CSS 12/02/2005 3

Introduction • XSL components – XSLT - a language for transforming XML documents –

Introduction • XSL components – XSLT - a language for transforming XML documents – XPath - a language for navigating in XML documents – XSL-FO - a language formatting XML documents 12/02/2005 4

XSLT • What is XSLT? – XSLT stands for XSL Transformations – XSLT transforms

XSLT • What is XSLT? – XSLT stands for XSL Transformations – XSLT transforms an XML document into another XML document – XSLT uses XPath to navigate in XML documents 12/02/2005 5

XSLT • How does it works – In the transformation process, XSLT uses XPath

XSLT • How does it works – In the transformation process, XSLT uses XPath to define parts of the source document that should match one or more predefined templates. When a match is found, XSLT will transform the matching part of the source document into the result document. encoding="ISO-8859 -1"? > <? xml version="1. 0" <xsl: stylesheet version="1. 0" xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform"> <xsl: template match="/"> <html> <body> <h 1>Brazil IP</h 1>. . . </body> </html> </xsl: template> </xsl: stylesheet> 12/02/2005 6

XSLT – Transformation • How to create a HTML document with the following XML

XSLT – Transformation • How to create a HTML document with the following XML document? <? xml version="1. 0" encoding="ISO-8859 -1"? > <? xml-stylesheet type="text/xsl" href="exemplo 1. xsl"? > <Document> <Title>Brazil IP</Title> <Project>Fênix</Project> <Group. Name>Configuração</Group. Name> <Leader id = ‘ 1’>rgo</Leader> <Leader id = ‘ 2’>drm</Leader> <Numberof. Members>5</Numberof. Members> </Group> … </Document> 12/02/2005 7

XSLT - Transformation • Create the XSL template <? xml version=“ 1. 0” encoding=“ISO-8859

XSLT - Transformation • Create the XSL template <? xml version=“ 1. 0” encoding=“ISO-8859 -1”? > <xsl: stylesheet version=“ 1. 0” xmlns: xsl=“http: //www. w 3. org/1999/XSL/Transform”> <xsl: template match=“/”> <html> <body> <h 1><xsl: value-of select = “Document/Title”/></h 1>< br/> <h 1><xsl: value-of select = “Document/Project”/></h 1>< br/> <xsl: for-each select = “Document/Group”> Group. Name: <xsl: value-of select=“Group. Name”/> Numberof. Members: <xsl: value-of select=“Number. Of. Members”/><br/> </xsl: for-each> </body> </html> </xsl: template> </xsl: stylesheet> example 1. xsl (open the example. xml with browser) 12/02/2005 8

XSLT – <value-of> • The <xsl: value-of> element is used to extract the value

XSLT – <value-of> • The <xsl: value-of> element is used to extract the value of a selected nodeencoding=“ISO-8859 -1”? > <? xml version=“ 1. 0” <xsl: stylesheet version=“ 1. 0” xmlns: xsl=“http: //www. w 3. org/1999/XSL/Transform”> <xsl: template match=“/”> <html> <body> <h 1><xsl: value-of select = “Document/Title”/> </h 1><br/> <h 1><xsl: value-of select = “Document/Project”/> </h 1><br/> <xsl: for-each select = “Document/Group”> Group. Name: <xsl: value-of select=“Group. Name”/><br/> Leader. One: <xsl: value-of select=“Leader[@id = '1']”/> <br/> <!-- Value of the select attribute is an XPath element --> </xsl: for-each> </body> </html> </xsl: template> </xsl: stylesheet> 12/02/2005 9

XSLT - <for-each> • The <xsl: for-each> element allows you to do looping in

XSLT - <for-each> • The <xsl: for-each> element allows you to do looping in XSLT <? xml version=“ 1. 0” encoding=“ISO-8859 -1”? > <xsl: stylesheet version=“ 1. 0” xmlns: xsl=“http: //www. w 3. org/1999/XSL/Transform”> <xsl: template match=“/”> <html> <body> <h 1><xsl: value-of select = “Document/Title”/></h 1>< br/> <h 1><xsl: value-of select = “Document/Project”/></h 1>< br/> <!-- for-each for select only the Groups with 7 members --> <xsl: for-each select = "Document/Group[ Numberof. Members = 7]"> Group. Name: <xsl: value-of select=“Group. Name”/><br/> Numberof. Members: <xsl: value-of select=“Numberof. Members”/><br/> </xsl: for-each> </body> </html> </xsl: template> </xsl: stylesheet> 12/02/2005 Modif link (href) in the example. xml for example 3. xsl and view with browser 10

XSLT - <xsl: sort> • The <xsl: sort> element is used to sort the

XSLT - <xsl: sort> • The <xsl: sort> element is used to sort the output. <? xml version=“ 1. 0” encoding=“ISO-8859 -1”? > <xsl: stylesheet version=“ 1. 0” xmlns: xsl=“http: //www. w 3. org/1999/XSL/Transform”> <xsl: template match=“/”> <html> <body> <h 1><xsl: value-of select = “Document/Title”/></h 1>< br/> <h 1><xsl: value-of select = “Document/Project”/></h 1>< br/> <xsl: for-each select = “Document/Group”> <xsl: sort select=“Leader[@id=‘ 2’]” order=“ascending” case-order=“lower-first”/> Group. Name: <xsl: value-of select=“Group. Name”/><br/> Numberof. Members: <xsl: value-of select=“Numberof. Members”/><br/> </xsl: for-each> </body> </html> </xsl: template> </xsl: stylesheet> 12/02/2005 Modif link (href) in the example. xml for example 4. xsl and view with browser 11

XSLT - <xsl: if> • The <xsl: if> element is used to put a

XSLT - <xsl: if> • The <xsl: if> element is used to put a conditional test against the content of the XML file. – Syntaxe: <xsl: if test=“expression”>. . . some output if the expression is true. . . </xsl: if> • Where to put the <xsl: if> element? – To add a conditional test, add the <xsl: if> element inside the <xsl: for-each> element in the XSL file: 12/02/2005 12

XSLT - <xsl: if> • View the example above <? xml version=“ 1. 0”

XSLT - <xsl: if> • View the example above <? xml version=“ 1. 0” encoding=“ISO-8859 -1”? > <xsl: stylesheet version=“ 1. 0” xmlns: xsl=“http: //www. w 3. org/1999/XSL/Transform”> <xsl: template match=“/”> <html> <body> <h 1><xsl: value-of select = “Document/Title”/></h 1>< br/> <h 1><xsl: value-of select = “Document/Project”/></h 1>< br/> <xsl: for-each select = “Document/Group”> <xsl: if test=“Numberof. Members > 5”> Group. Name: <xsl: value-of select=“Group. Name”/><br/> </xsl: if> </xsl: for-each> </body> </html> </xsl: template> </xsl: stylesheet> 12/02/2005 Modif link (href) in the example. xml for example 5. xsl and view with browser 13

XSLT - <xsl: choose> • The <xsl: choose> element is used in conjunction with

XSLT - <xsl: choose> • The <xsl: choose> element is used in conjunction with <xsl: when> and <xsl: otherwise> to express multiple conditional tests. • The <xls: choose> element is used for simulation of if then else structure <xsl: choose> <xsl: when test="expression"> – Syntaxe : . . . some output. . . </xsl: when> <xsl: otherwise>. . . some output. . </xsl: otherwise> </xsl: choose> • Where to put the Choose Condition? 12/02/2005 – To insert a conditional choose test against the content of the XML file, add the <xsl: choose>, <xsl: when>, and <xsl: otherwise> elements to the XSL file: 14

XSLT - <xsl: choose> <? xml version=“ 1. 0” encoding=“ISO-8859 -1”? > <xsl: stylesheet

XSLT - <xsl: choose> <? xml version=“ 1. 0” encoding=“ISO-8859 -1”? > <xsl: stylesheet version=“ 1. 0” xmlns: xsl=“http: //www. w 3. org/1999/XSL/Transform”> <xsl: template match=“/”> <html> <body> <h 1><xsl: value-of select = “Document/Title”/></h 1><br/> <h 1><xsl: value-of select = “Document/Project”/></h 1><br/> <xsl: for-each select = “Document/Group”> <xsl: choose> <xsl: when test=“Numberof. Members < 8”> Group. Name: <xsl: value-of select=“Group. Name”/><br/> </xsl: when> <xsl: otherwise> Group. Name: <span style=“color: red”><xsl: value-of select=“Group. Name”/></span><br/> </xsl: otherwise> </xsl: choose> </xsl: for-each> </body> </html> </xsl: template> </xsl: stylesheet> 12/02/2005 Modif link (href) in the example. xml for example 6. xsl and view with browser 15

XSLT - <xsl: apply-templates> • The <xsl: apply-templates> element applies a template to the

XSLT - <xsl: apply-templates> • The <xsl: apply-templates> element applies a template to the current element or to the current element's child nodes. <? xml version=“ 1. 0” encoding=“ISO-8859 -1”? > <xsl: stylesheet version=“ 1. 0” xmlns: xsl=“http: //www. w 3. org/1999/XSL/Transform”> <xsl: template match=“/”> <html><body> <h 2><xsl: value-of select=“Document/Title”/></h 2><br/> <xsl: apply-templates/> </body></html> </xsl: template> <xsl: template match=“Group”> <xsl: apply-templates select=“Group. Name”/> </xsl: template> <xsl: template match=“Group. Name”> Group. Name: <span style=“color: blue”><xsl: value-of select=“. ”/></span><br/> </xsl: template> </xsl: stylesheet> Modif link (href) in the example. xml for example 7. xsl and view with browser 12/02/2005 16

XPath and XSLT – basics functions • Context functions: <? xml version=“ 1. 0”

XPath and XSLT – basics functions • Context functions: <? xml version=“ 1. 0” encoding=“ISO-8859 -1”? > <xsl: stylesheet version=“ 1. 0” xmlns: xsl=“http: //www. w 3. org/1999/XSL/Transform”> <xsl: template match=“/”> <html> <body> <h 1><xsl: value-of select = “Document/Title”/></h 1>< br/> <h 1><xsl: value-of select = “Document/Project”/></h 1>< br/> Group. Name: <xsl: value-of select=“Document/Group[position()=3]/Group. Name”/><br/> Numberof. Members: <xsl: value-of select=“Document/Group[last()]/Numberof. Members”/><br/> </body> </html> </xsl: template> </xsl: stylesheet> Modif link (href) in the example. xml for example 8. xsl and view with browser 12/02/2005 17

XPath and XSLT – basics functions • Functions on Boolean values: <? xml version=“

XPath and XSLT – basics functions • Functions on Boolean values: <? xml version=“ 1. 0” encoding=“ISO-8859 -1”? > <xsl: stylesheet version=“ 1. 0” xmlns: xsl=“http: //www. w 3. org/1999/XSL/Transform”> <xsl: template match=“/”> <html> <body> <h 1><xsl: value-of select = “Document/Title”/></h 1>< br/> <h 1><xsl: value-of select = “Document/Project”/></h 1>< br/> <xsl: for-each select = “Document/Group[ not(position()=2)]”> Group. Name: <xsl: value-of select=“Group. Name”/><br/> Numberof. Members: <xsl: value-of select=“Numberof. Members”/><br/> </xsl: for-each> </body> </html> </xsl: template> </xsl: stylesheet> Modif link (href) in the example. xml for example 9. xsl and view with browser 12/02/2005 18

XPath and XSLT – basics functions • Arithmetic functions: – sum(node set) - this

XPath and XSLT – basics functions • Arithmetic functions: – sum(node set) - this function sums up all the values in the set of nodes <? xml version=“ 1. 0” encoding=“ISO-8859 -1”? > <xsl: stylesheet version=“ 1. 0” xmlns: xsl=“http: //www. w 3. org/1999/XSL/Transform”> <xsl: template match=“/”> <html><body> <h 1><xsl: value-of select = “Document/Project”></h 1>< br/> Numberof. Members in the USB host project: < xsl: value-of select=“sum(//Document/Group/Numberof. Members)”/> </body></html> </xsl: template> </xsl: stylesheet> <!-- Modif link(href) in the example. xml for example 10. xsl --> – floor(number) returns the largest integer that is not greater than number • Example: floor(2. 5) returns 2 – ceiling(number) returns the smallest integer that is not less than number • Example. Ceiling(2. 5) returns 3 – round(number) returns the integer closest to number • Example. round(2. 3) returns 2 12/02/2005 19

XPath and XSLT – basics functions • String functions: – string(arg) - returns the

XPath and XSLT – basics functions • String functions: – string(arg) - returns the string value of the argument. The argument could be a number, boolean, or node-set • Example: string(314) – Result: “ 314” – string-length(string) - returns the length of the specified string. • Example: string-lenght(‘Brazil-IP’) – Result: 9 – concat(string, . . . ) - returns the concatenation of the strings • Example: concat(‘Brazil’, ‘-’, ’IP’, ) – Result: “Brazil-IP” – translate(string 1, string 2, string 3) – Converts string 1 by replacing the characters in string 2 with the characters in string 3 • Example: translate('12: 30', ‘ 0123', ‘abcd') – Result: “bc: da” 12/02/2005 20

XPath and XSLT – basics functions <? xml version=“ 1. 0” encoding=“ISO-8859 -1”? >

XPath and XSLT – basics functions <? xml version=“ 1. 0” encoding=“ISO-8859 -1”? > <xsl: stylesheet version=“ 1. 0” xmlns: xsl=“http: //www. w 3. org/1999/XSL/Transform”> <xsl: template match="/"> <html> <body> <h 1><xsl: value-of select = “translate(Document/Title, ’ABCDEF’, ’abcdef’)”/></h 1><br/> <h 1><xsl: value-of select = “concat(Document/Project, ' USB', ' 2. 0')”/></h 1><br/> <table border=“ 1” cellpadding=“ 5” cellspacing=“ 0”> <tr> <th>Group Name</th> <th>Leader 1</th> <th>Leader 2</th> <th>Length String</th> </tr> <xsl: for-each select = “Document/Group”> <tr> <td><xsl: value-of select=“Group. Name”/></td> <td><xsl: value-of select=“concat(Leader[position()=1], ' is', ' the', ' first', '!')”/></td> <td><xsl: value-of select=“concat(Leader[position()=2], ' is', ' the', ' second', '!')”/></td> <td align=“center”><xsl: value-of select=“string-length(Group. Name)”/></td> </tr> </xsl: for-each> </table> </body> </html> </xsl: template> </xsl: stylesheet> Modif link (href) in the example. xml for example 11. xsl and view with browser 12/02/2005 21

XSLT - elements • <xsl: atributte> – The <xsl: attribute> element is used to

XSLT - elements • <xsl: atributte> – The <xsl: attribute> element is used to add attributes to elements. <? xml version=“ 1. 0” encoding=“ISO-8859 -1”? > <xsl: stylesheet version=“ 1. 0” xmlns: xsl=“http: //www. w 3. org/1999/XSL/Transform”> <xsl: template match=“/”> <html> <head><title><xsl: value-of select=“Document/Title”/></title></head> <body> <table border=“ 1” width=“ 50%”> <xsl: for-each select=“/Document/Group”> <tr> <!--coloring alternate rows--> <xsl: if test=“position() mod 2 = 0”> <xsl: attribute name=“bgcolor ”>yellow</xsl: attribute> </ xsl: if> <td align=“center”>< xsl: value-of select=“Group. Name"/></td> </tr> </xsl: for-each> </table> </body> </html> </xsl: template> </xsl: stylesheet> Modif link (href) in the example. xml for example 12. xsl and view with browser 12/02/2005 22

XSLT - elements • <xsl: message> – The <xsl: message> element writes a message

XSLT - elements • <xsl: message> – The <xsl: message> element writes a message to the output. This element is primarily used to report errors <? xml version=“ 1. 0” enconding=“ISO-8859 -1”? > <xsl: stylesheet version=“ 1. 0” xmlns: xsl=“http: //www. w 3. org/1999/XSL/Transform”> <xsl: template match=“/”> <html> <head><title><xsl: value-of select=“Document/Title”/></title></head> <body> <table border=“ 1” width=“ 50%”> <xsl: for-each select=“Document/Group”> <tr> <xsl: if test=“position() mod 2 = 0”> <xsl: message terminate=“yes”>This line is pair</xsl: message> </xsl: if> <td><xsl: value-of select=“Group. Name”/></td> </tr> </xsl: for-each> </table> </body> </html> </xsl: template> </xsl: stylesheet> Modif link (href) in the example. xml for example 13. xsl and view with browser 12/02/2005 23

XSLT • XSLT Processors – Saxon (http: //saxon. sourceforge. net/) is a free processor

XSLT • XSLT Processors – Saxon (http: //saxon. sourceforge. net/) is a free processor written in Java – Xalan (http: //xml. apache. org/xalan-j/index. html) is part of the Apache XML Project. It has versions written in both Java and C++, both of them free – xsltproc (http: //xmlsoft. org/XSLT/) processor is written in C. It is free and is considered the fastest of the processors – Microsoft's MSXML (http: //msdn. microsoft. com/xml/) engine includes an XSLT processor. It is reported to be fast, but only runs on Windows 12/02/2005 24

XSLT • How to generate a HTML file from a XML and a XSL

XSLT • How to generate a HTML file from a XML and a XSL files – First we have to get the fop. zip from Apache. org – Then use the xalan command xalan -in input. File. xml -out output. File. html 12/02/2005 25

XSL-FO • What is XSL-FO? – XSL-FO stands for Extensible Stylesheet Language Formatting Objects

XSL-FO • What is XSL-FO? – XSL-FO stands for Extensible Stylesheet Language Formatting Objects – XSL-FO is an XML-based markup language describing the formatting of XML data for output to screen, paper or other media. 12/02/2005 26

XSL-FO • XSL-FO document structure <? xml version="1. 0" encoding="ISO-8859 -1"? > <fo: root

XSL-FO • XSL-FO document structure <? xml version="1. 0" encoding="ISO-8859 -1"? > <fo: root xmlns: fo="http: //www. w 3. org/1999/XSL/Format"> <fo: layout-master-set> <fo: simple-page-master-name="A 4"> <!-- Page template goes here --> </fo: simple-page-master> </fo: layout-master-set> <fo: page-sequence master-reference="A 4"> <!-- Page content goes here --> </fo: page-sequence> </fo: root> 12/02/2005 27

XSL-FO • XSL-FO Areas – XSL-FO uses rectangules boxes (areas) to display output •

XSL-FO • XSL-FO Areas – XSL-FO uses rectangules boxes (areas) to display output • • • 12/02/2005 Pages Regions Block areas Line areas Inline areas 28

XSL-FO • XSL-FO Areas – Pages contain • • • margin-top margin-bottom margin-left margin-right

XSL-FO • XSL-FO Areas – Pages contain • • • margin-top margin-bottom margin-left margin-right Regions – Regions contain • • • 12/02/2005 region-body region-before region-after region-start region-end Block areas 29

XSL-FO 12/02/2005 30

XSL-FO 12/02/2005 30

XSL-FO • XSL-FO Areas – Block Areas • Block areas defines small block elements

XSL-FO • XSL-FO Areas – Block Areas • Block areas defines small block elements like paragraphs, tables and lists • Can contain other Block areas, but most often they contain Line areas – Line Areas • Define text lines inside Block areas • Contain Inline areas – Inline Areas • Define text inside Lines (bullets, single character, graphics) 12/02/2005 31

XSL-FO • A real XSL-FO example <? xml version="1. 0" encoding="ISO-8859 -1"? > <fo:

XSL-FO • A real XSL-FO example <? xml version="1. 0" encoding="ISO-8859 -1"? > <fo: root xmlns: fo="http: //www. w 3. org/1999/XSL/Format"> <fo: layout-master-set> <fo: simple-page-master-name="master"> </fo: simple-page-master> </fo: layout-master-set> <fo: page-sequence master-reference="master"> <fo: flow-name="xsl-region-body"> <fo: block>Hello Brazil_IP</fo: block> </fo: flow> </fo: page-sequence> </fo: root> 12/02/2005 32

XSL-FO • Templates <fo: layout-master-set> <fo: simple-page-master-name="capa" page-width="297 mm" page-height="210 mm" margin-top="3 cm" margin-bottom="3

XSL-FO • Templates <fo: layout-master-set> <fo: simple-page-master-name="capa" page-width="297 mm" page-height="210 mm" margin-top="3 cm" margin-bottom="3 cm" margin-left="2 cm" margin-right="2 cm"> </fo: simple-page-master> <fo: simple-page-master-name="A 4" page-width="297 mm" page-height="210 mm" margin-top="1 cm" margin-bottom="1 cm" margin-left="1 cm" margin-right="1 cm"> <fo: region-before extent="2 cm"/> <fo: region-after extent="2 cm"/> <fo: region-start extent="2 cm"/> <fo: region-end extent="2 cm"/> </fo: simple-page-master> </fo: layout-master-set> 12/02/2005 33

XSL-FO • XSL-FO Processors – XSL Formatter (http: //www. antennahouse. com/) – Xinc Beta

XSL-FO • XSL-FO Processors – XSL Formatter (http: //www. antennahouse. com/) – Xinc Beta Release (http: //www. lunasil. com/) A Swing based XSLFO viewer allows you to view and print XSL-FO files as well as generate PDF files with the click of a button. – FOP (http: //xmlgraphics. apache. org/fop/) It is a free Java application that reads a formatting object (FO) tree and renders the resulting pages to a specified output. 12/02/2005 34

XSL-FO • How to generate a PDF file from a XML and a XSL

XSL-FO • How to generate a PDF file from a XML and a XSL files – Use the xalan command to create the FO file – Then use the fop comand to generate the PDF xalan -in input. File. xml -out output. File. fo fop -fo input. File. fo -pdf output. File. pdf 12/02/2005 35

XSL-FO • You also can generate a PS file – Use the xalan command

XSL-FO • You also can generate a PS file – Use the xalan command to create the FO file – Then use the fop comand to generate the PS xalan -in input. File. xml -out output. File. fo fop -fo input. File. fo -ps output. File. ps 12/02/2005 36

 • Study Bibliography – http: //www. w 3 schools. com • This site

• Study Bibliography – http: //www. w 3 schools. com • This site have some tutorials about XML, HTML, CSS and XSL for beginners. – http: //www. dpawson. co. uk/xsl/ • A reference guide about XSL. – http: //www. antennahouse. com/XSLsample. htm • A guide to generate PDF files from a XSL-FO. – http: //www. zvon. org/xxl/ • A complete source with tutorials an reference guides about XML, XSLT, XSL-FO, CSS and others. – http: //xmlgraphics. apache. org/fop/download. html • Where to download the FOP XSL-FO Processor. 12/02/2005 37