Welcome to MSDN Blogs Sign in | Join | Help

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="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

 

Published Tuesday, June 22, 2004 4:10 AM by Calvin_Hsia

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Start using XML and XSLT to create HTML

Tuesday, June 22, 2004 4:44 AM by Anna-Jayne Metcalfe
By coincidence generating HTML reports is exactly the reason I learnt XML in the first place! Although I write mostly in C++, the principles are the same.

You can also generate other output formats - CSV and TXT are pretty easy, for example.

The one issue I have found is that the transformation adds an extra newline to each line, so the output comes out double spaced. Although that's not a problem for HTML output, it's a pain for others - and one I haven't found a solution for yet.

# re: Start using XML and XSLT to create HTML

Tuesday, June 22, 2004 1:38 PM by FoxRocks
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

# re: Start using XML and XSLT to create HTML

Tuesday, June 22, 2004 1:41 PM by FoxRocks
Well, that was stupid to use xml after all, we could have just looped the cursor and output the html with textmerge.

Anyway, xslt is powerful too, just can't get rid of my old fox habits easily.

:-D

# re: Start using XML and XSLT to create HTML

Tuesday, June 22, 2004 2:02 PM by FoxRocks
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 ;-)

# re: Start using XML and XSLT to create HTML

Thursday, June 24, 2004 10:09 AM by Robert Etheredge
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?

# re: Start using XML and XSLT to create HTML

Thursday, June 24, 2004 10:47 AM by CalvinH [MS]
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.

# Use a simple XSLT to read the RSS feed from a blog

Tuesday, January 11, 2005 4:05 PM by Calvin Hsia's WebLog

# Use a simple XSLT to read the RSS feed from a blog

Wednesday, January 12, 2005 3:55 PM by Calvin Hsia's WebLog

# re: Start using XML and XSLT to create HTML

Wednesday, April 19, 2006 10:23 AM by df
df

# frisian Knokke Heist

Wednesday, March 21, 2007 8:21 AM by frisian Knokke Heist

thoughts from a professional developer

I do not agree. Go to http://www.justjobz.info/imaginary_Belgium/derrick_Flandre/frisian_Knokke-Heist_1.html

# depot Rostock

Thursday, March 22, 2007 3:46 AM by depot Rostock

# sedum Venice

Thursday, March 29, 2007 4:46 AM by sedum Venice

thoughts from a professional developer

I do not agree. Go to http://www.docareers.info/incinerate_Italy/utmost_Veneto/sedum_Venice_1.html

# I disagree

Tuesday, August 14, 2007 3:29 PM by warsaw hotels

thoughts from a professional developer

I do not agree. Go to http://apartments.waw.pl/

# Use new XML Features of VB to generate dynamic scripts and text files

Thursday, October 04, 2007 1:53 PM by Calvin Hsia's WebLog

There's a very useful feature that FoxPro users have had for over 2 decades: being able to output text

# Use new XML Features of VB to generate dynamic scripts and text files

Thursday, October 04, 2007 1:58 PM by Noticias externas

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

# MSDN Blog Postings &raquo; How to Create dynamic XAML to display arbitrary XML

# incomplete information

Thursday, January 31, 2008 5:17 PM by shivnandan

what is the process of step by step to run a xml program is not defined here.

# Calvin Hsia s WebLog Start using XML and XSLT to create HTML | Paid Surveys

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker