Project

Profile

Help

How to connect?
Download (2.77 KB) Statistics
| Branch: | Tag: | Revision:

he / tags / 9.8.0.2 / samples / styles / books-sql.xsl @ 02f8308b

1
<!-- This stylesheet demonstrates the use of element extensibility with SAXON -->
2
<!-- Requires Saxon-PE or Saxon-EE -->
3

    
4
<xsl:stylesheet
5
	xmlns:sql="http://saxon.sf.net/sql"
6
 	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
7
	xmlns:saxon="http://saxon.sf.net/"
8
	xmlns:java="http://saxon.sf.net/java-type"
9
 	extension-element-prefixes="saxon"
10
 	exclude-result-prefixes="java">
11

    
12
<!-- insert your database details here, or supply them in parameters -->
13
<xsl:param name="driver" select="'sun.jdbc.odbc.JdbcOdbcDriver'"/>
14
<xsl:param name="database" select="'jdbc:odbc:test'"/>  
15
<xsl:param name="user"/>
16
<xsl:param name="password"/>
17

    
18
<!-- This stylesheet writes the book list to a SQL database -->
19

    
20
<xsl:variable name="count" select="0" saxon:assignable="yes"/>
21

    
22
<xsl:output method="xml" indent="yes"/>
23

    
24
<xsl:template match="BOOKLIST">
25
    <xsl:if test="not(element-available('sql:connect'))">
26
        <xsl:message>sql:connect is not available</xsl:message>
27
    </xsl:if>
28
    <xsl:message>Connecting to <xsl:value-of select="$database"/>...</xsl:message>
29
    <xsl:variable name="connection" as="java:java.sql.Connection?">
30
        <!-- the 'as' allows an empty sequence, because that's what's produced in the fallback case -->
31
        <sql:connect database="{$database}" user="{$user}" password="{$password}"
32
		    driver="{$driver}" xsl:extension-element-prefixes="sql">
33
	        <xsl:fallback>
34
	            <xsl:message terminate="yes">SQL extensions are not installed</xsl:message>
35
            </xsl:fallback>
36
        </sql:connect>
37
    </xsl:variable>
38
    <xsl:message>Connected...</xsl:message>
39
    <xsl:apply-templates select="BOOKS">
40
       <xsl:with-param name="connection" select="$connection"/>
41
    </xsl:apply-templates>
42
    <xsl:message>Inserted <xsl:value-of select="$count"/> records.</xsl:message>
43

    
44
    <xsl:variable name="book-table">
45
    <sql:query connection="$connection" table="book" column="*" row-tag="book" column-tag="col"
46
               xsl:extension-element-prefixes="sql"/> 
47
    </xsl:variable>
48
    
49
    <xsl:message>There are now <xsl:value-of select="count($book-table//book)"/> books.</xsl:message>
50
    <new-book-table>
51
        <xsl:copy-of select="$book-table"/>
52
    </new-book-table>
53
    
54
    <sql:close connection="$connection" xsl:extension-element-prefixes="sql">
55
       <xsl:fallback/>
56
    </sql:close>
57
</xsl:template>
58

    
59
<xsl:template match="BOOKS">
60
   <xsl:param name="connection"/>
61
    <xsl:for-each select="ITEM">
62
    	<sql:insert table="book" connection="$connection" xsl:extension-element-prefixes="sql">
63
	    <sql:column name="title" select="TITLE"/>
64
            <sql:column name="author" select="AUTHOR"/>
65
            <sql:column name="category" select="@CAT"/>
66
    	</sql:insert>
67
	<saxon:assign name="count" select="$count+1"/>
68
    </xsl:for-each>
69
</xsl:template>
70

    
71
</xsl:stylesheet>	
(4-4/12)