3

I am trying to query MySQL database from XSLT2.0 using SQL query and populate a parameter in my XSLT file. I am using saxon9ee.jar along with saxon9-sql.jar.

I found the this link, but it didn't resolve my issue.

I am getting below error :

Connecting MySQL...
Connected...
Error on line 43 of DB_Query_XSLT_Test.xsl:
  XTDE1450: Unknown extension instruction
; SystemID: file:///C:/Users/davo/platform/src/config/xslt/DB_Query_XSLT_Test.xsl; Line#: 43; Column#: -1
net.sf.saxon.trans.XPathException: Unknown extension instruction
    at net.sf.saxon.expr.ErrorExpression.evaluateItem(ErrorExpression.java:58)
    at net.sf.saxon.expr.ErrorExpression.iterate(ErrorExpression.java:71)
    at net.sf.saxon.expr.Expression.process(Expression.java:467)
    at net.sf.saxon.instruct.DocumentInstr.evaluateItem(DocumentInstr.java:305)
    at net.sf.saxon.functions.StringFn.evaluateItem(StringFn.java:62)
    at net.sf.saxon.instruct.ValueOf.processLeavingTail(ValueOf.java:246)
    at net.sf.saxon.instruct.Block.processLeavingTail(Block.java:619)
    at net.sf.saxon.instruct.Instruction.process(Instruction.java:93)
    at net.sf.saxon.instruct.ElementCreator.processLeavingTail(ElementCreator.java:301)
    at net.sf.saxon.instruct.Instruction.process(Instruction.java:93)
    at net.sf.saxon.instruct.ElementCreator.processLeavingTail(ElementCreator.java:301)
    at net.sf.saxon.instruct.Instruction.process(Instruction.java:93)
    at net.sf.saxon.instruct.ElementCreator.processLeavingTail(ElementCreator.java:301)
    at net.sf.saxon.instruct.Block.processLeavingTail(Block.java:619)
    at net.sf.saxon.instruct.Instruction.process(Instruction.java:93)
    at net.sf.saxon.instruct.ElementCreator.processLeavingTail(ElementCreator.java:301)
    at net.sf.saxon.instruct.Template.applyLeavingTail(Template.java:229)
    at net.sf.saxon.instruct.ApplyTemplates.applyTemplates(ApplyTemplates.java:354)
    at net.sf.saxon.Controller.transformDocument(Controller.java:1849)
    at net.sf.saxon.Controller.transform(Controller.java:1700)
    at com.alu.ipprd.aor.fsw.tt.util.xml.transform.XSLTUtil.transformXML(XSLTUtil.java:52)
    at com.alu.ipprd.aor.fsw.tt.util.xml.transform.XSLTUtil.main(XSLTUtil.java:214)
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                  xmlns:tro="http://www.example.org/TroubleTicketWS_v3/">
   <soapenv:Header/>
   <soapenv:Body>
      <tro:CreateTroubleTicketByValueRequest

XSLT:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:sql="http://saxon.sf.net/sql" 
    extension-element-prefixes="sql" 
    exclude-result-prefixes="xs">

    <xsl:output method="xml" encoding="UTF-8" indent="yes" />

    <xsl:variable name="driver" select="'com.mysql.jdbc.Driver'"
        as="xs:string" />
    <xsl:variable name="database"
        select="'jdbc:mysql://xxx.xxx.xxx.xxx:3306/platform'" as="xs:string" />
    <xsl:variable name="user" select="'dbremote'" as="xs:string" />
    <xsl:variable name="password" select="'dbremote'" as="xs:string" />

    <xsl:template match="/">
        <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
            xmlns:tro="http://www.example.org/TroubleTicketWS_v3/">
            <soapenv:Header />
            <soapenv:Body>
                <tro:CreateTroubleTicketByValueRequest>
                    <ticketkey>
                        <xsl:message>Connecting MySQL...</xsl:message>
                        <xsl:variable name="connection" as="java:java.sql.Connection" xmlns:java="http://saxon.sf.net/java-type">
                            <sql:connect driver="{$driver}" database="{$database}" user="{$user}" password="{$password}">
                                <xsl:fallback>
                                    <xsl:message terminate="yes">Connection to MySQL failed.</xsl:message>
                                </xsl:fallback>
                            </sql:connect>
                        </xsl:variable>

                        <xsl:message>Connected...</xsl:message>

                        <xsl:variable name="internalticketid" select="/tTroubleticket/internalticketid" />

                        <xsl:variable name="troubleticketkey">
                            <sql:query connection="$connection" table="T_TROUBLETICKET"
                                column="TROUBLETICKETKEY" row-tag="ticket" column-tag="ttf"
                                where="INTERNALTICKETID={$internalticketid}" />
                        </xsl:variable>

                        <xsl:value-of select="$troubleticketkey" />
                    </ticketkey>
                </tro:CreateTroubleTicketByValueRequest>
            </soapenv:Body>
        </soapenv:Envelope>
    </xsl:template>
</xsl:stylesheet>

Java Class:

package com.fsw.tt.util.xml.transform;

import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

//import net.sf.saxon.TransformerFactoryImpl;
import com.saxonica.config.EnterpriseTransformerFactory; 

public class XSLTUtil {

    TransformerFactory factory = null;
    public XSLTUtil() {
        //factory = new TransformerFactoryImpl();
        factory = new EnterpriseTransformerFactory();
    }

    public String transformXML(String inXML, String XSLTFilename) throws TransformerFactoryConfigurationError, TransformerException {
        StringWriter xmlResultResource = new StringWriter();
        try {
            Source xslDoc = new StreamSource(XSLTFilename);
            Transformer transformer = factory.newTransformer(xslDoc);
            transformer.transform(new StreamSource(new StringReader(inXML)), new StreamResult(xmlResultResource));
        } catch (TransformerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return xmlResultResource.getBuffer().toString();
    }

    public static void main(String[] args) throws TransformerFactoryConfigurationError, TransformerException {

        String xmlSourceResource = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" 
            + "<tTroubleticket>" 
                + "<internalticketid>INC0789543765790</internalticketid>"
            + "</tTroubleticket>";

        String xsltFilename = "C:/Users/davo/platform/src/config/xslt/DB_Query_XSLT_Test.xsl";

        XSLTUtil util = new XSLTUtil();
        String transformedXML = util.transformXML(xmlSourceResource, xsltFilename);
        System.out.println(transformedXML);

    }
}

The error is thrown at Line Number 43 in my XSLT, which is <xsl:value-of select="$troubleticketkey" />.

I am not sure what's wrong in this line.

I am not even sure if it is connecting to MySQL DB also.

Please let me know if I am missing something.

Community
  • 1
  • 1
dev
  • 1,343
  • 2
  • 19
  • 40
  • Are you sure your Java code creates a `Transformer` using Saxon EE? Can you debug and see which class your created `transformer` has? – Martin Honnen Feb 03 '16 at 13:52

1 Answers1

1

You have import net.sf.saxon.TransformerFactoryImpl; and factory = new TransformerFactoryImpl();, to use Saxon EE you need import com.saxonica.config.EnterpriseTransformerFactory; and factory = new EnterpriseTransformerFactory();. See http://saxonica.com/html/documentation9.6/using-xsl/embedding/jaxp-transformation.html.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • Do you know the maven dependency which can be added to pom.xml to support Saxon EE ? – dev Feb 03 '16 at 14:12
  • After updating the suggested change also, I am getting same error. – dev Feb 03 '16 at 14:27
  • Is the stack trace not at least different now? Does it show any indication that EE is used instead of HE? Do you have license for Saxon EE on the class path? Which version of Saxon EE exactly do you use? – Martin Honnen Feb 03 '16 at 15:03
  • There is no change in the trace. I have added saxon9ee.jar in classpath. I am not sure what do you mean by licence here. – dev Feb 03 '16 at 15:07
  • You need a license file from Saxonica to be able to use Saxon EE or PE, and that license file needs to be on the class path. Do you have a license from Saxonica? If not, you will first need to buy a license or at least request a trial license from them to be able to use EE or PE. – Martin Honnen Feb 03 '16 at 15:12
  • Okay, I will get the trial licence and see if it works or not. Thank you. – dev Feb 03 '16 at 15:16
  • Is there any other third party which can do SQL queries in XSLT. Saxon HE doesn't support SQL in XSLT, is that true ? – dev Feb 03 '16 at 16:45
  • Saxon HE does not support that, if you want an open source product you might want to check whether the old Saxon 9.1 B release from http://saxon.sourceforge.net/ does support it, I am not sure whether it does, but both the code download as well as the documentation download should be available on sourceforge to check. – Martin Honnen Feb 03 '16 at 17:18