Home Tutorial Xml Global and Local Simple-Type Elements

 
 

Global and Local Simple-Type Elements
Posted on: November 22, 2010 at 12:00 AM
When an element is declared globally then it can be referenced by any other element declaration.
Global and Local Simple-Type Elements

When an element is declared globally then it can be referenced by any other element declaration. Declare the element as a child of the xs:schema element then it is considered as global and now it can be reused many time by other elements.

For example: line1 and line2 elements are global and can be referenced by Address element.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
       <xs:element name="line1" type="xs:string"/>
       <xs:element name="line2" type="xs:string"/>
       <xs:element name="Address">
            <xs:complexType>
                 <xs:sequence>
                      <xs:element ref="line1"/>
                      <xs:element ref="line2"/>
                 </xs:sequence>
            </xs:complexType>
       </xs:element>
</xs:schema>

You can also define the type of an element locally. The local type is unnamed and that type is available to that element only.

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
     <xs:element name="myelm1">
          <xs:complexType>
               <xs:sequence>
                   <xs:element name="myelm2">
                        <
xs:simpleType>
                           <
xs:restriction base="xs:string">
                               <
xs:length value="10"/>
                           <
/xs:restriction>
                        <
/xs:simpleType>
                   <
/xs:element>
               </xs:sequence>
          </xs:complexType>
     </xs:element>
</xs:schema>


Related Tags for Global and Local Simple-Type Elements:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.