Attribute Use: Default and Fixed Values : Attribute « XML Schema « XML Tutorial

XML Tutorial
1. Introduction
2. Namespace
3. XML Schema
4. XPath
5. XSLT stylesheet
Java
XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
XML Tutorial » XML Schema » Attribute 
3. 84. 9. Attribute Use: Default and Fixed Values
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"/>
3. 84. Attribute
3. 84. 1. Declaring Attributes
3. 84. 2. Attribute declarations have the following format
3. 84. 3. Creating a Local/Global Type for Attribute
3. 84. 4. how we declare attributes
3. 84. 5. Value Constraints on Attributes
3. 84. 6. Value of an attribute is the same as the value we prescribe in the schema, whether or not the attribute is present
3. 84. 7. Attribute declarations may also be defined locally within the complexType element
3. 84. 8. Requiring an Attribute
3. 84. 9. Attribute Use: Default and Fixed Values
3. 84. 10. Prohibited attributes in the restriction
3. 84. 11. Predefining an Attribute's Content with a fixed value or a default value
3. 84. 12. Restrict the data type by restricting its attributes
3. 84. 13. Mark attribute as required
3. 84. 14. A globally defined attribute declaration
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.