Formalize how to express null value in xml #3959
Labels
media and encoding
Issues regarding media type support and how to encode data (outside of query/path params)
xml
Milestone
Since XML has no concept of
null
, how can we handle validating a null value (both as an attribute and as an element) ?consider the following 3.0 schema :
notice how
required
here prevents us from removing the attribute/elementI have thought about this, and here are some of the approaches I came up with:
For elements
Approach 1: self closing tags
Pros: Makes sense to whoever reads it
Cons: Nothing i can think of, but maybe some xml parsers can consider a self-closing tag equivalent to empty string and don't distinguish between them, which means they don't survive round tripping:
e.g.
<name />
gets represented on the way back to:<name></name>
Approach 2: empty string
Cons: if the property is of type string, it's not possible to distinguish between a non-null empty string and a null.
Workaround: force strings to be wrapped around double quotes, e.g.
Ofc this workaround is very problematic and not good since most parsers consider
""
A valid 2 character string.Approach 3: special marker attribute
Pros: Can represent nulls consistently without having to check the contents of the element, this is also how xml schema does it.
Cons: Size overhead of having to use
xsi:nil="true"
everywhere null is used.For attributes
Approach 1: empty string
Approach 2: disallow nullable attributes altogether
make it that
xml.attribute: true
andnullable: true
are mutually exclusiveThe text was updated successfully, but these errors were encountered: