ARTICLE

XML Transformations

Posted by John Charles Olamendy Turruellas Articles | XML March 15, 2004
This article shows you how to use XML transformation classes defined in the .NET Framework class library to transform XML data into various user interfaces.
Reader Level:

Your XML document is a well-organized container of information, but it seems static.Although the document format is useful in representing information structurally, you still tied down to software applications specifically made to handle it.

What if you want to see the document in an HTML browser that doesn't support XML?
Or output it as text for printing.

You see how an XML application can replace a word processor or a browser. This is advantageous because you can begin with an XML document (which can be read from a database, a file in your local system, or downloaded from the Web) and transform it through a style sheet by processor, (or manipulated through the myriad of XML tools available to us) yielding another document (possible another filtered XML document). The XML solution is then to separate authoring from publishing therefore the author of the pages writes the document in XML, thinking in a well-formed and logic structure adopting an XML vocabulary that focuses on the organization of the document, and not handle the presentation in terms of sections, titles, abstracts and more.

So publishing the document then simply requires converting the document into HTML, WML, or another popular format.

There are three main roles:

1. The documents in structure-rich XML providing the static information.

2. XLST style sheets that implements the conversion to HTML, WML or another format. This is a scripting language optimized for conversion of XML documents.

3. An XSLT processor that is responsible for applying the style sheets and generating an output format, which options might be:

  • HTML: is not an XML vocabulary. There is no XML to XML transformation, but HTML is used by Web browser to display information.

  • XHTML: an XML application of HTML. The major advantage is that it is based on HTML so it will be familiar for Web designers. Currently it is too complex for mobile phones. W3C is working to simplify XHTML.

  • ML: the markup language for WAP devices. It is an XML application used for wireless applications, such as mobile phones. You can think of WML as an HTML for mobile users.

  • OpenBook: the format for eBook, based on HTML. Another interesting format for mobile users designed for eBook, a different groups a mobile user. An eBook can take a lot of forms, but it is generally a palm-size device on which readers download books.

  • XSLFO: a new display language that is optimized for printed documents.

For more information about XSLT, there is an XSLT specification on the Web; you can rich it at www.w3.org/TR/xslt.

This paper shows how we can take advantage of the former ideas, using the technologies shipped in Microsoft.NET platform.

Suppose you have a bookstore and you want to publish all the books for sale. For this solution, I'll use ASP.NET over Microsoft.NET as the Web environment.

First of all you have to analyze and design your solution, so define the items (objects holding information) to publish, and the attributes of such items, in this case: the title, author, an abstract according the theme exposed, and a collection of paragraphs.

Figure 1 shows an XML document with all the information of my bookstore.
You may notice how the items are represented in this document in a logic manner, expressing the containment of objects, so the parent-child relationship.

<?xml version="1.0"?>
<?
xml-stylesheet type="text/xsl" href="BookFair.xslt"?>
<
News>
<
URL>http://www.john.com</URL>
<
Item>
<
Title>Using XML.</Title>
<
Author>Author1</Author>
<
Abstract>
A new intermediate/advanced book for XML developers.
</Abstract>
<
Para>
Learn advanced XML programming with Applied XMLSolutions. This hands-on teaching book is filled with practical examples.
</Para>
<
Para>
Applied XML Solutions is a great complement to XML by Example.
</Para>
<
Para>
This is the end paragraph.
</Para></Item>
<
Item>
<
Title>XML programming</Title>
<
Author>Author2</Author>
<
Abstract>
XML for novice.
</Abstract>
<
Para>
This is a book for novice in XML as Web technologies.
</Para>
</
Item>
<
Item>
<
Title>.NET Programming</Title>
<
Author>Author3</Author>
<
Abstract>
About everything in .NET platform.
</Abstract>
<
Para>
A book that explains how you can use Microsoft.NET for your Web Solutions.</Para>
<
Para>
The best book about Microsoft.NET.
</Para>
</
Item>
<
Item>
<
Title>Windows Programming</Title>
<
Author>Author4</Author>
<
Abstract>
How to use Windows API.
</Abstract>
<
Para>
A very instructive book about how to program to Win32.
</Para>
<
Para>
You can learn to use the Windows API.
</Para>
</
Item>
</
News>

Figure 1.(BookFair.xml).

I then define the transformation rules to generate an HTML table for showing to the Web customers. In figure 2, you can see the XSL rules.

<?xml version="1.0" encoding="UTF-8"?>
<
xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform>"
<
xsl:template match="News">
<
h1>Show items of my company.</h1>
<
table width="735" height="130" border="1">
<
caption>
Books Information.
</caption>
<
tr>
<
th width="33" scope="col">Pos</th>
<
th width="162" scope="col">Author</th>
<
th width="169" scope="col">Title</th>
<
th width="343" scope="col">Param</th>
</
tr>
<
xsl:for-each select="Item">
<
tr>
<
td><xsl:value-of select="position()"/></td>
<
td>
<
b>
<
xsl:value-of select="Author"/>
</
b>
</
td>
<
td>
<
p>
<
xsl:value-of select="Title"/>
</
p>
</
td>
<
td>
<
xsl:apply-templates select="Para"/>
</
td>
</
tr>
</
xsl:for-each>
</
table>
</
xsl:template>
<
xsl:template match="Para">
<
p><xsl:apply-templates/></p>
</
xsl:template>
</
xsl:stylesheet>

Figure 2. (BookFair.xlst).

The Web application that processes the documents is composed by page named BookFair.aspx (figure 3) as the view of the application, and a file hosting all the objects that implements the logic of the application it is named BookFair.aspx.cs (figure 4).

<%@ Page language="c#" Codebehind="BookFair.aspx.cs" AutoEventWireup="false" Inherits="BookFair.BookFairForm" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<
HTML>
<
HEAD>
<
title>WebForm1</title>
<
meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<
meta name="CODE_LANGUAGE" Content="C#">
<
meta name="vs_defaultClientScript" content="JavaScript">
<
meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5>"
</
HEAD>
<
body MS_POSITIONING="GridLayout">
<
h1>John Olamendy Test.</h1>
<
p>
<%Response.Write(m_strXmlOutput);%>
</p>
</
body>
</
HTML>

Figure 3.( (BookFair.aspx)

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Xml.Xsl;
using System.IO;
namespace BookFair
{
public class BookFairForm : System.Web.UI.Page
{
protected string m_strXmlOutput;
private void Page_Load(object sender,System.EventArgs e)
{
//This is the logic of the application.
XslTransform objTransform=new XslTransform();
XmlDocument objDocument=
new XmlDocument();
string strAppPath=this.Server.MapPath("");
StringWriter objStream=
new StringWriter();
objDocument.Load(strAppPath+\\BookFair.xml);
objTransform.Load(strAppPath+\\BookFair.xslt);
objTransform.Transform(objDocument,
null,objStream,null);
this.m_strXmlOutput=objStream.ToString();
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
}
}

Figure 4. (BookFair.aspx.cs)

And as you can see this is an application for Web. But in the same manner it is possible create an application for Mobile Users, only changing the view side, and maintaining the documents. This is a good practice of design named model-view, where you have the logic of your application and this logic can be applied for any view no matter if it is a desktop application, a Web application or Wireless application.

Eng. John Charles Olamendy Turruellas.

COMMENT USING

Trending up