1 / 34

XSL Transformation

XSL Transformation. Houssam Haitof. XSL. XSL (e X tensible S tylesheet L anguage) is a language for expressing style sheets. What is a Stylesheet?. A stylesheet is a file which contains a set of rules for converting an XML document into another document, which can be XML, HTML, plain text ….

Download Presentation

XSL Transformation

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. XSL Transformation Houssam Haitof Houssam Haitof

  2. XSL • XSL (eXtensible Stylesheet Language) is a language for expressing style sheets. Houssam Haitof

  3. What is a Stylesheet? • A stylesheet is a file which contains a set of rules for converting an XML document into another document, which can be XML, HTML, plain text … Houssam Haitof

  4. Style Sheet Specifications • XSL - Extensible Style Language • Combines features of DSSSL and CSS using XML syntax • DSSSL - Document Style and Semantics Specification Language • An international SGML standard for Scheme-like languages for style sheets and document conversion • CSS - Cascading Style Sheet Specification • Simple syntax for assigning styles to elements (in some HTML browsers) • XSLT is an XML Document! Houssam Haitof

  5. XSL • It consists of three parts: • XSL Transformations (XSLT): a language for transforming XML documents • An XML vocabulary for specifying formatting semantics (XSL Formatting Objects) • An expression language used by XSLT to access or refer to parts of an XML document (XPATH) Houssam Haitof

  6. XSL-FO • Used to describe XML documents for display • W3C recommendation 15 October 2001 • A language for completely describing a styled document (content organization, styling, layouts and layout-selection rules) • Everything needed to format and paginate a document! Houssam Haitof

  7. XPath • Used in conjunction with XSLT • Became a W3C recommendation 16 Nov 1999 • Provides a language for pointing to different parts of an XML document • specifies location path from specific context nodes (i.e. starting points) Houssam Haitof

  8. XPath Expressions • Addresses a specific part or parts of XML documents which then allows XSLT to transform them • Provides expressions for manipulation of strings, Booleans, numbers so that the XSL elements can act upon them Houssam Haitof

  9. XSLT • XSLT is the most important part of the XSL Standard. • It is the part of XSL that is used to transform an XML document into another XML document, or another type of document (text, java code, …) Houssam Haitof

  10. XSLT • XSLT can be used to manipulate an XML document • Can transform/extract information as a programming language can • Can be used like a database language to extract information • Uses XML syntax Houssam Haitof

  11. XSLT is rule-based • XSLT is based on template rules which specify how XML documents should be processed. • Template rules can be based in any order because XSLT is a declarative language. • The stylesheet declares what output should be produced when a pattern in the XML document is matched. Houssam Haitof

  12. Example • A stylesheet could declare that when the XSLT transformation engine finds a 'NAME' element it should add markup by calling the 'NAME' template. The template syntax is: <xsl:template match="NAME">    ... </xsl:template> Houssam Haitof

  13. XSLT Capabilities • Add prefix or suffix text to content • Remove, create, re-order and sort elements • Re-use elements elsewhere in the document • Transform data between XML formats • Uses a recursive mechanism to go through document Houssam Haitof

  14. XSLT consists of two parts • A source document • original xml document • A result tree • newly-created xml, html or a plain text document • A common way to describe the transformation process is to say that XSL uses XSLT to transform an XML source tree into an XML result tree. Houssam Haitof

  15. Trees & Nodes • With XSLT one does not think in terms of documents, but in terms of trees & nodes • A tree represents the data in a document as a set of nodes • Nodes are elements, attributes, comments, etc in a hierarchy Houssam Haitof

  16. Structure of Style Sheet • style sheet • Style sheet is made up of templates • Templates have other XSL elements/XPath expressions • Style sheet is usually given extension *.xsl • Must be well-formed XML • XSLT has a number of pre-defined elements to perform certain transformations • <xsl:stylesheet> • <xsl:template> • <xsl:value-of> • <xsl:if> • <xsl:choose> • … Houssam Haitof

  17. A Sample XML file <?xml version="1.0"?> <!-- File Name: XslDemo01.xml --> <?xml-stylesheet type="text/xsl" href="XslDemo01.xsl"?> <BOOKS> <BOOK> <TITLE>Moby-Dick</TITLE> <AUTHOR> <FIRSTNAME>Herman</FIRSTNAME> <LASTNAME>Melville</LASTNAME> </AUTHOR> <BINDING>hardcover</BINDING> <PAGES>724</PAGES> <PRICE>$9.95</PRICE> </BOOK> … </BOOKS> Houssam Haitof

  18. A simple transformation of an XML file Book Description Author: Herman Melville Title: Moby-Dick Price: $9.95 Binding type: hardcover Number of pages: 724 Author: John Doe Title: Tales Price: $19.95 Binding type: softcover Number of pages: 450 Houssam Haitof

  19. <?xml version ="1.0"?> <xsl:stylesheet version ="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <HTML> <HEAD><TITLE>XSLT Test</TITLE></HEAD> <H2>Book Description</H2> <xsl:for-each select=“/BOOKS/BOOK”> <Span Style="font-style:italic">Author: </Span> <xsl:value-of select=“./AUTHOR"/><br/> <span style="font-style:italic">Title: </span> <xsl:value-of select=“./TITLE"/><br/> <span style="font-style:italic">Price: </span> <xsl:value-of select=“./PRICE"/><br/> <span style="font-style:italic">Binding: </span> <xsl:value-of select=“./BINDING"/><br/> <span style="font-style:italic">Number of Pages: </span> <xsl:value-of select=“./PAGES"/><br/> </xsl:for-each> </HTML> </xsl:template> </xsl:stylesheet> Houssam Haitof

  20. XSL Processor • An XSL processor can transform an input XML document to a XML or any other output document based on rules in a second XML document (i.e. a XSLT file) Houssam Haitof

  21. Template Matching • XPath is used by XSLT in the match attribute of a template e.g. <template match=“sentence”> matches any sentence element. • The stylesheet processor goes through the XML document one element at a time, finds the first template which that element matches, and carries out the instructions in that template. Houssam Haitof

  22. Template Matching (cont) • If the element does not match any template in the stylesheet, then the default behaviour is for the processing to pass through to the children of this element without carrying out any instructions. • When the processing reaches an element which has only text children, the result of processing these children is to print out the text. Houssam Haitof

  23. Some examples of patterns • * matches any element • / matches the top-level element • sentence/word matches any word element with a sentence parent • word[@pos=‘noun’] matches any noun element whose pos attribute has the value noun • Many other possibilities … Houssam Haitof

  24. Some XSLT Syntax and Elements Houssam Haitof

  25. To begin • Start by declaring that this is an xml document • <?xml version ="1.0"?> • Next: declare the processing instruction & the namespace <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> Houssam Haitof

  26. XSLT Namespace • Every XSL file needs to specify the XSL namespace so that the parser knows which version of XSLT to use. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> • The namespace prefix xsl: is used in the rest of the XSL file to identify XSL processing statements. • If a statement isn't prefixed with xsl:, then it's simply copied to the output without being processed. Houssam Haitof

  27. XSL Elements • <xsl:template [match = “/”]> • Before processing can begin, the part of the XML document with the information to be copied to the output must be selected with an XPath expression. • The selected section of the document is called a node and is normally selected with the match operator. • If the entire document is to be selected, match the root node using match="/". • Another approach is to match the document element (the element that includes the entire document). Houssam Haitof

  28. XSL Elements .. • <xsl:apply-templates> • The <xsl:apply-templates> element applies a template rule to the current element or to the current element's child nodes. • If we add a select attribute to the <xsl:apply-templates> element it will process only the child element that matches the value of the attribute. Houssam Haitof

  29. XSL Elements .. • <xsl:for-each> • allows for processing on a number of nodes in the source tree, one at a time • Attributes • “select” denotes the path to the nodes • Example <xsl:for-each select=“/BOOKS/BOOK” > .. // other xsl elements </xsl:for-each> Houssam Haitof

  30. XSL Elements .. • <xsl:value-of> • When the xsl:for-each expression has selected a ‘BOOKS' element, the xsl:value-of expression extracts and copies to the output file the value stored in the selected element. • syntax: <xsl:value-of select= • Examples <xsl:value-of select="."> <xsl:value-of select="customer/@id"> <xsl:value-of select="author"/> Houssam Haitof

  31. XSL Elements .. • <xsl:if> • Allows for conditional processing. • Attribute: test • Example <xsl:if test=“title[@type=‘main]”> <b><xsl:apply-templates/></b> </xsl:if> Houssam Haitof

  32. XSL Elements .. • <xsl:sort> • XSLT can be used to sort elements by alphabetical or numerical order, according to attribute values, text contents, in ascending or descending order, and more … • <xsl:sort    select = string-expression     data-type = { "text" | "number" | Qname }    order = { "ascending" | "descending" }    case-order = { "upper-first" | "lower-first" }    lang = { language-code }/>  Houssam Haitof

  33. More XSL Elements .. • Some of the xsl elements not covered • <xsl:choose> • <xsl:when> • <xsl:attribute> • <xsl:element> • <xsl:comment> • <xsl:text> • <xsl:variable> Houssam Haitof

  34. XSLT Thank you! Houssam Haitof

More Related