pdvrieze / xmlutil

XML Serialization library for Kotlin
https://pdvrieze.github.io/xmlutil/
Apache License 2.0
363 stars 30 forks source link

Default namespace on root element and unqualified child elements #172

Closed wbervoets closed 10 months ago

wbervoets commented 11 months ago

Hi,

I'm having a hard time to understand why the following happens:

Input XML

<CardRequest xmlns="http://www.my-namespace.com" requestId="00001254">
              <Child language="en">
                  <Data>TEST123</Data>
              </Child>
</CardRequest>

Kotlin

@Serializable
@XmlSerialName("CardRequest", "http://www.my-namespace.com")
class CardRequest(
    @XmlSerialName("Child")
    @XmlElement
    val child: Child

    @XmlSerialName("requestId")
    val requestId: String
}
@Serializable
@XmlSerialName("Child")
class Child(
    @XmlSerialName("Data")
    @XmlElement
    val requestId: String
}

So when I do XML().decodeFromString first and then call encodeToString again the written output will use the default namespace for the root element but Child will have an added xmlns:n1="http://www.my-namespace.com" + n1 prefixes on attributes and elements.

<CardRequest xmlns="http://www.my-namespace.com" requestId="00001254">
              <Child xmlns:n1="http://www.my-namespace.com" n1:language="en">
                  <n1:Data>TEST123</n1:Data>
              </Child>
</CardRequest>

I would expect that xmlns:n1 is not used and not necessary.

I've tried some combinations with ignoreNamespaces(), adding empty prefix and /or namespaces:

For example adding @XmlSerialName("Child", "http://www.my-namespace.com", "") @XmlSerialName("CardRequest", "http://www.my-namespace.com", "")

but this does not change anything.

When using @XmlSerialName("Child", "", "") I get an exception Namespace http://www.my-namespace.com does not match expected ""

pdvrieze commented 11 months ago

The reason for the additional namespace/prefix declaration is that the default namespace binding does not apply to attributes. Attributes without prefix always map to the null namespace. You probably want the language attribute to be unqualified (or in the xml prefix/namespace if that is appropriate).

pdvrieze commented 11 months ago

I have just pushed a version that may help a bit with this case. When you use default namespace attributes (the for now snapshot release) should now let the attribute be unqualified (no-namespace attributes). Your classes are incomplete (and incorrect - in terms of capitalisation).

wbervoets commented 10 months ago

Hi, I probably made a mistake when creating a simpler example. The snapshot version now lets the attribute unqualified. thanks! Any idea when you're planning a new release?

pdvrieze commented 10 months ago

I've gathered quite a number of bug fixes myself (trying to implement xmlschema parsing - and validating that the schema is valid - gets lots and lots of edge cases exposed). I will look at making a bug fix release with those (and the other ones) soon.

pdvrieze commented 10 months ago

Should now be released.