Element Type - Declaration
I have an XML document, with one structure nested inside another - e.g "manager" element nested inside a "company":
code:
company>
manager>
name> john /name>
/manager>
/company>
I'm trying to create a schema (xsd) for this.
Note my "manager" needs to be defined outside the scope of "company", so that it's re-usable (i.e. manager may be nested inside other structures, not just "company").
Note my "manager" needs to be defined outside the scope of "company", so that it's re-usable (i.e. manager may be nested inside other structures, not just "company").
I get the impression there are 2 ways to do it:
1. Define "manager" as *element*, then let "company" declare a *ref* to it
code:
schema ...>
element name="company">
complexType>
sequence>
element ref="manager"/>
/sequence>
/complexType>
/element>
element name="manager">
complexType>
sequence>
element name="name" type="string"/>
/sequence>
/complexType>
/element>
/schema>
2. Define "manager" as *complex type*
code:
schema ...>
element name="company">
complexType>
sequence>
element name="manager" type="managerType"/>
/sequence>
/complexType>
/element>
complexType name="managerType">
sequence>
element name="name" type="string"/>
/sequence>
/complexType>
/schema>
Could anyone please tell what's the difference between the 2 approaches ? Why does XML define 2 syntaxes to achieve the same goal ?
Answer:
First XSD(Schema) is called Using a global Type.
Second XSD(Schema) is called Referring to a existing global element.
Using a global Type:
Often many of our elements will have the same content. Instead of declaring duplicate local types throughout our schema, we can create a global type. Within our element declarations, we can refer to a global type by name (with type attribute).
Referring to a existing global element:
Referring to global types allows us to easily reuse content model definitions within our XML Schema. Often, we may want to reuse a particular element delcaration, instead of the type. XML Schemas allow you reuse global element declarations within your content model.
Check this link for better understanding.
Answer:
First XSD(Schema) is called Using a global Type.
Second XSD(Schema) is called Referring to a existing global element.
Using a global Type:
Often many of our elements will have the same content. Instead of declaring duplicate local types throughout our schema, we can create a global type. Within our element declarations, we can refer to a global type by name (with type attribute).
Referring to a existing global element:
Referring to global types allows us to easily reuse content model definitions within our XML Schema. Often, we may want to reuse a particular element delcaration, instead of the type. XML Schemas allow you reuse global element declarations within your content model.
Check this link for better understanding.
0 Comments:
Post a Comment
<< Home