Inserting XML fragment adds xmlns="" attribute

Having trouble installing Oxygen? Got a bug to report? Post it all here.
sijomon
Posts: 83
Joined: Wed May 20, 2009 1:18 pm

Inserting XML fragment adds xmlns="" attribute

Post by sijomon »

Hi (me again),

I am developing a framework operation. The operation is responsible for surrounding a piece of text selected in the author window with some XML retrieved from a web app. The XML coming back from the webapp looks like this:

<legis-cite title="Directive 80/68 on the Protection of Groundwater Against Pollution" type="eudirective" year="1979" div-num="art1" div-seq="001" div-depth="1" lcr:id="111428075">
<ancestor-part div-num="root" div-seq="001" div-depth="0" lcr:id="294967762"></ancestor-part>
</legis-cite>

When I add it to the document using:

authorAccess.surroundInFragment(citation, offset, end);

the XML that gets inserted looks like this:

<legis-cite title="Directive 80/68 on the Protection of Groundwater Against Pollution" type="eudirective" year="1979" div-num="art1" div-seq="001" div-depth="1" lcr:id="111428075" xmlns="">
<ancestor-part div-num="root" div-seq="001" div-depth="0" lcr:id="294967762"></ancestor-part>
</legis-cite>

NOTE the additional 'xmlns=""' attribute in the top level legis-cite element.

Has anyone any ideas why this attribute is being added and what I can do to prevent it? Please bear in mind I'm a Java nerd not an XML nerd ;)

Many thanks,

Simon.
Radu
Posts: 9048
Joined: Fri Jul 09, 2004 5:18 pm

Re: Inserting XML fragment adds xmlns="" attribute

Post by Radu »

Hello Simon,

When we made the author extensions we made the decision that XML fragments inserted with the specific insert actions needed to have the namespace fully specified on them.

So if your main XML file looks like:

Code: Select all


<root xmlns="http://rootns.com">
........................
</root>
and you want to add an XML fragment in the same namespace the fragment will have to be:

Code: Select all


<fragXML xmlns="http://rootns.com">
...........
</fragXML>
When the fragment will get inserted, the extra namespace declaration will get deleted.
If your fragment does not explicitly specify the namespace, it will be considered in no-namespace and the additional xmlns="" attribute will be added to it to enforce that.

In your particualar case, the XML fragment you are trying to insert should be something like:

<legis-cite xmlns="http://the/namespace/of/the/main/xml.file"
.................
</legis-cite>

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
sijomon
Posts: 83
Joined: Wed May 20, 2009 1:18 pm

Re: Inserting XML fragment adds xmlns="" attribute

Post by sijomon »

Thanks for the response.

Is there a way to programtically dertrmine the default namespace of the currently active document from an Opertaion class? I can't seem to find it on the AuthorAccess object, am I just missing it?

Simon.
Radu
Posts: 9048
Joined: Fri Jul 09, 2004 5:18 pm

Re: Inserting XML fragment adds xmlns="" attribute

Post by Radu »

Hi Simon,

The code should be something like:

Code: Select all


    AuthorNode contextNode = authAccess.getDocumentController().getNodeAtOffset(insertOffset);
if(contextNode.getType() == AuthorNode.NODE_TYPE_ELEMENT) {
String ns = ((AuthorElement)contextNode).getNamespace();
}
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
sijomon
Posts: 83
Joined: Wed May 20, 2009 1:18 pm

Re: Inserting XML fragment adds xmlns="" attribute

Post by sijomon »

Yes that works perfectly.

many thanks.

Simon.
Post Reply