You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by "Brian Minchau (JIRA)" <xa...@xml.apache.org> on 2004/11/09 21:15:24 UTC

[jira] Commented: (XALANJ-1985) Problem with namespaces in ToXMLStream

     [ http://nagoya.apache.org/jira/browse/XALANJ-1985?page=comments#action_55243 ]
     
Brian Minchau commented on XALANJ-1985:
---------------------------------------

Nick,
your code is in error in these three ways. 

1) You shouldn't mix namespace aware DOM methods with ones that aren't namespace aware. You have used createElement() which is not namespace aware with setAttributeNS() that is namespace aware. 

2) You have not set the document builder factory as namespace aware

3) Though not an error, you should be using the public APIs to get a serializer, not "new ToXMLStream()".

The "correct" Java program would look like this:

import java.io.ByteArrayOutputStream;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.xml.serializer.DOMSerializer;
import org.apache.xml.serializer.OutputPropertiesFactory;
import org.apache.xml.serializer.Serializer;
import org.apache.xml.serializer.SerializerFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Bug1985
{   
    public static void main(String[] args) throws Exception
    {
        // Create the document factory and make it namespace aware
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);        
        Document document = dbf.newDocumentBuilder().newDocument();
        
        // Note the createElementNS not createElement calls
        document.appendChild(document.createElementNS(null,"joske"));
        Element houseEl = document.createElementNS(null,"house");        
        houseEl.setAttributeNS("http://joske.mydomain/bla", "jo:ta", "blabla");
        document.getDocumentElement().appendChild(houseEl);
        
        // Using public APIs to create the serializer
        java.util.Properties xmlProps =
        OutputPropertiesFactory.getDefaultMethodProperties("xml");
        Serializer ser =
            SerializerFactory.getSerializer(xmlProps);

        // Serialize the DOM
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ser.setOutputStream(out);
        DOMSerializer serializer = ser.asDOMSerializer();
        serializer.serialize(document);
        System.out.println(out.toString());
    }
}

Unfortunately the output is still the same, the prefix jo is seen in the output, but there is no attribute declaring the prefix to URI mapping, that is there is no xmlns:jo="http://joske.mydomain/bla" attribute.

I've tracked this down to an issue with the document factory (Xerces) rather than Xalan. A newer Xerces will have the required fix. Thanks for you patch, but the fix is in Xerces.

I'll probably be moving this issue to the Xerces queue shortly.

Regards,
Brian Minchau

> Problem with namespaces in ToXMLStream
> --------------------------------------
>
>          Key: XALANJ-1985
>          URL: http://nagoya.apache.org/jira/browse/XALANJ-1985
>      Project: XalanJ2
>         Type: Bug
>   Components: Serialization
>     Reporter: Nick Van den Bleeken
>  Attachments: Test.java, Xalan_ToStream.patch
>
> There is a problem in ToXMLStream (org.apache.xml.serializer.ToXMLStream) if 
> there is an attribute of a namespace that is never used before, the 
> namespace is never defined in this case. As attachment you can find a small 
> test program that demonstrates the problem and a patch file that solves the 
> problem. I am not sure if it is the patch is the way that the problem needs 
> to be solved, but it seems to works. Can the patch be applied on the CVS tree? Or can 
> the problem be solved on the CVS in an other way? If you need more 
> information please feel free to ask.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://nagoya.apache.org/jira/secure/Administrators.jspa
-
If you want more information on JIRA, or have a bug to report see:
   http://www.atlassian.com/software/jira


---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-dev-help@xml.apache.org