Last November, I presented SQL2XLS, a utility to generate Excel sheets from an SQL statement. By using the same QSQPRCED API to run the SQL query and extract data from a database, I present here two companion utilities: SQL2CSV and SQL2XML.
SQL2CSV allows you to directly create CSV files of any size from an SQL statement. (You can use it to replace the CPYTOIMPF command.) CSV is a well-known format used to exchange data, so it requires no further explanation.
SQL2XML allows you to create XML documents, again from any SQL statement. XML is a standard way of putting information in a format that can be processed and exchanged across hardware devices, operating systems, software applications, and the Web. A variety of standards exist in the XML universe. In addition to the base XML standard, other standards define schemas, style sheets, links, Web services, security, and other important items.
Technologies and standards have been built around XML. Consortiums and business organizations have developed industry-wide XML formats and standardization, but the basics of XML have not changed. What has changed is the extent to which XML is being used today, and the way it is being incorporated into future technologies.
XML offers many benefits, and XML documents can be used for many purposes, including the following:
- Transporting data between applications, systems, or companies
- Generating Web pages from XML data
- Transforming XML data to another XML format
The SQL2XML allows you to extract data from your database with a standard SQL statement and generate a portable XML result set in which the columns of the selected table or tables are mapped to elements of the XML document. The generated XML document, apart from being used to exchange data between heterogeneous systems, can be used by many other client applications, like Web browsers and Web Services.
For example, Extensible Stylesheet Language Transformation (XSLT) is designed to allow you to easily transform your XML data from one form into another, most commonly HTML. Transformation to HTML is the final step before the user sees the Web page. XML was designed to describe data, and HTML was designed to display data, so the combination of XML and XSLT is growing in popularity because XSLT automates the process of changing the presentation of a Web site, keeping the data and its display separate.
A style sheet written using XSL is expressed as a well-formed XML document that describes the rules for transforming an XML document into another document whose structure may be completely different from the structure of the source document.
To give you an idea of how you can build dynamic Web pages using XML, let's use the same sample tables we used for SQL2XLS and run the following SQL2XML statements.
SQLSTMT('Select a.ticker as [Ticker], cast(b.cname as char(25)) as [Company],+
a.revenue as [Revenue], a.assets as [Assets], a.liabil as [Liabilities], +
a.equity as [Equities], a.sharesout as [Shares] +
from comp_fin a join comp_hdr b on a.ticker = b.ticker +
where sector = "Financial Services"') +
TOXML('/home/costagliol/Companies.xml') XSL('Companies.xsl') +
ROOT('Companies' ELEMENT('FinancialServices') +
MODE(*REPLACE) ENDROOT(*NO) COLHDRS(*ANY)
Leave the XML open by specifying ENDROOT(*NO). Next, run more statements to extract other financial sectors. Then, append the result to the same document by specifying MODE(*ADD). The last query must close the document with ENDROOT(*YES).
The TOXML parameter can be used to generate into the XML document an XML processing instruction used to associate this XML document to a style sheet.
Now, create your stylesheet (Companies.xls) with XSL to combine XML data with an HTML template and transform the raw XML document into a well-formatted set of tables, with titles, headers, and colors. Then, just click on your Companies.xml, and see the results as in Figure 1.
Figure 1: Companies.xml now looks like this after being transformed by Companies.xsl. (Click image to enlarge.)
I hope you liked my previous SQL2XLS utility. If so, you can download the updated version that offers more features, like adding named sheets, working with variable length fields, and starting and ending the JVM. In the same package, you will find the source files of these two new utilities presented here. Experience the performance of SQL2CSV, and have fun with SQL2XML.
Giuseppe Costagliola is a programmer in Turin, Italy. You can reach him at
LATEST COMMENTS
MC Press Online