WEEK 8: XSL - eXstensible Stylesheet Language |
| |
| Reading Assignments | |
|---|---|
| BOOK | PAGES |
| XML in easy steps | .CH 7 |
LINKS TO XML RELATED SITES
XSL, works by matching tags and acting on the elements that was matched. The first part of the document that we need to matching is the root node whose child is: <xsl:template match="/">. Afterward the parser moves down our XML document structure.
In our example we have created a simple one element XML document, were we have parsed the document root and have told the XSL parser to return everything (their values) within the root elements (<xsl:value-of />). In this case, it is just the word "hello".
Click here To view the XSL
It is worth knowing that the element "value-of" is part of the "xsl:" which as you recall is the name space. If you have ever programmed in Java or JavaScript you have inevitably used the "object.property" syntax. If we were to follow that syntax we would have written "xsl.value-of".
Click here To view the XSL
The first thing that should be noticeable is the use of "apply-templates select="//first_name" " where we had "value-of" . This syntax is instructing the parser to only match for "first_name" and upon matching it search and apply another set of "template" instructions. For now lets ignore "//" in the "//first_name" we will comeback to later. In the next template set there is pattern for matching the "first_name" (<xsl:template match="first_name">)with instructions for displaying the data inside the "first_name", "value-of". Note that we are only able to see the first name.
Click here To view the XSL
Click here To view the XSL
Now for some tricky stuff!
We can reverse the order of the elements in the XML file, and reverse the order of the rendering in the XSL file:
Look at XML file xsl4a.xml and XSL file xsl4a.xsl - look at the order of first_name and last_name in both .xml and .xsl documents
Look at XML file xsl4b.xml and XSL file xsl4b.xsl - now we reverse the order of the named templates in the stylesheet - no difference!
Look at XML file xsl4c.xml and XSL file xsl4c.xsl - now we reverse first_name and last_name in the .xml document - and no difference!
Look at XML file xsl4d.xml and XSL file xsl4d.xsl - now we keep first_name and last_name reversed in the .xml document, and then reverse first_name and last_name in the named templates in the .xsl document - and still no difference in the order of first_name and last_name!
Look at XML file xsl4e.xml and XSL file xsl4e.xsl - now we go back to the original files in 4a above, but reverse the order of the apply-templates select statement. Now the order of the output is reversed!
Look at XML file xsl4f.xml and XSL file xsl4f.xsl - we keep the pattern from 4e above, but reverse the template match statements. We still have last_name appearing before first_name. What XSL statement appears to be key? (hint: see 4e above).
Look at XML file xsl4g.xml and XSL file xsl4g.xsl - with first_name and last_name reversed in the .xml document, otherwise a repeat of 4e.
Look at XML file xsl4h.xml and XSL file xsl4h.xsl - with first_name and last_name reversed in the .xml document, otherwise a repeat of 4f.
Please study these carefully *before* asking (in person or online) questions about how this all works. It's all about the 'template select', not the 'template match' statements, as that affects the order in which the display is rendered. See 4e.
First we have some explaining to do: The first instruction in the XSL, <xsl:template match="/"> does not matches the root tag (same as root element). What it matches is the root node. The root element is the child of the root node. Until now, we did not have to care much about this technical detail, as every tag is a descendent of the root node. However, we now need to instruct the parser to search for all the direct children of the "student" tag ( the document root element) which is the child of the root node, "/" itself.
Therefore, we have given the instruction to the root node to search and match "student" then inside the "student" templates there is instruction to search for all the template of its children, "apply-templates".
Click here To view the XSL
Click here To view the XSL
Click here To view the XSL
Click here To view the XSL
Click here To view the XSL
Click here To view the XSL
One important thing that we need to remember is that XSL is an XML document, therefore all of our HTML tag must follow the well-formedness rules of XML document: Each tag needs to be closed or be treated as an XML empty tag ("<BR />).
In this example we have wrapped the "HTML" and "BODY" around the root node. Since these tags are not visible by themselves we added the text "CLASS" centered on the page as visible indication.
Click here To view the XSL
Click here To view the XSL
Click here To view the XSL
Click here To view the XSL
Click here To view the XSL
Click here To view the XSL
Click here To view the XSL
Click here To view the XSL
A very late edition (this is very rough) is a method of creating links and images without the html namespace. Try this approach first. Look at roses_xsl.xml, roses.xsl, using rose.jpg, or the whole roses_xsl.zip archive. Again - this is very rough.
Last is another example of creating links and images without the html namespace. This includes adding an image, a website link, and an email link. Look at formatting.xml, formatting_xsl.xml, and formatting.xsl, using metaman.jpg, or the whole formatting.zip archive.
Click here To view the XSL
Click here To view the CSS
More Example Files - Now that you have worked through the tutorials, you would like to see some elegant examples. (Right click to download the links).
The first is an artistic and colorful rendering by Tal Broda address_book_xsl.xml and address_book.xsl.
The second is a more conservative style which is also very elegant, and adds links to both the email address and to the website links. This was created by Alex Ivanov. It is shown in address_book_xsl_1.xml and address_book_1.xsl which uses the <html:a> prefixed element. An example of the same files which do not use an <html:a> prefix are shown in address_book_xsl_2.xml and address_book_2.xsl (the better approach).
HOMEWORK
For homework, display the address book which you had created, in a tabular format with ten records in them using XSL. It also needs to have at least one attribute and there must be a table nesting like the examples above. This homework can satisfy the XSL file requirement for your final project if you are using the address_book as your "theme". The .xsl requirement is almost 20% of your final grade in this class.