Description
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 :
person:
type: object
required:
- name
- attrName
properties:
name:
type: string
nullable: true
attrName:
type: string
nullable: true
xml:
attribute: true
notice how required
here prevents us from removing the attribute/element
I have thought about this, and here are some of the approaches I came up with:
For elements
Approach 1: self closing tags
<person>
<name />
</person>
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
<person>
<name></name>
</person>
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.
- this is null:
<name></name>
- this is empty string:
<name>""</name>
- this is valid string:
<name>"hello"</name>
- this is non valid string
<name>hello</name>
Ofc this workaround is very problematic and not good since most parsers consider ""
A valid 2 character string.
Approach 3: special marker attribute
<name xsi:nil="true"></name>
<name xsi:nil="true"/>
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
<person attrName="" />
Approach 2: disallow nullable attributes altogether
make it that xml.attribute: true
and nullable: true
are mutually exclusive