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ß
No comments:
Post a Comment