Experimenting with XML and XSLT
Requires XSLT Test Tool http://xslttest.appspot.com
- Copy the XML below
and paste it into the page at http://xslttest.appspot.com in the edit box beneath the title “Enter your XML code or it's URL:”
Next, copy the XSL below
and paste it into the same page (http://xslttest.appspot.com), this time in the edit box beneath title “Enter your XSLT or it's URL:”
3. Within http://xslttest.appspot.com click to generate the resultant XML, that looks like:
Note: the above XML is in fact also HTML that constructs a table. If you save it to a .html file and open it in a browser, it will provide the following:
You can also preview the table within http://xslttest.appspot.com by clicking
- Within your source XML at http://xslttest.appspot.com, add the two years of publication (indicated below):
2. Within your XSL at http://xslttest.appspot.com, add lines that will read the year of publication and add it to the table within a new column. First add the column header, then add a line to populate the value for each book:
Again within http://xslttest.appspot.com click to generate the updated XML that now includes publication year for each book
The table will now include Publication Year
Preview the table within http://xslttest.appspot.com by clicking
Further Exploration if you have time
We have used xpath within our XSLT to indicate which elements to obtain - books/book is the xpath that specifies the root node books and its child book. The subsequent children - title, author, publisher, and publishYear - are all found from the books/book context.
- Try changing the root node from books to publications and see what happens; for your XSLT to produce the same XML as has been generated so far, you will have to change any reference to books to publications within your XSL as well.
- Try adding further elements and values to your source XML and determine what additions will be required within the XSL to process these values. You may like to experiment further with attributes (e.g. <book format=”hardcover”> that would be accessed with @format)
- Experiment further with xpaths within http://www.freeformatter.com/xpath-tester.html, using http://www.w3schools.com/xsl/xpath_intro.asp as a guide to xpath syntax. You can use the source XML that we have used so far.
Much can be done to further format your HTML table: colour can be added, cell padding can be configured, fonts can be changed…
- Experiment with enhancing the resultant HTML - refer to http://www.w3schools.com/html for some inspiration! To start with, you may like to change the background colour that is set by the XSL within the bgcolor attribute (currently it is #FC99EF). Be guided by http://www.colorhexa.com/web-safe-colors to find the hexadecimal value for a colour of your choice.