Web Development Tutorials




  
  

XML Simple Elements

Simple Elements

<xsd:element name="element name" type="element data type"/>

Listing 3-5. xsd namespace.

The simple element is prefixed with the schema namespace xsd, followed by the keyword element, and then a name attribute. The name attribute provides the name of the element Or XML tag name. Finally, the type attribute indicates the data type of the element. The most common data types include: xsd:string, xsd:decimal, xsd:integer, xsd:boolean, xsd:date, and xsd:time.

The following example illustrates how simple elements are defined.

Trees.xml

<?xml version="1.0"?>
<TreeInventory xsi:noNamespaceSchemaLocation="Trees.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <Tree tid="1">
        <ItemNumber>1047</ItemNumber>
        <TreeName>
            <CommonName>Colorado Blue Spruce</CommonName>
            <ScientificName>Picea pungens</ScientificName>
        </TreeName>
        <Description>A magnificent sight of silver blue-green spruce.</Description>
        <Price>1.99</Price>
        <Quantity>50</Quantity>
        <Type>Evergreen</Type>
        <Picture>1047.jpg</Picture>
    </Tree>

</TreeInventory>

Listing 3-6. Defining simple elements.

Below are some of the simple elements defined in the Trees.xml file.

<xsd:element name="Description" type="xs:string"/>
<xsd:element name="Price" type="xs:decimal"/>
<xsd:element name="Quantity" type="xs:integer"/> 

Listing 3-7. Trees.xml simple elements.

Simple elements can also be assigned a default value using the default attribute or a fixed value using the fixed attribute. A default value is automatically assigned to the element when no other value is specified. A fixed value is also automatically assigned to the element, and you cannot specify another value.

<xsd:element name="Description" type="xs:string" default="No Description Available"/>
<xsd:element name="Quantity" type="xs:integer" fixed="25"/>

Listing 3-8. Assigning default values.

In the example, the Description element is assigned the default text "No Description Available and the Quantity element is given a fixed value of 25.


TOP | NEXT: Simple Attributes