Problem with XQJ-Implementation (method XQResultSequence.getSequenceAsStream)

Tuesday, 3 April 2012 · 3 messages
IG
Isidre Guixà3 Apr 2012 09:35 #
Hi I am experiencing the BaseX API XQJ and I compare to other XQJ NXD and I find the following problem. Suppose we have the next XML file in database "test":
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore param="hao">
<book>
<title>Book01</title>
</book>
<book>
<title>Book02</title>
</book>
</bookstore>
If I execute:
     XQDataSource xqs = ...
     XQConnection conn = xqs.getConnection();
     XQExpression xqe = conn.createExpression();
     String cad= "collection('test')//title";

     XQResultSequence xqrs = xqe.executeQuery(cad);
     XMLStreamReader result = xqrs.getSequenceAsStream();
     System.out.println("Iteration on XMLStreamReader...");
     for(; result.hasNext(); result.next())
       System.out.println(getEventString(result));

     xqrs = xqe.executeQuery(cad);
     System.out.println("Using a Transformer...");
     Source resultXML = new StAXSource(xqrs.getSequenceAsStream());
     StreamResult resultSR = new StreamResult(System.out);
     TransformerFactory factory = TransformerFactory.newInstance();
     Transformer tr = factory.newTransformer();
     tr.setOutputProperty(OutputKeys.INDENT, "yes");
     tr.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
     tr.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","2");
     tr.transform(resultXML, resultSR);

where
   static String getEventString(final XMLStreamReader reader) {
     switch(reader.getEventType()) {
       case XMLStreamConstants.START_ELEMENT:
         return "START_ELEMENT:\t\"" + reader.getLocalName() + "\"";
       case XMLStreamConstants.END_ELEMENT:
         return "END_ELEMENT:\t\"" + reader.getLocalName() + "\"";
       case XMLStreamConstants.START_DOCUMENT:
         return "START_DOCUMENT";
       case XMLStreamConstants.END_DOCUMENT:
         return "END_DOCUMENT";
       case XMLStreamConstants.CHARACTERS:
         return "CHARACTERS:\t\"" + reader.getText() + "\"";
     }
     return "";
   }
I get: Iteration on XMLStreamReader... START_ELEMENT: "title" CHARACTERS: "Book01" END_ELEMENT: "title" END_DOCUMENT START_ELEMENT: "title" CHARACTERS: "Book02" END_ELEMENT: "title" END_DOCUMENT Using a Transformer... <?xml version="1.0" encoding="UTF-8"?> <title>Book01</title> It's not good! When I use a Transformer I should get something similar to:
     <?xml version="1.0" encoding="UTF-8"?>
     <result>
       <title>Book01</title>
       <title>Book02</title>
     <result>
That is, if result sequence length is greater than 1, getSequenceAsStream should wrap the sequence in a containing result element, so to not break JAXP Transformers and XMLStreamReader events. Is there any solution in BaseX ? Thanks a lot! Isidre Guixà
CG
Christian Grün3 Apr 2012 10:37 #
Hi Isidre, as our XQJ implementation will soon be replaced by a new version of Charles Foster (who also did the versions for eXist, Sedna and MarkLogic), we won't put any more effort into the existing code. The new version will also be based on the client/server architecture of BaseX. Hope this helps, Christian ___________________________ 2012/4/3 Isidre Guixà <iguixa at xtec.cat>:
Quoted text · Isidre Guixà · 92 lines
> Hi > > I am experiencing the BaseX API XQJ and I compare to other XQJ NXD and I > find the following problem. > > Suppose we have the next XML file in database "test": > > <?xml version="1.0" encoding="ISO-8859-1"?> > <bookstore param="hao"> > <book> > <title>Book01</title> > </book> > <book> > <title>Book02</title> > </book> > </bookstore> > > If I execute: > > XQDataSource xqs = ... > XQConnection conn = xqs.getConnection(); > XQExpression xqe = conn.createExpression(); > String cad= "collection('test')//title"; > > XQResultSequence xqrs = xqe.executeQuery(cad); > XMLStreamReader result = xqrs.getSequenceAsStream(); > System.out.println("Iteration on XMLStreamReader..."); > for(; result.hasNext(); result.next()) > System.out.println(getEventString(result)); > > xqrs = xqe.executeQuery(cad); > System.out.println("Using a Transformer..."); > Source resultXML = new StAXSource(xqrs.getSequenceAsStream()); > StreamResult resultSR = new StreamResult(System.out); > TransformerFactory factory = TransformerFactory.newInstance(); > Transformer tr = factory.newTransformer(); > tr.setOutputProperty(OutputKeys.INDENT, "yes"); > tr.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); > tr.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","2"); > tr.transform(resultXML, resultSR); > > where > static String getEventString(final XMLStreamReader reader) { > switch(reader.getEventType()) { > case XMLStreamConstants.START_ELEMENT: > return "START_ELEMENT:\t\"" + reader.getLocalName() + "\""; > case XMLStreamConstants.END_ELEMENT: > return "END_ELEMENT:\t\"" + reader.getLocalName() + "\""; > case XMLStreamConstants.START_DOCUMENT: > return "START_DOCUMENT"; > case XMLStreamConstants.END_DOCUMENT: > return "END_DOCUMENT"; > case XMLStreamConstants.CHARACTERS: > return "CHARACTERS:\t\"" + reader.getText() + "\""; > } > return ""; > } > > I get: > > Iteration on XMLStreamReader... > START_ELEMENT: "title" > CHARACTERS: "Book01" > END_ELEMENT: "title" > END_DOCUMENT > START_ELEMENT: "title" > CHARACTERS: "Book02" > END_ELEMENT: "title" > END_DOCUMENT > Using a Transformer... > <?xml version="1.0" encoding="UTF-8"?> > <title>Book01</title> > > It's not good! > > When I use a Transformer I should get something similar to: > <?xml version="1.0" encoding="UTF-8"?> > <result> > <title>Book01</title> > <title>Book02</title> > <result> > > That is, if result sequence length is greater than 1, getSequenceAsStream > should wrap the sequence in a containing result element, so to not break > JAXP Transformers and XMLStreamReader events. > > Is there any solution in BaseX ? > > Thanks a lot! > > > Isidre Guixà
CF
Charles Foster3 Apr 2012 10:54 #
Isidre, The client/server version of the BaseX Driver will do exactly what you want Isidre, in exactly the same way the Sedna, eXist and MarkLogic drivers do. It will not be long until this driver will be available. Regards, Charles
Quoted text · Christian Grün · 108 lines
> Hi Isidre, > > as our XQJ implementation will soon be replaced by a new version of > Charles Foster (who also did the versions for eXist, Sedna and > MarkLogic), we won't put any more effort into the existing code. The > new version will also be based on the client/server architecture of > BaseX. > > Hope this helps, > Christian > ___________________________ > > 2012/4/3 Isidre Guixà <iguixa at xtec.cat>: >> Hi >> >> I am experiencing the BaseX API XQJ and I compare to other XQJ NXD and I >> find the following problem. >> >> Suppose we have the next XML file in database "test": >> >> <?xml version="1.0" encoding="ISO-8859-1"?> >> <bookstore param="hao"> >> <book> >> <title>Book01</title> >> </book> >> <book> >> <title>Book02</title> >> </book> >> </bookstore> >> >> If I execute: >> >> XQDataSource xqs = ... >> XQConnection conn = xqs.getConnection(); >> XQExpression xqe = conn.createExpression(); >> String cad= "collection('test')//title"; >> >> XQResultSequence xqrs = xqe.executeQuery(cad); >> XMLStreamReader result = xqrs.getSequenceAsStream(); >> System.out.println("Iteration on XMLStreamReader..."); >> for(; result.hasNext(); result.next()) >> System.out.println(getEventString(result)); >> >> xqrs = xqe.executeQuery(cad); >> System.out.println("Using a Transformer..."); >> Source resultXML = new StAXSource(xqrs.getSequenceAsStream()); >> StreamResult resultSR = new StreamResult(System.out); >> TransformerFactory factory = TransformerFactory.newInstance(); >> Transformer tr = factory.newTransformer(); >> tr.setOutputProperty(OutputKeys.INDENT, "yes"); >> tr.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); >> >> tr.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","2"); >> tr.transform(resultXML, resultSR); >> >> where >> static String getEventString(final XMLStreamReader reader) { >> switch(reader.getEventType()) { >> case XMLStreamConstants.START_ELEMENT: >> return "START_ELEMENT:\t\"" + reader.getLocalName() + "\""; >> case XMLStreamConstants.END_ELEMENT: >> return "END_ELEMENT:\t\"" + reader.getLocalName() + "\""; >> case XMLStreamConstants.START_DOCUMENT: >> return "START_DOCUMENT"; >> case XMLStreamConstants.END_DOCUMENT: >> return "END_DOCUMENT"; >> case XMLStreamConstants.CHARACTERS: >> return "CHARACTERS:\t\"" + reader.getText() + "\""; >> } >> return ""; >> } >> >> I get: >> >> Iteration on XMLStreamReader... >> START_ELEMENT: "title" >> CHARACTERS: "Book01" >> END_ELEMENT: "title" >> END_DOCUMENT >> START_ELEMENT: "title" >> CHARACTERS: "Book02" >> END_ELEMENT: "title" >> END_DOCUMENT >> Using a Transformer... >> <?xml version="1.0" encoding="UTF-8"?> >> <title>Book01</title> >> >> It's not good! >> >> When I use a Transformer I should get something similar to: >> <?xml version="1.0" encoding="UTF-8"?> >> <result> >> <title>Book01</title> >> <title>Book02</title> >> <result> >> >> That is, if result sequence length is greater than 1, >> getSequenceAsStream >> should wrap the sequence in a containing result element, so to not break >> JAXP Transformers and XMLStreamReader events. >> >> Is there any solution in BaseX ? >> >> Thanks a lot! >> >> >> Isidre Guixà >

⚡Generated with XQuery