QueryProcessor's result has 'restructured ns declarations'

Wednesday, 19 November 2014 · 5 messages
EK
Erdal Karaca19 Nov 2014 20:37 #
Hi all, I have this file (simplified):
<r:root xmlns:r="r" xmlns:x="x" xmlns:y="y" xmlns:z="z">
  <x:x/>
  <y:y/>
  <z:z/>
</r:root>
I have created a new database from that file (using Java API) and checked in the GUI that the file's contents are as "expected" by querying the root of the database. Now, when using the QueryProcessor API to query the root of the database, I will get this re-structured namespaces xml:
<r:root xmlns:r="r">
  <x:x xmlns:x="x"/>
  <y:y xmlns:y="y"/>
  <z:z xmlns:z="z"/>
</r:root>
How can I prevent this behavior? Is there a setting to provide to the QueryProcessor? Thanks!
CG
Christian Grün19 Nov 2014 21:21 #
Hi Erdal, Sorry, I need more information in order to reproduce this. Could you provide us with an SSCCE? Christian
Quoted text · Erdal Karaca · 28 lines
On Wed, Nov 19, 2014 at 8:37 PM, Erdal Karaca <erdal.karaca.de at gmail.com> wrote: > Hi all, > I have this file (simplified): > > <r:root xmlns:r="r" xmlns:x="x" xmlns:y="y" xmlns:z="z"> > <x:x/> > <y:y/> > <z:z/> > </r:root> > > I have created a new database from that file (using Java API) and checked in > the GUI that the file's contents are as "expected" by querying the root of > the database. > > Now, when using the QueryProcessor API to query the root of the database, I > will get this re-structured namespaces xml: > > <r:root xmlns:r="r"> > <x:x xmlns:x="x"/> > <y:y xmlns:y="y"/> > <z:z xmlns:z="z"/> > </r:root> > > How can I prevent this behavior? Is there a setting to provide to the > QueryProcessor? > > Thanks! >
EK
Erdal Karaca19 Nov 2014 21:49 #
Hi Christian, I have extracted an SSCCE:
    public static void main(String[] args) throws Exception {
        String origXML = "<r:root xmlns:r='r' xmlns:x='x' xmlns:y='y'
xmlns:z='z'><x:x/><y:y/><z:z/></r:root>";
        Context dbCtx = new Context();

        // create DB
        {
            CreateDB cmd = new CreateDB("test-db", origXML);
            cmd.execute(dbCtx);
        }

        // query root of doc
        {
            QueryProcessor query = new QueryProcessor("/", dbCtx);
            Value value = query.value();
            Node node = (Node)value.toJava();
java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream(); javax.xml.transform.Transformer transformer = javax.xml.transform.TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(javax.xml.transform.OutputKeys.INDENT, "yes");
            transformer.transform(new
javax.xml.transform.dom.DOMSource(node),
                new javax.xml.transform.stream.StreamResult(out));
            System.out.println(out.toString());
        }
    }
The resulting output is:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<r:root xmlns:r="r">
<x:x xmlns:x="x"/>
<y:y xmlns:y="y"/>
<z:z xmlns:z="z"/>
</r:root>
As said, the expected result is:
<r:root xmlns:r="r" xmlns:x="x" xmlns:y="y" xmlns:z="z">
  <x:x/>
  <y:y/>
  <z:z/>
</r:root>
2014-11-19 21:21 GMT+01:00 Christian Grün <christian.gruen at gmail.com>:
Quoted text · Christian Grün · 41 lines
> Hi Erdal, > > Sorry, I need more information in order to reproduce this. Could you > provide us with an SSCCE? > > Christian > > > On Wed, Nov 19, 2014 at 8:37 PM, Erdal Karaca <erdal.karaca.de at gmail.com> > wrote: > > Hi all, > > I have this file (simplified): > > > > <r:root xmlns:r="r" xmlns:x="x" xmlns:y="y" xmlns:z="z"> > > <x:x/> > > <y:y/> > > <z:z/> > > </r:root> > > > > I have created a new database from that file (using Java API) and > checked in > > the GUI that the file's contents are as "expected" by querying the root > of > > the database. > > > > Now, when using the QueryProcessor API to query the root of the > database, I > > will get this re-structured namespaces xml: > > > > <r:root xmlns:r="r"> > > <x:x xmlns:x="x"/> > > <y:y xmlns:y="y"/> > > <z:z xmlns:z="z"/> > > </r:root> > > > > How can I prevent this behavior? Is there a setting to provide to the > > QueryProcessor? > > > > Thanks! > > >
EK
Erdal Karaca19 Nov 2014 21:52 #
BTW: I am using BaseX 7.7 2014-11-19 21:49 GMT+01:00 Erdal Karaca <erdal.karaca.de at gmail.com>:
Quoted text · Erdal Karaca · 96 lines
> Hi Christian, > I have extracted an SSCCE: > > public static void main(String[] args) throws Exception { > String origXML = "<r:root xmlns:r='r' xmlns:x='x' xmlns:y='y' > xmlns:z='z'><x:x/><y:y/><z:z/></r:root>"; > Context dbCtx = new Context(); > > // create DB > { > CreateDB cmd = new CreateDB("test-db", origXML); > cmd.execute(dbCtx); > } > > // query root of doc > { > QueryProcessor query = new QueryProcessor("/", dbCtx); > Value value = query.value(); > Node node = (Node)value.toJava(); > > java.io.ByteArrayOutputStream out = new > java.io.ByteArrayOutputStream(); > javax.xml.transform.Transformer transformer = > > javax.xml.transform.TransformerFactory.newInstance().newTransformer(); > > transformer.setOutputProperty(javax.xml.transform.OutputKeys.INDENT, "yes"); > transformer.transform(new > javax.xml.transform.dom.DOMSource(node), > new javax.xml.transform.stream.StreamResult(out)); > System.out.println(out.toString()); > } > } > > The resulting output is: > > <?xml version="1.0" encoding="UTF-8" standalone="no"?> > <r:root xmlns:r="r"> > <x:x xmlns:x="x"/> > <y:y xmlns:y="y"/> > <z:z xmlns:z="z"/> > </r:root> > > As said, the expected result is: > > <r:root xmlns:r="r" xmlns:x="x" xmlns:y="y" xmlns:z="z"> > <x:x/> > <y:y/> > <z:z/> > </r:root> > > 2014-11-19 21:21 GMT+01:00 Christian Grün <christian.gruen at gmail.com>: > >> Hi Erdal, >> >> Sorry, I need more information in order to reproduce this. Could you >> provide us with an SSCCE? >> >> Christian >> >> >> On Wed, Nov 19, 2014 at 8:37 PM, Erdal Karaca <erdal.karaca.de at gmail.com> >> wrote: >> > Hi all, >> > I have this file (simplified): >> > >> > <r:root xmlns:r="r" xmlns:x="x" xmlns:y="y" xmlns:z="z"> >> > <x:x/> >> > <y:y/> >> > <z:z/> >> > </r:root> >> > >> > I have created a new database from that file (using Java API) and >> checked in >> > the GUI that the file's contents are as "expected" by querying the root >> of >> > the database. >> > >> > Now, when using the QueryProcessor API to query the root of the >> database, I >> > will get this re-structured namespaces xml: >> > >> > <r:root xmlns:r="r"> >> > <x:x xmlns:x="x"/> >> > <y:y xmlns:y="y"/> >> > <z:z xmlns:z="z"/> >> > </r:root> >> > >> > How can I prevent this behavior? Is there a setting to provide to the >> > QueryProcessor? >> > >> > Thanks! >> > >> > >
CG
Christian Grünin reply to Erdal Karaca, 19 Nov 2014, 21:4919 Nov 2014 22:00 #
It may be easier to stick with the built-in serializer of BaseX (it's faster anyway): Old:
Quoted text · Erdal Karaca · 11 lines
> java.io.ByteArrayOutputStream out = new > java.io.ByteArrayOutputStream(); > javax.xml.transform.Transformer transformer = > > javax.xml.transform.TransformerFactory.newInstance().newTransformer(); > > transformer.setOutputProperty(javax.xml.transform.OutputKeys.INDENT, "yes"); > transformer.transform(new > javax.xml.transform.dom.DOMSource(node), > new javax.xml.transform.stream.StreamResult(out)); > System.out.println(out.toString());
New: System.out.println(value.serialize()); Alternative:
  ByteArrayOutputStream ao = new ByteArrayOutputStream();
  Serializer ser = query.getSerializer(ao);
  for(Item item : value) ser.serialize(item);
  System.out.println(ao);

⚡Generated with XQuery