
Using a sad old cliché I hereby declare the train wreck that is xsi:type should be considered harmful:
- First of all it assumes the receiver has a W3C XML Schema. Wrong! Schemas are just one of a number of different ways I might have used to describe the XML. You might be using a description I gave you in RelaxNG, on the back of an envelope, whatever. You might have constructed your own model. I don’t care so long as you send me the right elements, attributes and content. You know, the right “stuff”.
- Sticking the Schema abstract type into a message is at best verbose, worst expects me to understand how you’re thinking about the data. I don’t care. Really. Just send me the “stuff”.
- Whilst Schema is wishy-washy about things such as where a schema is located, here we don’t have a hint. No, it’s an assertion. You will obey
xsi:type. - It encourages people to want to switch “types” on the fly. Yikes, that’s a manifesto to Monkey Patch (via Sam)
- It’s intrinsically insecure, consider eviltude such as
<getTime xsi:type="FireNuclearMissiles">. - Dynamic languages don’t need it, OK, maybe it’s useful when a repeated list has only one item, but otherwise Duck Typing is what I like and if I want to process my
accountNoas anintegeror astring, I’d rather keep that choice under my Kimono. thank you very much! - It doesn’t work with many existing static language tools, for example a number of JAXB2 implementations take an
xs:anyURI, generate a Java string, but then bouncexsi:type="xs:anyURI". Yes really. I may cry. - It doesn’t help the versioning story. Sorry. It just doesn’t.
So don’t touch it. It’s eviil!
Technorati Tags: databinding, Schema, w3c, XML
Schema 1.1 has introduced the “fallback to declard type” versioning mechanism. This at least helps the versioning story. I’ve described it in the guide to versioning at
http://www.w3.org/TR/2006/WD-xmlschema-guide2versioning-20060928/#d0e368
[...] xsi:type is Evil (tags: xsd xsi xml_schema by:paul_downey) [...]
[...] xsi:type is Evil (tags: by:paul_downey xsd xml) [...]
I agree. Point 1 is the most important for me.
I don’t understand when you write “OK, maybe it’s useful when a repeated list has only one item”, could you point me to an example ?
Hi Michele,
You need metadata when building an object on the fly from XML to tell you a single item may be repeated in a different message sent in the future.
For an example see the Perl XML::Simple library ForceArray option:
http://search.cpan.org/dist/XML-Simple/lib/XML/Simple.pm#ForceArray_=%3E_1_#_in_-_important
the only problem is that is the best (most pluggable) extension mechanism provided by xsd. If you use xsd and want to use something like oop inheritance you will surely hit xsi:type very soon. Unluckily xsd seems a widely used standard.
“Doctor Doctor it hurts when I use oop inheritance and XML …”