Namespaces in SpinXmlElements

Hello Community,

I´m working with Spin and I have a problem with the namespaces.

Let´s say I have the following XML:

<root xmlns="test">
    <sub></sub>
</root>

If I read this into a SpinXmlElement using

SpinXmlElement root = Spin.XML("<root xmlns=\"test\"><sub></sub></root>");

<sub> is automatically in the same namespace as <root> which is “test”.

Now let´s add a new Element called “sub2”:

SpinXmlElement sub2 = Spin.XML("<sub2></sub2>");
root.append(sub2);

After a xpath-test you can see that <sub2> is in no namespace.

try {
    root.xPath("./ns:sub2").ns("ns", "test").element();
} catch (final SpinXPathException ex) {
    System.out.println("This will throw an error, because <sub2> has no namespace.");
}

Important:
If you print root via .toString() you get the following output:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="test">
   <sub/>
   <sub2 xmlns=""/>
</root>

You can see, that <sub2> has an empty namespace here.

Now let´s add a new Element called “sub3”. This time with the same namespace.

SpinXmlElement sub3 = Spin.XML("<sub3 xmlns=\"test\"></sub3>");
root.append(sub3);

If you do a xpath-test, you will see that <sub3> now is in the same namespace as <root> (“test”).

try {
    root.xPath("./sub3").element();
} catch (final SpinXPathException ex) {
    System.out.println("This will throw an error, because <sub3> has a namespace.");
}

Important:
But if you print it again, you´ll get the following output:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="test">
   <sub/>
   <sub2 xmlns=""/>
   <sub3 xmlns=""/>
</root>

So, <sub2> and <sub3> have an empty namespace when you print them, but in reallity <sub3> has a namespace and <sub2> has not.

In the end my questions are:

  1. Is this behaviour a bug or am i making mistakes? Maybe it is the correct behaviour and I´m misunderstanding things.
  2. Is it possible to ignore the namespaces while working with Spin?
  3. Is there a way to add an element to an existing namespace without adding the xmlns attribute in the printed form?
    e.g.
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="test">
<!-- All <sub> elements in the namespace "test" -->
   <sub/>
   <sub2/>
   <sub3/>
</root>

Thank you for your help in advance.

Greetings from Germany,
Stefan Geiß

Hi @stefan_geiss,

Sorry for the delayed response. Regarding your questions 1 and 3, the empty namespaces being generated in both cases (sub2 and sub3) sounds like a bug to me. I’m not entirely sure, though. To help us with that, could you please post up the code in a test case on github?

Regarding question 2, I think you can make a namespace-independent xPath query (confer https://stackoverflow.com/questions/4440451/how-to-ignore-namespaces-with-xpath). With the SpinXmlElement API it appears you can’t do that (see for example the javadoc for SpinXmlElement#attr(String) that explicitly states it goes by the empty namespace).

Cheers,
Thorben

1 Like

Hi Torben,

Thank you for your response.
I created a repository at… https://github.com/GoatyGuy/Spin_Test_Case
I only uploaded the .java-file I wrote the test in. If you need anything else, feel free to ask. :slight_smile:

Yours,
Stefan

Hi Stefan,

First of all, adding sub3 actually prints this on my machine:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="test">
   <sub/>
   <sub2 xmlns=""/>
   <sub3 xmlns="test"/>
</root>

which I consider the expected output.

Furthermore, if you have this document:

<root xmlns="test">
    <sub></sub>
</root>

and add <sub2></sub2>, then sub2 is not part of the test namespace. Adding it as a child of root without a namespace would therefore change its semantics and therefore Spin (and more precisely, JAXB, which is used by Spin= adds the empty namespace attribute. So I would consider this expected behavior as well.

Lastly, can you append elements such that the resulting document becomes the following?

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="test">
<!-- All <sub> elements in the namespace "test" -->
   <sub/>
   <sub2/>
   <sub3/>
</root>

I’m afraid you can’t with Spin. As an alternative, you could try using camunda-xml-model or another XML processing library.

Cheers,
Thorben

1 Like

Thank you for your help, Thorben. :slight_smile:
I will look into it again in the near future.

Yours,
Stefan