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="http://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