Calling XLST Module from Java
Tuesday, 22 March 2016 · 4 messages
TJ
Taschner, Jan22 Mar 2016 08:41 #
Hello Guys,
I´ve been messing around with BaseX for a few days and now I am kind of stuck.
I have a XSL 2.0 Transformation sheet which works perfectly fine for a standalone application using Saxon HE. Now I tried to use the XSLT module of BaseX with Java. Here is my code:
import java.io.IOException;
import org.basex.core.Context;
import org.basex.core.cmd.CreateDB;
import org.basex.core.cmd.List;
import org.basex.core.cmd.XQuery;
public class App {
public static void main(String[] args) {
try {
// initialize timer
final long time = System.nanoTime();
// create session
final BaseXClient session = new BaseXClient("localhost", 1984, "admin", "admin");
System.out.println(session.execute("info"));
// create db context
Context ctx = new Context();
// create db
new CreateDB("stammdaten", "stammdaten2.xml").execute(ctx);
// show all databases
System.out.print(new List().execute(ctx));
// xls transformation
System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");
String query = "declare option output:method 'xml';"
+ " declare option db:chop 'no';"
+ " let $in := db:open('stammdaten')"
+ " let $style := doc('template.xsl')"
+ " return xslt:transform($in, $style)";
String result = new XQuery(query).execute(ctx);
System.out.println("Result: " + result);
ctx.close();
// close session
session.close();
// print time needed
final double ms = (System.nanoTime() - time) / 1000000d;
System.out.println("\n\n" + ms + " ms");
} catch(final IOException ex) {
// print exception
ex.printStackTrace();
}
}
}I have started the basexhttp.bat in the bin directory and executed the above code. I get the following console output and stack trace:
General Information:
Version: 8.4.1
Used Memory: 20405 KB
Global options:
ASYNCTIMEOUT: 3600
AUTHMETHOD: Basic
CACHERESTXQ: false
DBPATH: C:/Program Files (x86)/BaseX/data
DEBUG: false
GLOBALLOCK: false
HOST: localhost
HTTPLOCAL: false
IGNORECERT: false
KEEPALIVE: 600
LANG: English
LANGKEYS: false
LOG: true
LOGMSGMAXLEN: 1000
NONPROXYHOSTS:
PARALLEL: 8
PASSWORD:
PORT: 1984
PROXYHOST:
PROXYPORT: 0
REPOPATH: C:/Program Files (x86)/BaseX/repo
RESTPATH:
RESTXQPATH:
SERVERHOST:
SERVERPORT: 1984
STOPPORT: 8985
TIMEOUT: 30
USER:
WEBPATH: C:\Program Files (x86)\BaseX\webapp
Local options
ADDARCHIVES: true
ADDCACHE: false
ADDRAW: false
ARCHIVENAME: false
ATTRINCLUDE:
ATTRINDEX: true
AUTOFLUSH: true
AUTOOPTIMIZE: false
BINDINGS:
CASESENS: false
CATFILE:
CHECKSTRINGS: true
CHOP: true
COMPPLAN: true
CREATEFILTER: *.xml
CREATEONLY: false
CSVPARSER:
DEFAULTDB: false
DIACRITICS: false
DOTCOMPACT: false
DOTPLAN: false
DTD: false
EXPORTER:
FORCECREATE: false
FTINCLUDE:
FTINDEX: false
HTMLPARSER:
INLINELIMIT: 100
INTPARSE: false
JSONPARSER:
LANGUAGE: en
LSERROR: 0
MAINMEM: false
MAXCATS: 100
MAXLEN: 96
MAXSTAT: 30
MIXUPDATES: false
PARSER: xml
QUERYINFO: false
QUERYPATH:
RUNQUERY: true
RUNS: 1
SERIALIZE: true
SERIALIZER:
SKIPCORRUPT: false
SPLITSIZE: 0
STEMMING: false
STOPWORDS:
STRIPNS: false
TAILCALLS: 256
TEXTINCLUDE:
TEXTINDEX: true
TEXTPARSER:
TOKENINCLUDE:
TOKENINDEX: false
UPDINDEX: false
WRITEBACK: false
XINCLUDE: true
XMLPLAN: false
Name Resources Size Input Path
----------------------------------------------------------------------------------------------------------------------------- stammdaten 1 812176 D:/Projekte/XYZ/workspaces/workspace_poc/BaseX/stammdaten2.xml1 database(s).
org.basex.core.BaseXException: Stopped at ., 1/154:
[FODC0002] "" (Line 1): Vorzeitiges Dateiende.
at org.basex.core.Command.execute(Command.java:101)
at org.basex.core.Command.execute(Command.java:123)
at net.atos.epi.XMLTransformationProofOfConceptBaseX.App.main(App.java:31)
Caused by: org.basex.query.QueryException: "" (Line 1): Vorzeitiges Dateiende.
at org.basex.query.QueryError.get(QueryError.java:1344)
at org.basex.query.func.xslt.XsltTransform.item(XsltTransform.java:39)
at org.basex.query.expr.ParseExpr.iter(ParseExpr.java:44)
at org.basex.query.MainModule.iter(MainModule.java:110)
at org.basex.query.QueryContext.iter(QueryContext.java:342)
at org.basex.query.QueryProcessor.iter(QueryProcessor.java:81)
at org.basex.core.cmd.AQuery.query(AQuery.java:90)
at org.basex.core.cmd.XQuery.run(XQuery.java:22)
at org.basex.core.Command.run(Command.java:398)
at org.basex.core.Command.execute(Command.java:100)
... 2 more
Caused by: java.io.IOException: "" (Line 1): Vorzeitiges Dateiende.
at org.basex.build.xml.SAXWrapper.parse(SAXWrapper.java:83)
at org.basex.build.SingleParser.parse(SingleParser.java:33)
at org.basex.build.Builder.parse(Builder.java:73)
at org.basex.build.MemBuilder.dataClip(MemBuilder.java:77)
at org.basex.build.MemBuilder.build(MemBuilder.java:66)
at org.basex.build.MemBuilder.build(MemBuilder.java:61)
at org.basex.build.MemBuilder.build(MemBuilder.java:50)
at org.basex.build.MemBuilder.build(MemBuilder.java:40)
at org.basex.query.value.node.DBNode.<init>(DBNode.java:85)
at org.basex.query.func.xslt.XsltTransform.item(XsltTransform.java:37)
... 10 more
Caused by: org.xml.sax.SAXParseException; systemId: file:///D:/Projekte/XYZ/workspaces/workspace_poc/BaseX/; lineNumber: 1; columnNumber: 1; Vorzeitiges Dateiende.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at org.basex.build.xml.SAXWrapper.parse(SAXWrapper.java:78)
... 19 more(Vorzeitiges Dateiende = Premature end of file)
When I run my standalone application with stammdaten2.xml and my template.xsl everything is fine, so I doubt that the stylesheet is the problem. The stylesheet is rather complex and consists of several subtemplates, which are included with <xsl: include href="xyz.xsl" />.
Running the XQuery in the BaseX GUI does not work, since the GUI only provides support for XLST 1.0. Is there a way to change that?
The stacktrace tells me that the system tries to open and parse an empty file, but I have no idea why and which file causes the problem. My assumption is the template.xsl causes the problem, since it said that it "Stopped at ." I copied the stylesheets in every folder that was reasonable for me, but I still got this error. Please tell me what I did wrong.
Best regards,
Jan Taschner
CG
Christian Grün22 Mar 2016 09:44 #
Hi Jan,
Quoted text · Taschner, Jan · 1 line
> + " return xslt:transform($in, $style)";
Maybe your transformation does not return a single XML document. Did you try
xslt:transform-text ?Quoted text · Taschner, Jan · 2 lines
> Running the XQuery in the BaseX GUI does not work, since the GUI only > provides support for XLST 1.0. Is there a way to change that?
How do you start the GUI?
Cheers,
Christian
TJ
Taschner, Jan22 Mar 2016 10:01 #
Hello Christian,
Wow, changing it to
xslt:transform-text worked. It is true that my transformation returns multiple XML documents.
I am just a beginner in XSLT and BaseX and I did not realized that I have to use xslt:transform-text(...), when my transformation outputs multiple XML files. Maybe you could add that to the documentation?Quoted text · Christian Grün · 1 line
> How do you start the GUI?
Well either by clicking on the link on the desktop, which was created during the installation of BaseX or by running the basexgui.bat in the bin folder. But since I am interested in the Java interface of BaseX that is not very important for me.
Thanks for your help and best regards,
Jan
Quoted text · Christian Grün · 14 lines
-----Original Message----- From: Christian Grün [mailto:christian.gruen at gmail.com] Sent: Tuesday, March 22, 2016 9:45 AM To: Taschner, Jan Cc: basex-talk at mailman.uni-konstanz.de Subject: Re: [basex-talk] Calling XLST Module from Java Hi Jan, > + " return xslt:transform($in, $style)"; Maybe your transformation does not return a single XML document. Did you try xslt:transform-text ? > Running the XQuery in the BaseX GUI does not work, since the GUI only > provides support for XLST 1.0. Is there a way to change that? How do you start the GUI? Cheers, Christian
CG
Christian Grün22 Mar 2016 10:17 #
Quoted text · Taschner, Jan · 1 line
> Maybe you could add that to the documentation?
I’ll be happy to do so. Do you have a good proposal in mind which text to add?
Quoted text · Taschner, Jan · 2 lines
>> How do you start the GUI? > Well either by clicking on the link on the desktop, which was created during the installation of BaseX or by running the basexgui.bat in the bin folder. But since I am interested in the Java interface of BaseX that is not very important for me.
Hm, it shouldn’t make a difference it you run basex.bat or
basexgui.bat; in both cases, the jar files in the lib directory will be added to the classpath..
Quoted text · Taschner, Jan · 1 line
> Thanks for your help and best regards,
You are welcome,
Christian
Quoted text · Taschner, Jan · 22 lines
> Jan > > -----Original Message----- > From: Christian Grün [mailto:christian.gruen at gmail.com] > Sent: Tuesday, March 22, 2016 9:45 AM > To: Taschner, Jan > Cc: basex-talk at mailman.uni-konstanz.de > Subject: Re: [basex-talk] Calling XLST Module from Java > > Hi Jan, > >> + " return xslt:transform($in, $style)"; > > Maybe your transformation does not return a single XML document. Did you try xslt:transform-text ? > >> Running the XQuery in the BaseX GUI does not work, since the GUI only >> provides support for XLST 1.0. Is there a way to change that? > > How do you start the GUI? > > Cheers, > Christian
Written for BaseX 8.4. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery