Friday, 25 March 2016

XML : Using Saxon Extension in XSLT to connect to SQL server NullPointerException

Scenario: Transforming XML to XML, but some fields I need to do a lookup in an existing database table to populate in the output XML. I'm using SQL Server 2014. I've downloaded the Microsoft JDBC driver version 4.1. The ODBC driver has been setup under Datasources. I'm able to connect to the database locally using my Windows Authentication. When I compile the stylesheet however, a "nullPointerexception" occurs. I'm not sure what I'm doing wrong or if there are additional configuration settings that need to be set on my PC.

Operating System: Windows 7 Saxon Version: 9.5.1.8 TCP/IP has been enabled in the Configuration Tool for SQL Server IDE used to create stylesheet is Stylus Studio.

Below is the code snippet from the stylehsheet:

  <?xml version='1.0' ?>  <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"   xmlns:xs="http://www.w3.org/2001/XMLSchema"   extension-element-prefixes="saxon sql"    xmlns:sql="java:/net.sf.saxon.sql.SQLElementFactory"   xmlns:java="http://saxon.sf.net/java-type"   xmlns:saxon="http://saxon.sf.net/">    <xsl:variable name="count" select="0"/>  <xsl:variable name="date" select="current-date"/>  <xsl:variable name="time" select="current-time"/>    <!-- insert your database details here, or supply them in parameters -->  <xsl:param name="jdbc.driver" as="xs:string" select="string('com.microsoft.sqlserver.jdbc.SQLServerDriver')"/>  <xsl:param name="jdbc.database" as="xs:string" select="string('jdbc:sqlserver://localhost:1433;integratedSecurity=true;databaseName=master')"/>        <xsl:template match="/">    <xsl:choose>      <xsl:when test="element-available('sql:connect')">          <xsl:variable name="sql.conn" as="java:java.sql.Connection">              <sql:connect driver="{$jdbc.driver}" database="{$jdbc.database}">              <xsl:fallback>                  <xsl:message terminate="yes"> SQL extensions are not installed</xsl:message>              </xsl:fallback>              </sql:connect>              </xsl:variable>              <sql:close connection="$sql.conn"/>      </xsl:when>      <xsl:otherwise>          <xsl:message terminate="yes">sql:connect element is not available</xsl:message>      </xsl:otherwise>   </xsl:choose>  .......         Result is a "java.lang.NullPointerException"  java.lang.NullPointerException  at net.sf.saxon.expr.Expression.computeDependencies(Expression.java:979)  at net.sf.saxon.expr.Expression.computeStaticProperties(Expression.java:927)  at net.sf.saxon.expr.Expression.getDependencies(Expression.java:351)  at net.sf.saxon.expr.instruct.TraceExpression.getDependencies(TraceExpression.java:352)  at net.sf.saxon.expr.Expression.computeDependencies(Expression.java:979)  at net.sf.saxon.expr.Expression.computeStaticProperties(Expression.java:927)  at net.sf.saxon.expr.Expression.getDependencies(Expression.java:351)  at net.sf.saxon.expr.Expression.computeDependencies(Expression.java:979)  at net.sf.saxon.expr.Expression.computeStaticProperties(Expression.java:927)  at net.sf.saxon.expr.Expression.getCardinality(Expression.java:312)  at net.sf.saxon.style.SourceBinding.getInferredType(SourceBinding.java:484)  at net.sf.saxon.style.XSLLocalVariable.getRequiredType(XSLLocalVariable.java:61)  at net.sf.saxon.style.StyleElement.compileSequenceConstructor(StyleElement.java:1607)  at net.sf.saxon.style.XSLChoose.compile(XSLChoose.java:121)  at net.sf.saxon.style.StyleElement.compileSequenceConstructor(StyleElement.java:1634)  at net.sf.saxon.style.XSLTemplate.compileDeclaration(XSLTemplate.java:325)  at net.sf.saxon.style.PrincipalStylesheetModule.compileStylesheet(PrincipalStylesheetModule.java:773)  at net.sf.saxon.PreparedStylesheet.setStylesheetDocument(PreparedStylesheet.java:393)  at net.sf.saxon.PreparedStylesheet.prepare(PreparedStylesheet.java:236)  at net.sf.saxon.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:142)  at net.sf.saxon.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:97)  at com.exln.stylus.CSaxon8Driver.doProcessing(CSaxon8Driver.java:224)  at com.exln.stylus.CProcessorDriver.process(CProcessorDriver.java:116)  java.lang.RuntimeException    

No comments:

Post a Comment