Accessing a Database in Xalan

Xalan provides a set of extension functions that give us access to any JDBC data source. The extension functions we’ll use are sql:new() to create a new database connection, sql:query() to execute a query, and sql:close() to close the connection. In addition, we’ll use the function sql:disableStreamingMode() to get around a “feature” of the Xalan SQL library.

Here’s the stylesheet:

<?xml version="1.0"?>
<!-- xalan-sql.xsl -->
<xsl:stylesheet version="1.0" 

  <xsl:output method="html"/>

  <xsl:template match="/">

    <!-- Create the JDBC connection -->
    <xsl:variable name="books"

    <!-- Workaround for a bug in Xalan’s SQL extension? -->
    <xsl:variable name="streaming"

    <!-- Run the query -->
    <xsl:variable name="queryResults"
      select="sql:query($books, 'select * from doug.compbks')"/>

        <title>Computer books in our database</title>
      <body style="font-family: sans-serif;">
        <h1>Computer books in our database</h1>
        <p>Here are the 
        <xsl:value-of select="count($queryResults/sql/row-set/row)"/>
        computer books we have in stock:</p>
        <table border="1" cellpadding="5">
            <xsl:for-each select="$queryResults/sql/metadata/column-header">
 <xsl:value-of select="@column-label"/> ...

Get XSLT, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.