1 / 26

Extensible Stylesheet Language (XSL)

Extensible Stylesheet Language (XSL). Special Edition Using XSLT Michael Floyd Que Publishing Chapters 1, 2, 4. What is XSL?. XSL is a markup language defined in XML that enables you to describe how XML data will be transformed into another XML vocabulary, HTML, or text.

abe
Télécharger la présentation

Extensible Stylesheet Language (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. Extensible Stylesheet Language (XSL) Special Edition Using XSLT Michael Floyd Que Publishing Chapters 1, 2, 4

  2. What is XSL? • XSL is a markup language defined in XML that enables you to describe how XML data will be transformed into another XML vocabulary, HTML, or text. • Introduced in 1998 • Split into three specifications: • Extensible Stylesheet Language Formatting Objects (XSL-FO) • Strictly addresses formatting • XSL Transformation Language (XSLT) • Allows you to convert of transform XML documents to other forms • XML Path Language (XPATH) • Allows you to locate an and access parts of an XML document

  3. Benefits of XSL • You can use XSLt with XPATH expressions to accomplish many of the same tasks that require use of the DOM. Those of you who had difficulty with the DOM might find XSL easier to work with.

  4. XSL Features • Uses a style sheet that can be associated with an XML document by: • Referencing it directly. The XML processor reads the stylesheet directive and loads the sheet and processes it. • Generating it dynamically using the DOM. You create the style sheet as a DOM object and call the DOM’s transformNode method.

  5. XSL Transformations • Provides a set of elements that allow you to locate parts of an XML document and transform them to another form • XML to HTML (We saw the need for this last week) • XML to XML (When you want a document that fits one schema to fit another) • XPATH assists in the searching process • The most common use is to take an XML document and convert it to HTML while including a CSS document to format the HTML nicely.

  6. XSL Transformations Part Two • Provides a large set of elements and attributes that allow you to: • Read the source tree and generate the result tree • Copy and edit elements, attributes, text, etc from the ”source” tree (the tree loaded into memory) and add them to the “result” tree (the tree being created on the fly that contains the results of applying transformations to the source tree). • Test for conditions • Perform conditional processing • Iterate through a group of elements • Search and sort elements • Set variables • Invoke scripts

  7. XPATH • General mechanism for traversing a document tree to locate and access node/s • Also uses with the XML Linking Specification (XLINK) • Used with XSLT to select and transform part or all of a document • You can use XPATH expressions as arguments to DOM method calls that return node/s such as SelectNodes() • Allow you to specify conditions • Can generate text that will be placed in the “result” tree.

  8. XSL Formatting Objects • Similar in concept to CSS • Used with complex structures • Uses XML notation • Allows you to change the order in which the elements appear • Much newer technology than CSS • Generally renders documents as PDF files since absolute positioning is used

  9. XSL Processing • The XML document loaded into memory is the source tree as it represents the source document • The goal is to create a second tree, the result tree. • Rules in the XSL Style sheet (called templates): • Walk through the source tree • Select components to process • Tranformation occurs

  10. Creating the XSL Style Sheet • You only have to transform the desired portions of the source tree • The XSL style sheet contains an XML declaration indicating it’s an XML document • <xsl:stylesheet version=“1.0” xmlns:xsl=http://www.w3.org/1999/XSL/Transform> • When the XML processor sees the style sheet reference, it creates the document tree. Then the XSLT processor applies the stylesheet to the XML document creating the result tree. • The URI varies depending on the version of XSL you’re using. • 1.0 is the current version • An official W3C recommendation as of 10/15/2001 • 2.0 is a work in progress • <xsl:stylesheet> is the root element • The XSL style sheet also contains an xsl:output element that indicates the result tree’s type: <xsl:output method=“html”> Other values include “text” or “xml”

  11. Templates and Template Rules • Templates are the individual statements used to create a transformation • A collection of templates is known as a template rule • <xsl:template match=“/”> sets a pointer to the root element in the source document • Known as the root template rule • All stylesheet contain this rule. • The match attribute specifies which element/s you are processing. The “/” character indicates the root element. • The “/” character is known as a pattern. • The template element is a top-level element because it’s a child of the xsl:StyleSheet root element.

  12. Understanding Patterns • A pattern is the syntax used to navigate around the source tree. • Used in the match attribute of a template rule. • Basic pattern syntax is easy to learn. • Allows you to perform 80% of what you need to navigate the source tree. • Similar to DOS syntax used to navigate a PC’s hard drive directories. • You can always move to the root directory by using the “/” pattern.

  13. Characters Used to Construct Patterns

  14. Creating Text • Anything in a template rule that is not a recognized stylesheet element will be placed in the result tree as a text node • <B>Company: </B><xsl:value-of select=“invoice/clientName” />

  15. Getting the Content of an Element • The <value-of> element retrieves the content from an element or attribute. • Takes an optional “Select” attribute and pattern/location path that allows you to specify node/s from which to get content. • A beginning slash is not needed if the pointer is currently set at the root node via <xsl:template match=“/”> • <value-of is an instruction element since it tells the XSLT processor to do something.

  16. Outputting the Results • The easiest way to attach your XSL Stylesheet to the XML document is by using the same method we used to attach a CSS stylesheet to an XML document. • However you set the mime type to text/xsl instead of text/css. • <?xml-stylesheet type=“text/xsl” href=“invoice.xsl”?>

  17. Controlling the Output • You can only convert XML to HTML, text, or XML using the <output> element. • It doesn’t change the actual source code when you view the page source in the Web browser.

  18. Retrieving Attributes • Preface the attribute name with an @ sign • <xsl:value-of select=“invoice/@num” /> • This is MUCH easier than grabbing attributes using the DOM.

  19. Creating an HTML File from an XML File The following XSLT file will display the title and genre of just the FIRST movie in the xml file. It does not do any looping on its own. <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <head> <title>My Favorite Movies</title> </head> <body> <h3>Rating: <xsl:value-of select="dvdCollection/dvd/@numberstars" /></h3> <h3>Title: <xsl:value-of select="dvdCollection/dvd/title" /></h3> <h3>Genre: <xsl:value-of select="dvdCollection/dvd/genre" /></h3> </body> </html> </xsl:template> </xsl:stylesheet>

  20. Explaining the XSL File • You have to move from the current location of the pointer to the level in the XML that contains the content you want to display. That’s why we had to list two parent elements before title and genre • The pointer’s current location was on the root element because the template element’s match attribute was set to “/” • Even though numberstars is an attribute of the DVD element, a slash is used. The @ sign denotes an attribute. • Since only the first DVD’s information was displayed, we need a way to loop through the entire source tree. • XPATH will show us the way!

  21. Using For-Each to Iterate <xsl:for-each select="dvdCollection/dvd"> <hr size="3" width="50%" color="green" /> <h3>Rating: <xsl:value-of select="@numberstars" /></h3> <h3>Title: <xsl:value-of select="title" /></h3> <h3>Genre: <xsl:value-of select="genre" /></h3> </xsl:for-each> • The xsl:for-each element allows you to use the select attribute to move to the child element you want the processor to point to. • Leave off the ending slash • Make sure you close the xsl:for-each element tag • Anything between the opening and closing xsl:for-each tag will be processed once for each instance of that element or attribute in the XML document.

  22. Fine Tuning Your Iteration • You can use xsl:sort to sort by an element or attribute. You can also choose whether the sort should be in ascending or descending order • The sort element comes after the xsl:for-each element: • <xsl:sort select="title" order="ascending" /> • The select attribute value contains the element/attribute that will be used to sort. • The default value is “.” which sets the strng value of the urrent node to be used as the sort key. • Order can be set to “ascending” or “descending”. Ascending is the default. • If you have a list of values with mixed case and you want one case to take precedence over the other you can use the case-order attribute and set the value to either upper-first or lower-first.

  23. Conditional Processing Using XSL:if • XSLT provides two methods for processing based upon a set of conditions • <xsl:if> operates like an if/then construct. You specify the condition for the if element using the test attribute: <xsl:if test="released>1999"> <h3>Year Released: <xsl:value-of select="released" /></h3> </xsl:if>

  24. Conditional Processing Using xsl:choose • Slightly more complex than using xsl:if • Works like Select Case or Switch • Lets you choose from a series of choices • Choices are written using a sequence of xsl:when elements <xsl:choose> <xsl:when test="genre='Comedy'"> <h3><font color="Red">Funny Stuff!</font></h3> </xsl:when> <xsl:when test="genre='Thriller'"> <h3><font color="purple">You'll be on the edge of your seats!</font></h3> </xsl:when> </xsl:choose>

  25. Adding Comments • You can create a comment node using <xsl:comment> • This allows you to add a comment to the processed document

  26. Including and Importing Style Sheets • You can combine XSL Stylesheets using the xsl:import element in the first style sheet. <xsl:stylesheet …..> <xsl:import href=“locationofOtherStyleSheet.xsl” /> </xsl:stylesheet> • The elements that are imported will precede those in the stylesheet containing the import element.

More Related