XML to XHTML transformation using XSLT server-side processor (PHP, ASP, .NET)

    Demerits of Presentational Markup in handling data markup

    Imagine if you could take pieces of existing HTML content and programmatically slice, dice and transform it into a new document that fits new criteria! You cannot reliably do that with conventional HTML because it is not always well-formed and hierarchical enough to enable DOM navigation to find and extract required information. This is especially difficult and even impossible when the markup is not semantic (using tables and other presentational markup). Do not loose hope though. The above scenario is possible if the markup is in semantic XHTML.

    XML/XSLT to the rescue

    The object of this document is not to establish or discuss the possibility of storing content in HTML or XHTML, but the markup, extraction and manipulation of data in XML by means of XSLT (Extensible Stylesheet Language for Transformations). In a previous document related to this one, we addressed the process of applying an XSLT file to XML data to result in a new XML, XHTML, or any other format (RTP, CSV,TSV,ASCII and so on).

    In this document, we will review how to accomplish this process on the server. This process has a significant advantage over client-size XML processing in that it is not dependent on the browser's ability to parse and process XML/XSLT. In addition, even when the browser has a parser, the differences and characteristics of every browser and parser come into play thereby compromising the reliability of the process. Some clients (PDA, WAP etc) may be excluded as they may not have an XML parser, and an XSLT processor.

    Procedure

    The process involves creating an server object and procedure that can apply the XSLT file to a given XML file and place the output into a variable that can be further processed, written into a file or printed to the screen.

    PHP

    In PHP, the commonly used XSLT processor especially upto PHP 4.x is the Sablotron XSLT processor. Below is sample code. This method enabled the use of an XML and XSLT file located on a remote server. This is more practical than local files as in the latter case, the information can be extracted and processed by other means.

    <?php // $xml and $xsl contain the XML and XSL data...
    $xml = implode('',file('http://www.cmsproducer.com/labs/xmlpagemarkup/myback.xml'));
    $xsl = implode('',file('http://www.cmsproducer.com/labs/xmlpagemarkup/idonny.xsl'));

    $xh = xslt_create();
    $output = xslt_process($xh, 'arg:xml', 'arg:xsl', NULL,
    array(
    'xml' => $xml,
    'xsl' => $xsl));
    echo $output; // print output
    ?>

    ASP.NET

    A similar approach to the above is taken, and without wasting time, here is the code:

    <%@Import Namespace="System.Xml"%>
    <%@Import Namespace="System.Xml.Xsl"%>
    &ltscript language="C#" runat="server">
    void Page_Load(object sender, System.EventArgs e) {
    XmlDocument doc = new XmlDocument();
    doc.Load("http://www.cmsproducer.com/labs/xmlpagemarkup/wan_specials.xml");
    XslTransform trans = new XslTransform();
    trans.Load("http://www.cmsproducer.com/labs/xmlpagemarkup/myspecials.xsl");
    xslTransform.Document = doc;
    xslTransform.Transform = trans;
    }
    </script>
    &ltasp:Xml ID="xslTransform" Runat="server">
    </asp:Xml>

    Classic ASP

    The Microsoft server object - Microsoft.XMLHTTP is used on the server to accessXML data from a remote location for processing. More information about this server-side object can be obtained at the Microsoft Support resource for Microsoft.XMLHTTP

    <%
    'Create variable containers
    Dim xmlhttp_id
    Dim responseXML_id
    Dim XMLloaderror_id

    Set xmlhttp_id = Server.CreateObject("Microsoft.XMLHTTP") 'Initialise an instance of the MS XMLHTTP object

    'Retrieve XML data from a remove HTTP resouce using MS XMLHTTP object
    xmlhttp_id.Open "GET", "http://cmsproducer.com/taxonomy/term/91/all/feed", false
    xmlhttp_id.Send("")

    'Set a condition to see if the data was retrieved by looking for HTTP header status code (200 = OK)
    If xmlhttp_id.Status = 200 Then
    Set responseXML_id = xmlhttp_id.ResponseXML 'Save the retrieved XML to an object
    XMLloaderror_id = False
    Else
    Response.Write("<p class=""service_alerts"">Page Error: Remote XML resource could not be reached</p>") 'Show error message
    XMLloaderror_id = True
    End If

    Set xmlhttp_id = nothing 'Clear Microsoft XMLHTTP object (important for reuse of resources)

    If Not XMLloaderror_id Then

    'Load the XML from the responseXML_id object
    set oXML_id = Server.CreateObject("Microsoft.XMLDOM")
    oXML_id.Async = false
    oXML_id.Load(responseXML_id)

    'Load the XSL from disk
    set oXSL_id = Server.CreateObject("Microsoft.XMLDOM")
    oXSL_id.Async = False
    'Refer to the XSLT file transform your remote XML resource data
    oXSL_il.Load(Server.MapPath("/content/xslt/prepareXHTML.xslt"))

    Response.Write(oXML_id.transformNode(oXSL_id)) 'Transform the XML using the XSLT stylesheet

    End If

    %>