0

I am trying to add a default namespace to a root element that is in a different namespace but XOM doesn't like this!

e.g

<env:contentEnvelope 
xmlns:env="http://data.com/Envelope/2008-05-01/" 
xmlns:inv="http://Patents.data.com/2012-01-01/" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

I want the inv namespace to also be the default namespace by adding e.g.

xmlns="http://Patents.data.com/2012-01-01/"

but XOM won't have it!

Any ideas on how to do this?

madDerDog
  • 23
  • 5

1 Answers1

0

It was a combination of things that stopped me from adding a default namespace. The solution as with most things in IT is straight forward but not easy getting there:

The key here is using the .addNamespaceDeclaration(“”, namespace) with the first param set to “” this is not intuitive I was using .setNamespace() which doesn’t work.

Also I didn’t realise that creating an element using:

new Element("env:CEnvelope", IConstants.ENV_NAMESPACE);

automatically creates a namespace xmlns:env in the root element; I was explicitly doing this again; this doesn’t cause an issue but if you use .addNamespaceDeclaration(“”, namespace) after this it doesn’t work.

The line with ** is all you need:

private void setRoot(){

  Element root = new Element("env:CEnvelope", IConstants.ENV_NAMESPACE);    

  //Add root Element to the Document
  this.doc = new Document(root);

  root.addNamespaceDeclaration("xsi", IConstants.XSI_NAMESPACE);
  **root.addNamespaceDeclaration("", IConstants.PAT_NAMESPACE);
  //root.setNamespaceURI(IConstants.PAT_NAMESPACE);
  //root.addNamespaceDeclaration("env", IConstants.ENV_NAMESPACE);

Oh yes, I didn't need the inv namespace as this is the same as the default namespace!

Cheers :-)

madDerDog
  • 23
  • 5