Skip to content

Search for Research Data

Search the ANDS Site

Search
Search
http://www.ands.org.au http://www.ands.org.au
Share
Share

Experimenting with XML and XSLT

Requires XSLT Test Tool http://xslttest.appspot.com

Running the Example
  1. 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 Run Transformation 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:

Book Collection

You can also preview the table within http://xslttest.appspot.com by clicking Open Result

Making Changes

  1. 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 Run Transformation to generate the updated XML that now includes publication year for each book

The table will now include Publication Year

Book Collection 2

Preview the table within http://xslttest.appspot.com by clicking Open Result

Further Exploration if you have time

xpath

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)

html

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.

in Skills