To specify a default value, simply include the default attribute with the desired value:
<attribute name="kind" type="contacts:KindType" default="Home"/>
Fixed values operate much like default values.
If the attribute is omitted, then the parser inserts the attribute with the fixed value.
<attribute name="version" type="string" fixed="1.0"/>
Attribute with fixed value
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.java2java.com" xmlns="http://www.java2java.com"
elementFormDefault="qualified">
<xsd:element name="source">
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="xsd:anyType">
<xsd:attribute name="sectionid" type="xsd:string"
use="fixed" value="101" />
<xsd:attribute name="newspaperid" type="xsd:string" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>
File: Data.xml
<?xml version="1.0"?>
<source xmlns="http://www.java2java.com"
sectionid="101"
newspaperid="21"/>
Attribute with default value
The fixed attribute only sets the content if the attribute actually appears in the XML.
If it is omitted, no content is set.
If the default attribute is set but the attribute is omitted from the XML, then the attribute's value is set to the default value.
File: Schema.xsd
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.java2java.com" xmlns="http://www.java2java.com"
elementFormDefault="qualified">
<xsd:element name="source">
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="xsd:anyType">
<xsd:attribute name="sectionid" type="xsd:string"
use="fixed" value="101" />
<xsd:attribute name="newspaperid" type="xsd:string"
use="default" value="21" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>
File: Data.xml
<?xml version="1.0"?>
<source sectionid="101" newspaperid="21"/>
|