0

If an attribute that is defined as an enumerated list is missing from an XML element should the first value in the list be used as a default if it doesn't have a default specified?

I have the following in a schema:

<xsd:simpleType name="YesNoType">
    <xsd:restriction base="xsd:string">
        <xsd:enumeration value="yes"/>
        <xsd:enumeration value="no"/>
    </xsd:restriction>
</xsd:simpleType>

<xsd:complexType name="TelephoneStructure">
    <xsd:sequence>
        <xsd:element name="TelNationalNumber" type="core:TelephoneNumberType"/>
        <xsd:element name="TelExtensionNumber" type="core:TelephoneExtensionType" minOccurs="0"/>
        <xsd:element name="TelCountryCode" type="core:TelCountryCodeType" minOccurs="0"/>
    </xsd:sequence>
    <xsd:attribute name="TelUse" type="core:WorkHomeType"/>
    <xsd:attribute name="TelMobile" type="core:YesNoType"/>
    <xsd:attribute name="TelPreferred" type="core:YesNoType"/>
</xsd:complexType>

I have generated C# types from the above schema. I expected that missing attributes would result in null values, but I find that missing attributes default to the first value in the list, is this correct according to the XML spec?

Jamie Kitson
  • 3,973
  • 4
  • 37
  • 50
  • Given then default values. See : https://www.w3schools.com/xml/schema_simple.asp – jdweng Mar 12 '21 at 18:09
  • Mappings from XML to C# aren't governed by any W3C (or other) standards as far as I'm aware, this is entirely a matter for the product designers, – Michael Kay Mar 12 '21 at 18:16
  • @MichaelKay Surely the XML spec says how XML should be interpreted though? – Jamie Kitson Mar 15 '21 at 10:43
  • 1
    It says that default values should be reported to the application, but it says nothing about what the application (here, your C# binding library) should do with them. – Michael Kay Mar 15 '21 at 12:50

1 Answers1

1

Only TelExtensionNumber and TelCountry code are optional according to your schema. Everthing else is not optional, and so will assume the default value (0 for int, null for classes).

How to make an element in XML schema optional?

Neil
  • 11,059
  • 3
  • 31
  • 56
  • When I validate the XML the validator doesn't flag up the missing attributes, does _optional_ just allow null values? – Jamie Kitson Mar 12 '21 at 17:38
  • When you validate the XML, are you validating against the schema or your code? Can you post the TelephoneStructure class, the YesNo enum and the XML you are trying to parse? – Neil Mar 12 '21 at 17:42
  • I'm validating XML without the attributes against the schema using the .Net validator. – Jamie Kitson Mar 12 '21 at 21:25
  • [Attributes are by default optional](https://www.w3schools.com/xml/schema_simple_attributes.asp), so I'm not sure what your point is. I think you might have missed the fact that I'm talking about attributes not elements. – Jamie Kitson Mar 15 '21 at 09:22