1 / 37

Introduction to XSL

Introduction to XSL. Hudson Ummem Veloso (huv) Luciano de Moura Silva (lms4). Introduction. What is XSL? XSL stands for E X tensible S tylesheet L anguage XSL is a style sheet language applied in archives XML But what is a Style Sheet?

sheryl
Download Presentation

Introduction to XSL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to XSL Hudson Ummem Veloso (huv) Luciano de Moura Silva (lms4)

  2. 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.

  3. Introduction • How XSL will be used in the BrazilIP? • 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 W3C for formatting the XML files • The XSL is more powerfull than CSS

  4. Introduction • XSL components • XSLT - a language for transforming XML documents • XPath - a language for navigating in XML documents • XSL-FO - a language for formatting XML documents

  5. 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

  6. 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. <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h1>Brazil IP</h1> ... </body> </html> </xsl:template> </xsl:stylesheet>

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

  8. XSLT - Transformation • Create the XSL template example1.xsl (open the example.xml with browser) <?xml version=“1.0” encoding=“ISO-8859-1”?> <xsl:stylesheet version=“1.0” xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”> <xsl:template match=“/”> <html> <body> <h1><xsl:value-of select = “Document/Title”/></h1><br/> <h1><xsl:value-of select = “Document/Project”/></h1><br/> <xsl:for-each select = “Document/Group”> GroupName: <xsl:value-of select=“GroupName”/><br> NumberofMembers: <xsl:value-of select=“NumberOfMembers”/><br/> </xsl:for-each> </body> </html> </xsl:template> </xsl:stylesheet>

  9. XSLT – <value-of> • The<xsl:value-of> element is used to extract the value of a selected node Modif link (href) in the example.xml for example2.xsl and view with browser <?xml version=“1.0” encoding=“ISO-8859-1”?> <xsl:stylesheet version=“1.0” xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”> <xsl:template match=“/”> <html> <body> <h1><xsl:value-of select = “Document/Title”/></h1><br/> <h1><xsl:value-of select = “Document/Project”/></h1><br/> <xsl:for-each select = “Document/Group”> GroupName: <xsl:value-of select=“GroupName”/><br/> LeaderOne: <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>

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

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

  12. XSLT - <xsl:if> • The <xsl:if> element is used to put a conditional test against the contentof the XML file. • Syntaxe: • 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: <xsl:if test=“expression”> ... ...some output if the expression is true... ... </xsl:if>

  13. XSLT - <xsl:if> • View the example above Modif link (href) in the example.xml for example5.xsl and view with browser <?xml version=“1.0” encoding=“ISO-8859-1”?> <xsl:stylesheet version=“1.0” xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”> <xsl:template match=“/”> <html> <body> <h1><xsl:value-of select = “Document/Title”/></h1><br/> <h1><xsl:value-of select = “Document/Project”/></h1><br/> <xsl:for-each select = “Document/Group”> <xsl:if test=“NumberofMembers &gt; 5”> GroupName: <xsl:value-of select=“GroupName”/><br/> </xsl:if> </xsl:for-each> </body> </html> </xsl:template> </xsl:stylesheet>

  14. 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 • Syntaxe: • Where to put the Choose Condition? • 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: <xsl:choose> <xsl:when test="expression"> ... some output ... </xsl:when> <xsl:otherwise> ... some output .... </xsl:otherwise> </xsl:choose>

  15. XSLT - <xsl:choose> Modif link (href) in the example.xml for example6.xsl and view with browser <?xml version=“1.0” encoding=“ISO-8859-1”?> <xsl:stylesheet version=“1.0” xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”> <xsl:template match=“/”> <html> <body> <h1><xsl:value-of select = “Document/Title”/></h1><br/> <h1><xsl:value-of select = “Document/Project”/></h1><br/> <xsl:for-each select = “Document/Group”> <xsl:choose> <xsl:when test=“NumberofMembers &lt; 8”> GroupName: <xsl:value-of select=“GroupName”/><br/> </xsl:when> <xsl:otherwise> GroupName: <span style=“color:red”><xsl:value-of select=“GroupName”/></span><br/> </xsl:otherwise> </xsl:choose> </xsl:for-each> </body> </html> </xsl:template> </xsl:stylesheet>

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

  17. XPath and XSLT – basics functions • Context functions: Modif link (href) in the example.xml for example8.xsl and view with browser <?xml version=“1.0” encoding=“ISO-8859-1”?> <xsl:stylesheet version=“1.0” xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”> <xsl:template match=“/”> <html> <body> <h1><xsl:value-of select = “Document/Title”/></h1><br/> <h1><xsl:value-of select = “Document/Project”/></h1><br/> GroupName: <xsl:value-of select=“Document/Group[position()=3]/GroupName”/><br/> NumberofMembers: <xsl:value-of select=“Document/Group[last()]/NumberofMembers”/><br/> </body> </html> </xsl:template> </xsl:stylesheet>

  18. XPath and XSLT – basics functions • Functions on Boolean values: Modif link (href) in the example.xml for example9.xsl and view with browser <?xml version=“1.0” encoding=“ISO-8859-1”?> <xsl:stylesheet version=“1.0” xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”> <xsl:template match=“/”> <html> <body> <h1><xsl:value-of select = “Document/Title”/></h1><br/> <h1><xsl:value-of select = “Document/Project”/></h1><br/> <xsl:for-each select = “Document/Group[not(position()=2)]”> GroupName: <xsl:value-of select=“GroupName”/><br/> NumberofMembers: <xsl:value-of select=“NumberofMembers”/><br/> </xsl:for-each> </body> </html> </xsl:template> </xsl:stylesheet>

  19. XPath and XSLT – basics functions • Arithmetic functions: • sum(node set) - this function sums up all the values in the set of nodes • 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 <?xml version=“1.0” encoding=“ISO-8859-1”?> <xsl:stylesheet version=“1.0” xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”> <xsl:template match=“/”> <html><body> <h1><xsl:value-of select = “Document/Project”></h1><br/> NumberofMembers in the USB host project: <xsl:value-of select=“sum(//Document/Group/NumberofMembers)”/> </body></html> </xsl:template> </xsl:stylesheet> <!-- Modif link(href) in the example.xml for example10.xsl -->

  20. 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,string,...) - returns the concatenation of the strings • Example: concat(‘Brazil’,‘-’,’IP’,) • Result: “Brazil-IP” • translate(string1,string2,string3) – Converts string1 by replacing the characters in string2 with the characters in string3 • Example: translate('12:30',‘0123',‘abcd') • Result: “bc:da”

  21. XPath and XSLT – basics functions <?xml version=“1.0” encoding=“ISO-8859-1”?> <xsl:stylesheet version=“1.0” xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”> <xsl:template match="/"> <html> <body> <h1><xsl:value-of select = “translate(Document/Title,’ABCDEF’,’abcdef’)”/></h1><br/> <h1><xsl:value-of select = “concat(Document/Project,' USB', ' 2.0')”/></h1><br/> <table border=“1” cellpadding=“5” cellspacing=“0”> <tr> <th>Group Name</th> <th>Leader1</th> <th>Leader2</th> <th>Length String</th> </tr> <xsl:for-each select = “Document/Group”> <tr> <td><xsl:value-of select=“GroupName”/></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(GroupName)”/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> • Modif link (href) in the example.xml for example11.xsl and view with browser

  22. 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.w3.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=“GroupName"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> • Modif link (href) in the example.xml for example12.xsl and view with browser

  23. 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.w3.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=“GroupName”/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> • Modif link (href) in the example.xml for example13.xsl and view with browser

  24. 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

  25. 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 inputFile.xml -out outputFile.html

  26. 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.

  27. XSL-FO • XSL-FO document structure <?xml version="1.0" encoding="ISO-8859-1"?> <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> <fo:layout-master-set> <fo:simple-page-master master-name="A4"> <!-- Page template goes here --> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="A4"> <!-- Page content goes here --> </fo:page-sequence> </fo:root>

  28. XSL-FO • XSL-FO Areas • XSL-FO uses rectangules boxes (areas) to display output • Pages • Regions • Block areas • Line areas • Inline areas

  29. XSL-FO • XSL-FO Areas • Pages contain • margin-top • margin-bottom • margin-left • margin-right • Regions • Regions contain • region-body • region-before • region-after • region-start • region-end • Block areas

  30. XSL-FO

  31. 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)

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

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

  34. XSL-FO • XSL-FO Processors • XSL Formatter (http://www.antennahouse.com/) • Xinc Beta Release (http://www.lunasil.com/) A Swing based XSL-FO 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.

  35. 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 inputFile.xml -out outputFile.fo fop -fo inputFile.fo -pdf outputFile.pdf

  36. 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 inputFile.xml -out outputFile.fo fop -fo inputFile.fo -ps outputFile.ps

  37. Study Bibliography • http://www.w3schools.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/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.

More Related