Default and fixed attribute values

The default values for default and fixed attributes and elements come from the statically typed property getters. These default values are not stored in the attribute map or content list for an XML type. Gosu adds default or fixed values to attributes and elements in the XML output stream when it serializes its representation of an XML document.

Example

The following example XSD defines an XML element named person. The element definition includes an attribute definition named os with a default value of “Windows” and an attribute definition named location with a fixed value of “San Mateo”.

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="root">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="person" minOccurs="0" maxOccurs="unbounded">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="name" type="xsd:string"/>
            </xsd:sequence>
            <xsd:attribute name="os" type="ostype" default="Windows"/>
            <xsd:attribute name="location" type="xsd:string" fixed="San Mateo"/>
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

   <xsd:simpleType name="ostype">
    <xsd:restriction base="xsd:string">
      <xsd:enumeration value="Windows"/>
      <xsd:enumeration value="MacOSX"/>
      <xsd:enumeration value="Linux"/>
    </xsd:restriction>
  </xsd:simpleType>
</xsd:schema>

The following sample Gosu code creates a new Gosu representation of an XML document based on the preceding XSD. The code adds two person elements, one for jsmith and one for aanderson. For jsmith, the code adds an os attribute having the value Linux. The code does not add an os attribute to aapplegate, nor does the code add the location attribute to either person. Instead, the code relies on the default and fixed values defined in the XSD.

uses examples.pl.gosu.xml.myschema.Root

var xml = new Root()
xml.Person[0].Name = "jsmith"
xml.Person[0].Os = Linux
xml.Person[1].Name = "aapplegate"
// Gosu adds default and fixed values to the XML document at the time Gosu serializes XML for print.
for (person in xml.Person) {
print("${person.Name} (${person.Location}) -> ${person.Os}")
}
xml.print()

When the preceding Gosu code serializes its representation of an XML document, Gosu adds the fixed and default values to the XML output stream. The printed output shows that the Gosu representation of the XML document does not contain the values "San Mateo" or "Windows".

jsmith (San Mateo) -> Linux
aanderson (San Mateo) -> Windows 
<?xml version="1.0"?>
<root>
  <person os="Linux">
    <name>jsmith</name>
  </person>
  <person>
    <name>aanderson</name>
  </person>
</root>