MICRO CENTER: COMPUTERS AND ELECTRONICS
Random Access   chris, kp & rob
Tech Take-Apart
Introduction to XML, Part III
by kp

 

Geek Level: Beginner/Intermediate
HTML, XHTML or web-based programming experience helpful

Geek Tools:
Text editor or web development WYSIWYG; web browser

In the past two Tech Take Apart articles, I have introduced how to create an XML document from scratch including writing a schema and XSL template. This month, I am focusing on adding formatting to the XSL file from the previous month, and discussing some applications.

To begin, I went back to my kparticles_final.xsl file where I previously wrote a basic template for my XML using tables. There are a couple of ways in which you can add styles to your XML through XSLT. The first option is to add inline styles with CSS or use XSL-FO notation. In this example, I am just plugging in my CSS tags that I declared in my external CSS file.

XSLT CSS Format

I continued integrating my inline CSS tags for each of the table cells, and then tested my results in a browser. The final results look like this:

XSLT Final Format

As mentioned before, there is another way of formatting your XML using XSL-FO language. This is a specialized notation that integrates with the XSLT template to control layout and presentation. To use XSL-FO, you need to declare the namespace at the top of your XSLT document as follows:

        <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
The word "root" represents the root element name for your XML file. Each tag has to have the fo: prefix to apply the formatting. Here is an example of what the table in my XSLT file might look like using XSL-FO:
 
<?xml version="1.0" encoding="ISO-8859-1"?>
<fo:newsletter xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="myarticles">
<fo:table> <fo:table-row> <fo:table-cell> <fo:block> 2007 Random Access Articles</fo:block> </fo:table-cell> </fo:table-row> </fo:simple-page-master> </fo:layout-master-set> </fo:newsletter >
XSL-FO is also very useful when styling XML for print layout. You can control the flow of your pages by restricting data to be placed in a specific region or to flow onto the next page. For a more detailed tutorial on XSL-FO, visit W3schools.com.

Once your XML formatting is finished, you need an XSLT engine to render your template. There are several software tools available for free or paid. Some XSLT engines available are Saxon-B, Xalan, or Altova. These require some additional set up to run. Try XML Problem-Design-Solution tutorials for a step by step on installing Saxon-B and Xalan.

With your XSL template set up, you still are left with the task of keeping your XML data current. There is a more efficient way of managing your information using Excel. In the example below, I've imported my original XML file into Excel by going to Data -> XML -> Import. Excel places all of the data in order based on the XML tree hierarchy. From here, it is much easier to edit your data and export it for updates rather than making changes in a text editor.

XML Spreadsheet

Another way to use of XML is exporting data from a SQL database. Depending on the volume of information, it would take next to forever to format a web site with hundreds of items by using plain HTML. By incorporating XSLT, this is done automatically with your prepared template. Though utilizing XML for your data takes some planning, the long term benefits of saving time and managing your content efficiently makes it worthwhile.

Shop:
XML Problem Design Solution

References:
W3C, Extensible Markup Language
W3Schools, XSL - FO tutorial
Von See, Chris, Keskar, Nitin. XSLT Developer's Guide. McGraw Hill/ Osborne. ISBN: 0072194081.

Get Random Access

Understanding Tech

Print this article

Shop Online

Send-to-a-
Friend

Your Name:

Your Email:

Your Friend's Name:

Your Friend's Email:


 © Micro Center