Menu

java Saxon xmlns Trasformer

2009-07-14
2012-10-08
  • antonio marino

    antonio marino - 2009-07-14

    HI i have a problem:i'm working in a java application to write a xml file and i can't write a default xmlns namespace importing the saxon library. I have note that the problem is that instead of using javax......Transformer.class on runtime it used the saxon Transformer class and so it dont write on a file a xmlns like xmlns="http://.....".Someone have the same problem or haso some suggest for me???? thank you.

     
    • Michael Kay

      Michael Kay - 2009-07-14

      I'm sorry, you'll have to be much more specific. If your code isn't doing what you expect, then show us the code, tell us what output you expected, and tell us what output it is producing. Then we can tell you where you went wrong.

       
    • antonio marino

      antonio marino - 2009-07-14

      this is my code :
      Element root = doc.createElement("Mpeg7");
      root.setAttribute("xmlns", "urn:mpeg:mpeg7:schema:2001");
      root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
      root.setAttribute("xmlns:mpeg7", "urn:mpeg:mpeg7:schema:2001");
      root.setAttribute("xsi:schemaLocation", "urn:mpeg:mpeg7:schema:2001 Mpeg7-2001.xsd");
      doc.appendChild(root);

      this write the file:
      Transformer tFormer = TransformerFactory.newInstance().newTransformer();
      tFormer.setOutputProperty(OutputKeys.METHOD, "xml");
      doc.normalize();
      Source sourceMPEG7 = new DOMSource(doc);
      Result result = new StreamResult(outfile);
      tFormer.transform(sourceMPEG7, result);

      (outfile is my output file xml; i don't report for semplicity the declaration of DocumentBuilderFactory and so on).
      This code works well if i don't import saxon libraires in my project, but if i import these libraries (and note that i don't call saxon function in my code), the application doesn't write the first namespace (xmlns", "urn:mpeg:mpeg7:schema:2001) and i have to write this.
      How can i do? maybe i have to set some parameter to use this namespace?
      thank you for your answers

       
      • Michael Kay

        Michael Kay - 2009-07-15

        I have filed a bug at https://sourceforge.net/tracker/?func=detail&aid=2822068&group_id=29872&atid=397617, and a patch is in Subversion.

        Generally I would recommend avoiding the old "non-namespace-aware" methods in DOM: use createElementNS() rather than createElement() (if you have to use DOM at all...). Saxon tries to handle a DOM that was constructed using these methods (at considerable cost in performance, incidentally) but they are generally fragile.

         
        • antonio marino

          antonio marino - 2009-07-16

          Thank you, i solved it using the createElementNS() for root and it seems work fine.
          thank you very much

           
      • Michael Kay

        Michael Kay - 2009-07-14

        Could you confirm which Saxon version you are using, please?

        (Generally, I think that it's best to use createElementNS() when creating namespaced elements. But the way you are doing it ought to work.)

         
    • antonio marino

      antonio marino - 2009-07-14

      i try the last version of saxon and also the 8.8 but it don't change