Share via


Start using XML and XSLT to create HTML

Tonight at the Seattle Visual Foxpro user group meeting Richard Stanton gave a great presentation of the new reporting features of VFP9, which is currently available in beta.

The question came up about how to use XSLT to create HTML from XML. I said it could be done in just a few lines of code.

 

Here’s sample code to do it: it uses the first 4 records of the customer table and creates XML using the CURSORTOXML() function.

 

It then uses TEXTMERGE to create a 2nd XML string that is the XSLT.

Each of these strings is loaded into its own instance of XMLDOM.

Then the TRANSFORMNODE method applies the XSL to the XML to create the HTML (which is just an HTML table with headers and records), which is then shown in IE.

That’s a lot of acronyms!

USE customer

cursortoxml(0,'cxml',1,0,4) && Create a string variable cxml that contains the first 4 records of customer

TEXT TO cxsl noshow && Create a string variable cxsl: an XSLT that will convert XML to an HTML table

<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="https://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method="html"/>

<xsl:template match="/">

<HTML>

<HEAD>

<TITLE><xsl:value-of select="//VFPData/customer"/></TITLE>

</HEAD>

<BODY>

<table frame="box">

<tr>

<th>Customer ID</th>

<th>CompanyName</th>

<th>ContactName</th>

</tr>

<xsl:for-each select="//VFPData/customer">

<tr>

<td><xsl:value-of select="cust_id"/></td>

<td><xsl:value-of select="company"/></td>

<td><xsl:value-of select="contact"/></td>

</tr>

</xsl:for-each>

</table>

</BODY>

</HTML>

</xsl:template>

</xsl:stylesheet>

ENDTEXT

LOCAL oxml as msxml.DOMDocument, oxsl as msxml.DOMDocument

oxml=NEWOBJECT('msxml.DOMDocument')

oxsl=NEWOBJECT('msxml.DOMDocument') && another instance of the XMLDOM

oxml.loadXML(cxml) && load the cursor XML into the XMLDOM

oxsl.loadXML(cxsl) && load the XSLT that we just created.

cHTML= oxml.transformNode(oxsl) && apply the XSL to the cursor XML

STRTOFILE(cHTML,"d:\t.htm") && put it into a file

!start d:\t.htm && show the file in IE

*End of code

Comments

  • Anonymous
    June 21, 2004
    The comment has been removed

  • Anonymous
    June 22, 2004
    Here is the Fox way without XSLT ;-)

    USE customer
    cursortoxml(0,'xmlCustomers',1,0,4)

    xml = NewObject("Msxml2.DomDocument")
    xml.LoadXml(xmlCustomers)
    oNodes = xml.SelectNodes("//VFPData/customer")

    SET TEXTMERGE ON TO MEMVAR html NOSHOW
    <HTML>
    <HEAD>
    <TITLE>Customers</TITLE>
    </HEAD>
    <BODY>
    <table frame="box">
    <tr>
    <th>Customer ID</th>
    <th>CompanyName</th>
    <th>ContactName</th>
    </tr>
    For Each oNode In oNodes
    <tr>
    <td><<oNode.SelectSingleNode("cust_id").text>></td>
    <td><<oNode.SelectSingleNode("company").text>></td>
    <td><<oNode.SelectSingleNode("contact").text>></td>
    </tr>
    EndFor
    </table>
    </BODY>
    </HTML>
    SET TEXTMERGE OFF
    SET TEXTMERGE TO

    STRTOFILE(html,"c:test.htm")
    Modify Command "c:test.htm" NOWAIT

  • Anonymous
    June 22, 2004
    The comment has been removed

  • Anonymous
    June 22, 2004
    Ok, pure fox, no more acronyms required:

    USE customer

    SET TEXTMERGE ON TO MEMVAR html NOSHOW
    <HTML>
    <HEAD>
    <TITLE>Customers</TITLE>
    </HEAD>
    <BODY>
    <table frame="box">
    <tr>
    <th>Customer ID</th>
    <th>CompanyName</th>
    <th>ContactName</th>
    </tr>
    Scan Next 4
    <tr>
    <td><<cust_id>></td>
    <td><<company>></td>
    <td><<contact>></td>
    </tr>
    EndScan
    </table>
    </BODY>
    </HTML>
    SET TEXTMERGE OFF
    SET TEXTMERGE TO

    STRTOFILE(html,"c:test.htm") && put it into a file
    Modify Command "c:test.htm" NOWAIT


    Now that's simplicity
    I love U Fox ;-)

  • Anonymous
    June 24, 2004
    If I change the cursortoxml function to return all records, then some of the CompanyNames display funky characters. i.e. customerID COMMI, OCEAN.

    What causes that?
    How do you solve that?

  • Anonymous
    June 24, 2004
    Those records with "funky" characters are those with non English alphabet characters, such as an 'e' with an accent, an 'o' with an umlaut, etc.
    Just change the 4th parameter of CursorToXML() to 48 to change the output encoding.

  • Anonymous
    April 19, 2006
    df

  • Anonymous
    March 21, 2007
    thoughts from a professional developer I do not agree. Go to http://www.justjobz.info/imaginary_Belgium/derrick_Flandre/frisian_Knokke-Heist_1.html

  • Anonymous
    March 21, 2007
    thoughts from a professional developer I do not agree. Go to http://www.easyworkz.info/jounce_Germany/cauterization_Mecklenburg-Vorpommern/depot_Rostock_1.html

  • Anonymous
    March 28, 2007
    thoughts from a professional developer I do not agree. Go to http://www.docareers.info/incinerate_Italy/utmost_Veneto/sedum_Venice_1.html

  • Anonymous
    August 14, 2007
    thoughts from a professional developer I do not agree. Go to http://apartments.waw.pl/

  • Anonymous
    October 04, 2007
    There's a very useful feature that FoxPro users have had for over 2 decades: being able to output text

  • Anonymous
    October 04, 2007
    There&#39;s a very useful feature that FoxPro users have had for over 2 decades: being able to output

  • Anonymous
    December 06, 2007
    PingBack from http://msdnrss.thecoderblogs.com/2007/12/06/how-to-create-dynamic-xaml-to-display-arbitrary-xml/

  • Anonymous
    January 31, 2008
    what is the process of step by step to run a xml program is not defined here.

  • Anonymous
    May 29, 2009
    PingBack from http://paidsurveyshub.info/story.php?title=calvin-hsia-s-weblog-start-using-xml-and-xslt-to-create-html