I use various xml transactions for request and then response transactions. The response transaction echoes the request transaction and then provides any “hit” information. I’ve noticed that when I try to display the echo, if an element was optional and has the same value as the one provided in the default, then it is omitted.
<Request Name="Marty Jones" Education="0"/>
On Request, if ‘Name’ is required and ‘Education’ is optional, with an enumeration of (0 – none, 1 – high school, 2 – college) and the default is “0”. The element above will display as:
<Request Name="Marty Jones"/>
If I specify that Education=”2″, then it will display as:
<Request Name="Marty Jones" Education="2"/>
Is there a reason that the optional, defaulted value is omitted? I want to show the entire echo no matter what the conditions are.
Many XSD-aware tools will do this when writing XML. The idea is that it makes the XML shorter and more readable if values that equal the default are omitted (provided they are optional). For example, for XSD itself, any values of
minOccurs="1"ormaxOccurs="1"will be omitted when writing it out – imagine how cluttered your XSD would be with them included on every<element>,<sequence>,<choice>etc that they could be.Note that when parsing with an XSD, a defaulted value and an explicit value equal to the default are not distinguished – without this information, the tool cannot reconstruct which was originally used.
However, not everyone wants this behaviour in every application; so some tools have a configuration switch to turn this off. As this whole issue is not part of the XSD spec, any such configuration switch is tool-dependent. So you’ll have to tell us which tool you’re using.
BTW: Here’s a question on this issue, with links to other questions on specific tools. (e.g. MarkLogic has a switch):
Does the Xml Schema spec define the serialization of optional attributes with a default?