1 / 36

XSL

XSL. eXtensible Stylesheet Language. What is XSL?. XSL is a language that allows one to describe a browser how to process an XML file. XSL can convert an XML file into another XML with different format. XSL can convert an XML file into a non-XML file. XSL.

chuck
Download Presentation

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. XSL eXtensibleStylesheet Language

  2. What is XSL? • XSL is a language that allows one to describe a browser how to process an XML file. • XSL can convert an XML file into another XML with different format. • XSL can convert an XML file into a non-XML file.

  3. XSL • The most common type of XSL processing is to convert XML file into HTML file which can be displayed by browsers. We will focus on this use of XSL. • XSL is the bridge between XML and HTML. • We can use XSL to have different HTML formats for the same data represented in XML. • Separating data (contents) from style tags (display commands). • XML example, class.xml

  4. XML Tree (Logical Structure)

  5. XSL Example for class.xml (XMLHTML) <?xml version=“1.0” ?> <xsl:stylesheet xmlns:xsl=“http://www.w3.org/TR/WD-xsl”> <xsl:template match=“/”> <HTML> <HEAD><TITLE> Class.xml Stylesheet</TITLE></HEAD> <BODY BGCOLOR=“red”> The homepage of <EM> <xsl:value-of select=‘’class/title”/> </EM> </BODY> </HTML> </xsl:template> </xsl:stylesheet>

  6. Try it yourself • Down load class.xml and class.xsl from homepage of the course. • Save it on your machine. • View the XML file with web browser. • Add the line that instructs the browser which stylesheet to use to format the XML (add it to second line). • View the XML file again.

  7. XSL • The first line of the XSL file shows that XSL itself is written in XML. (<?xml version=“1.0” ?>). • Line: <xsl:stylesheetxmlns:xsl=“http://www.w3.org/TR/WD-xsl”> tells the browser that this is a XSL stylesheet. • The XSL tags specify the rules to be applied to elements or attributes of the XML. Any other tag, including HTML tags, or piece of text will be kept as it is.

  8. XSL paths • Using XSL paths we can display the content of the elements or values of the attributes. • “/” by itself represents the root element. • When “/” is used as separator between two elements it indicates a move one level down the hierarchy. • Example: / , class, class/title or class/students/student/name/first

  9. XSL Paths • The browser can also pick out and use attribute values. The syntax is @attributName • Example: class/students/student/major/@decided.

  10. XSL template • Each template contains rules to apply when an element is matched with the path specified as value of the match attribute. • Thus, <xsl:template match=“/”> …. </xsl:template> means apply the rules within the start and end tags to the root element.

  11. XSL value-of • The <xsl:value-of select=“….”> element is used to extract the value of the element or attribute specified in select attribute. • Examples: <xsl:value-of select=“class/title”> <xsl:value-of select=“class/students/student/name/first ”> <xsl:value-of select=“class/students/student/major/@decided”>

  12. Do it yourself 1-Copy this piece of XML and save it as note.xml: <?xml version="1.0" ?> <note> <from>John</from> <to>Merry</to> <message title=“Reminder”>Don’t forget mer</message> </note> 2- View it using an web browser.

  13. Do it yourself 3- Write an XSL stylesheet for note which displays note as: There was a message from John to Merry with title Reminder. 4- Note John is written in bold and Merry italic. 5- Name the stylesheet as note.xsl 5- Add the line which instructs the browser to style note.xml with note.xsl. 6- View note.xml with a web browser.

  14. Do it yourself 3- Write an XSL stylesheet for note which displays note as: There was a message from John to Merry with title Reminder. 4- Note John is written in bold and Merry italic. 5- Name the stylesheet as note.xsl 5- Add the line which instructs the browser to style note.xml with note.xsl. 6- View note.xml with a web browser.

  15. <?xml version="1.0" ?> <xsl:stylesheetxmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> <HTML> <HEAD><TITLE> Note Stylesheet</TITLE></HEAD> <BODY> There was a message from <B> <xsl:value-of select="note/from"/> </B> (Sample Solution) to <I> <xsl:value-of select="note/to"/> </I> with title <xsl:value-of select="note/message/@title"/>. </BODY> </HTML> </xsl:template> </xsl:stylesheet>

  16. Redo it. • Write an XSL stylesheet which displays the note.xml in the following form (HTML table):

  17. <?xml version="1.0" ?> <xsl:stylesheetxmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> <HTML> <HEAD><TITLE> Note Stylesheet </TITLE></HEAD> <BODY> <TABLE BORDER="5" CELLPADDING="5"> <TR> <TH>From</TH> <TH>To</TH> <TH>Message Title</TH> </TR> <TR> <TD> (Sample Solution) <xsl:value-of select="note/from"/> </TD> <TD> <xsl:value-of select="note/to"/> </TD> <TD> <xsl:value-of select="note/message/@title"/> </TD> </TR> </TABLE> </BODY> </HTML> </xsl:template> </xsl:stylesheet>

  18. HW2 • I posted assignment #2. • Let’s review it together.

  19. More XML • Create your own XML file for a Lunch Menu. The root element should be <lunch_menu>. The sub elements of <lunch_menu> are <food> corresponding to each food in the menu. Then each <food> element has a number of attributes or child elements (your choice) such as name, price, calories,…. • Add at least two entries to your XML tree. • View the result using a web browser. (Make sure it is well-formed).

  20. <?xml version="1.0" ?> <lunch_menu> <food> <name>Cheese Burger</name> <price>$5.95</price> <calories>650</calories> </food> <food> <name>Macaroni and Cheese</name> <price>$7.95</price> <calories>900</calories> (Sample Solution) </food> <food> <name>Hot Dog</name> <price>$3.95</price> <calories>500</calories> </food> </lunch_menu>

  21. Repetition • Using <xsl:value-of select=“…”> we can retrieve the value of element or attribute which matches the XSL path specified by the select attribute. • But what if there are more than one element or attribute which matches with the specified path and we want to apply some formatting rules on all these matched entities?

  22. Example • In class.xml we have couple of student elements which match with the XSL path: class/students/student • Assume we want to create an style-sheet which lists the name of all students in class. • We need an XSL instruction to apply a set of rules on each student element.

  23. <xsl:for-each> <xsl:for-each select=“…”> … </xsl:for-each> allows us to specify a set of styling rules within the start and end tags to be applied to all elements or attributes which match the specific path in select attribute. Example: Write an XSL stylesheet for class.xml where each student name is in a separate line and with bold face.

  24. Example of <xsl:for-each> <?xml version="1.0" ?> <xsl:stylesheetxmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> <HTML> <HEAD> <TITLE> Student Names </TITLE> </HEAD> <BODY> <xsl:for-each select="class/students/student"> <B> <xsl:value-of select="name/first"/> <xsl:value-of select="name/last"/> <BR/> </B> </xsl:for-each> </BODY> </HTML> </xsl:template> </xsl:stylesheet>

  25. Exercise • Download and save class-names.xml into your machines. • Download and save XSL-EX-1.xsl into your machines in the same folder. • Modify the style-sheet, so instead of name of students you have an unordered list of majors of students of the class. (Remember, the HTML syntax for unordered list: <UL> <LI> … </LI> <LI> … </LI> </UL> ) • View the classView.xml with a web browser.

  26. Sample Solution <?xml version="1.0" ?> <xsl:stylesheetxmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> <HTML> <HEAD> <TITLE> Student Majors </TITLE> </HEAD> <BODY> <UL> <xsl:for-each select="class/students/student"> <LI><xsl:value-of select="major/majorName"/></LI> </xsl:for-each> </UL> </BODY> </HTML> </xsl:template> </xsl:stylesheet>

  27. Problem • Some students have not yet decided their majors. For these students the value of decided attribute is “n”. • But the formatting rules inside the <xsl:for-each> will be applied to all students regardless of having a major or not. • We want a way to instruct the XSL to apply the rules when the value of decided attribute is “y” (decided = “y”).

  28. Selection: Filtering Data • <xsl:if match=“.[name=‘value’]”> is yet another xsl tag which instructs the XSL to apply the rules within start tag <xsl:if > and end tag </xsl:if> if the value of the specified element or attribute matches with the value specified at the right hand side of “=”. • Put it another way, <xsl:if> conditionally retrieves the value of the specified element or attribute. • This condition is specified as value of match attribute.

  29. Specifying the Condition inside match attribute <xsl:if match=“.[ XSL test path =‘value’ ]” > • Test path : XSL path which specifies the element or attribute we want to perform the test on its value. • Value : The conditional value. That is if the value of the specified element or attribute matches this value the rules or instructions within start and end tags will be executed.

  30. Examples • <xsl:if match=“ .[ majorName =‘MGT’ ]” > • “.” in XSL is shorthand for “the current node”. • Select the current element IF its sub-element, majorName has value MGT. • <xsl:if match=“.[major/@decided = ‘y’] • Select the current element if the value of “decided” attribute of its sub-element, major is ‘y’.] • <xsl:if match=“.[major/submajor = ‘Finance’]”> • Select the current element IF the value of the child, submajor of its child, major is ‘Finance’.

  31. Different Web Browsers • Note that the standard syntax for <xsl:if> suggested by W3C is: <xsl:if test ="major/@decided = 'n'">

  32. With the standard syntax for <xsl:if> , the <xsl:stylesheet> tag should be: <xsl:stylesheet version="1.0“ xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> Instead of: <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> You can use any of them, but the second case works only with Internet Explorer.

  33. Example List the name of the majors of all students in the class. Use <xsl:if> to avoid empty items.

  34. <?xml version="1.0" ?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> <HTML> <HEAD> <TITLE> Major Names </TITLE> </HEAD> <BODY> <UL> <xsl:for-each select="class/students/student"> <xsl:if match=".[major/@decided='y']"> <LI><xsl:value-of select="major/majorName"/></LI> </xsl:if> </xsl:for-each> </UL> (Sample Solution 1) </BODY> </HTML> </xsl:template> </xsl:stylesheet>

  35. Do it yourself • Download and save the file XSL-EX-3.xsl. • Change the appropriate line in class-names.xml to be styled with XSL-EX-3.xsl. • View class.xml with a browser. • Change the XSL-EX-3.xsl to present the class.xml as a table with a column for student names and another column for their major. If the student has no major, the phrase N/A should appear in major column.

  36. Things to remember • HTML table tags : <TABLE BORDER = “n”> <TR> <TH> header </TH> … </TR> <TR> <TD> data</TD> …. </TR> </TABLE> • You need to use <xsl:for-each> and two <xsl:if> within it.

More Related