You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Stéphane Rault <ks...@yahoo.fr> on 2005/02/21 15:59:55 UTC

RE : Serialisation with no default namespace prefix

Do you have a snippet of the code that constructs the elements? I believe
you just need to create the namespace attribute declaration correctly. In
your example you have 'xmlns:ced="namespace"', but what you want is
'xmlns="namespace"', but that could just be a typo.


Y<ou are right for the typo. I made a mistake and what I want is
xmlns="namespace". 
For the production of the code, I want something that is generic. I mean,
the doc I used for the example is an XmlBeans production but my code has to
work with every DOM Document in the same way.

Stéphane.


-----Original Message-----
From: Stéphane Rault [mailto:ksbrault@yahoo.fr] 
Sent: Monday, February 21, 2005 8:44 AM
To: xerces-j-user@xml.apache.org
Subject: Serialisation with no default namespace prefix

Hello all. 

I want to serialize some Xml Documents with namespace inside but with a
defaultNamespace and no prefix for this namespace. How can I do it ?

// Piece of code
            OutputFormat format = new OutputFormat("xml", "ISO-8859-1",
true);

            XMLSerializer serialiseur = new XMLSerializer(format);

            StringWriter sw = new StringWriter();
            serialiseur.setOutputCharStream(sw);
            serialiseur.asDOMSerializer();
            serialiseur.setNamespaces(true);

            serialiseur.serialize(anElement);
// End Piece of code

If I use it I have an output like that : 

// What I don't want
<?xml version="1.0" encoding="ISO-8859-1"?>
<ced:traceReceipt xmlns:ced="MyNamespace">
    <ced:taskName>MyContent</ced:taskName>
</ced:traceReceipt>
// End What I don't want

I want to obtain instead : 

// What I want
<?xml version="1.0" encoding="ISO-8859-1"?>
<traceReceipt xmlns:ced="MyNamespace">
    <taskName>MyContent</taskName>
</traceReceipt>
// End What I want

Thanks in advance




---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


RE : RE : Serialisation with no default namespace prefix

Posted by Stéphane Rault <ks...@yahoo.fr>.
<snip>

You might consider using a custom NoteFilter with a TreeWalker (both in 
org.w3c.dom.traversal) to achieve this.

--> thanks for the tip.. I will show it.

Converting namespace prefixes is something I've had trouble with in the 
past, and, frankly, I'm surprised that this sort of operation isn't 
supported directly by the DOM.

--> I wonder too.. It's quite ugly but I may miss something to understand
the problem...

Cheers,
James Shaw

--

Stéphane



---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


Re: RE : Serialisation with no default namespace prefix

Posted by James Shaw <he...@btopenworld.com>.
Stéphane Rault wrote:

>Thank you for your sharp answer... 
>
>I will implements the first method I think.
>
>Stéphane.
>
>-----Message d'origine-----
>De : Nathan Beyer [mailto:nbeyer@kc.rr.com] 
>Envoyé : mardi 22 février 2005 03:09
>À : xerces-j-user@xml.apache.org
>Objet : RE: Serialisation with no default namespace prefix
>
>
>If the document could be created in any way externally to this
>serialization, then you're basically going to have to add/modify the current
>XMLNS declarations to change the one you want to be the default namespace
>and then dig through all of the nodes in the document and set their prefixes
>to empty.
>
>  
>

You might consider using a custom NoteFilter with a TreeWalker (both in 
org.w3c.dom.traversal) to achieve this.

Converting namespace prefixes is something I've had trouble with in the 
past, and, frankly, I'm surprised that this sort of operation isn't 
supported directly by the DOM.

Cheers,
James Shaw

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


RE : Serialisation with no default namespace prefix

Posted by Stéphane Rault <ks...@yahoo.fr>.
Thank you for your sharp answer... 

I will implements the first method I think.

Stéphane.

-----Message d'origine-----
De : Nathan Beyer [mailto:nbeyer@kc.rr.com] 
Envoyé : mardi 22 février 2005 03:09
À : xerces-j-user@xml.apache.org
Objet : RE: Serialisation with no default namespace prefix


If the document could be created in any way externally to this
serialization, then you're basically going to have to add/modify the current
XMLNS declarations to change the one you want to be the default namespace
and then dig through all of the nodes in the document and set their prefixes
to empty.

This is a crude example:
		DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
		factory.setNamespaceAware(true);
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document doc = builder.newDocument();
		
		Element root =
doc.createElementNS("urn:temp:temp","tns:rootElement");
		doc.appendChild(root);
		Element child =
doc.createElementNS("urn:temp:temp","tns:childElement");
		root.appendChild(child);
		
		//serialize with prefixes
		TransformerFactory tf = TransformerFactory.newInstance();
		Transformer t = tf.newTransformer();
		t.setOutputProperty(OutputKeys.INDENT, "yes");
		t.setOutputProperty(OutputKeys.METHOD, "xml");
		t.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
		t.transform(new DOMSource(doc), new StreamResult
(System.err));
		
		//add the XMLNS declaration
		root.setAttributeNS("http://www.w3.org/2000/xmlns/",
"xmlns", "urn:temp:temp");
		//change the prefixes for all nodes
		root.setPrefix("");
		child.setPrefix("");
		
		t.transform(new DOMSource(doc), new StreamResult
(System.out));

To make this more robust you'd have to search through DOM looking for the
nodes with the namespace you're interested and change only those nodes.

Another way would be to create a simple copy XSLT stylesheet that changes
the prefixes for you. You could then just load this up as a stylesheet with
the transformer APIs and just apply it to any document.

These may not be the most efficient approaches, but they might work for you.

-Nathan

-----Original Message-----
From: Stéphane Rault [mailto:ksbrault@yahoo.fr] 
Sent: Monday, February 21, 2005 9:00 AM
To: xerces-j-user@xml.apache.org
Subject: RE : Serialisation with no default namespace prefix


Do you have a snippet of the code that constructs the elements? I believe
you just need to create the namespace attribute declaration correctly. In
your example you have 'xmlns:ced="namespace"', but what you want is
'xmlns="namespace"', but that could just be a typo.


Y<ou are right for the typo. I made a mistake and what I want is
xmlns="namespace". 
For the production of the code, I want something that is generic. I mean,
the doc I used for the example is an XmlBeans production but my code has to
work with every DOM Document in the same way.

Stéphane.


-----Original Message-----
From: Stéphane Rault [mailto:ksbrault@yahoo.fr] 
Sent: Monday, February 21, 2005 8:44 AM
To: xerces-j-user@xml.apache.org
Subject: Serialisation with no default namespace prefix

Hello all. 

I want to serialize some Xml Documents with namespace inside but with a
defaultNamespace and no prefix for this namespace. How can I do it ?

// Piece of code
            OutputFormat format = new OutputFormat("xml", "ISO-8859-1",
true);

            XMLSerializer serialiseur = new XMLSerializer(format);

            StringWriter sw = new StringWriter();
            serialiseur.setOutputCharStream(sw);
            serialiseur.asDOMSerializer();
            serialiseur.setNamespaces(true);

            serialiseur.serialize(anElement);
// End Piece of code

If I use it I have an output like that : 

// What I don't want
<?xml version="1.0" encoding="ISO-8859-1"?>
<ced:traceReceipt xmlns:ced="MyNamespace">
    <ced:taskName>MyContent</ced:taskName>
</ced:traceReceipt>
// End What I don't want

I want to obtain instead : 

// What I want
<?xml version="1.0" encoding="ISO-8859-1"?>
<traceReceipt xmlns:ced="MyNamespace">
    <taskName>MyContent</taskName>
</traceReceipt>
// End What I want

Thanks in advance




---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org




---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


RE: Serialisation with no default namespace prefix

Posted by Nathan Beyer <nb...@kc.rr.com>.
If the document could be created in any way externally to this
serialization, then you're basically going to have to add/modify the current
XMLNS declarations to change the one you want to be the default namespace
and then dig through all of the nodes in the document and set their prefixes
to empty.

This is a crude example:
		DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
		factory.setNamespaceAware(true);
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document doc = builder.newDocument();
		
		Element root =
doc.createElementNS("urn:temp:temp","tns:rootElement");
		doc.appendChild(root);
		Element child =
doc.createElementNS("urn:temp:temp","tns:childElement");
		root.appendChild(child);
		
		//serialize with prefixes
		TransformerFactory tf = TransformerFactory.newInstance();
		Transformer t = tf.newTransformer();
		t.setOutputProperty(OutputKeys.INDENT, "yes");
		t.setOutputProperty(OutputKeys.METHOD, "xml");
		t.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
		t.transform(new DOMSource(doc), new StreamResult
(System.err));
		
		//add the XMLNS declaration
		root.setAttributeNS("http://www.w3.org/2000/xmlns/",
"xmlns", "urn:temp:temp");
		//change the prefixes for all nodes
		root.setPrefix("");
		child.setPrefix("");
		
		t.transform(new DOMSource(doc), new StreamResult
(System.out));

To make this more robust you'd have to search through DOM looking for the
nodes with the namespace you're interested and change only those nodes.

Another way would be to create a simple copy XSLT stylesheet that changes
the prefixes for you. You could then just load this up as a stylesheet with
the transformer APIs and just apply it to any document.

These may not be the most efficient approaches, but they might work for you.

-Nathan

-----Original Message-----
From: Stéphane Rault [mailto:ksbrault@yahoo.fr] 
Sent: Monday, February 21, 2005 9:00 AM
To: xerces-j-user@xml.apache.org
Subject: RE : Serialisation with no default namespace prefix


Do you have a snippet of the code that constructs the elements? I believe
you just need to create the namespace attribute declaration correctly. In
your example you have 'xmlns:ced="namespace"', but what you want is
'xmlns="namespace"', but that could just be a typo.


Y<ou are right for the typo. I made a mistake and what I want is
xmlns="namespace". 
For the production of the code, I want something that is generic. I mean,
the doc I used for the example is an XmlBeans production but my code has to
work with every DOM Document in the same way.

Stéphane.


-----Original Message-----
From: Stéphane Rault [mailto:ksbrault@yahoo.fr] 
Sent: Monday, February 21, 2005 8:44 AM
To: xerces-j-user@xml.apache.org
Subject: Serialisation with no default namespace prefix

Hello all. 

I want to serialize some Xml Documents with namespace inside but with a
defaultNamespace and no prefix for this namespace. How can I do it ?

// Piece of code
            OutputFormat format = new OutputFormat("xml", "ISO-8859-1",
true);

            XMLSerializer serialiseur = new XMLSerializer(format);

            StringWriter sw = new StringWriter();
            serialiseur.setOutputCharStream(sw);
            serialiseur.asDOMSerializer();
            serialiseur.setNamespaces(true);

            serialiseur.serialize(anElement);
// End Piece of code

If I use it I have an output like that : 

// What I don't want
<?xml version="1.0" encoding="ISO-8859-1"?>
<ced:traceReceipt xmlns:ced="MyNamespace">
    <ced:taskName>MyContent</ced:taskName>
</ced:traceReceipt>
// End What I don't want

I want to obtain instead : 

// What I want
<?xml version="1.0" encoding="ISO-8859-1"?>
<traceReceipt xmlns:ced="MyNamespace">
    <taskName>MyContent</taskName>
</traceReceipt>
// End What I want

Thanks in advance




---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org




---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org