Switch to full style
XML Schema tutorial
Post a reply

lesson1 : <schema> Element

Tue Feb 17, 2009 11:51 pm

<schema> Element :
All XML schema have a common element which is <schema> .<schema> element is the root of your XSD schema.

It looks like this :
xml code
<?xml version="1.1"   encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.codemiles.com" xmlns="http://www.codemiles.com" >
<!-- other elements here -->
</xs:schema>

This is an XSD code that defines the structure and the data types of an XML document. The first line <?xml version="1.1" encoding="UTF-8"?> is the XML declaration, it specifies the version of XML used in the document and the character encoding of the document. The second line <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" is the root element of the schema, it defines the namespace of the schema; in this case, it's "http://www.w3.org/2001/XMLSchema", it's the standard namespace of the XML schema. The next line targetNamespace="http://www.codemiles.com" defines the target namespace of the schema; this is the namespace that will be used in the corresponding XML document. The last line xmlns="http://www.codemiles.com" is a default namespace that will be used in the corresponding XML document. This XSD code only defines the schema's basic structure, and no elements or types are defined in it. For the XSD to be useful, it must contain elements and types that describe the structure of the XML document it will use.


Let us understand what the meaning of the urls and namespaces in the xml schema is:

1.
Code:
xmlns:xs="http://www.w3.org/2001/XMLSchema"

This means that the datatypes and elements used in this schema come from this namespace, and it should be prefixed with "xs".

2.
Code:
targetNamespace="http://www.codemiles.com"

The new elements defined in this schema come from this namespace URL.

3.
Code:
xmlns="http://www.codemiles.com"

Default namespace, used if you didn't find the first one.


Once you create your XML schema, you can generate an XML file and put a link to its XML schema as a reference in validation. For example :
xml code
<?xml version="1.0"?>

<school xmlns="http://www.codemiles.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.codemiles.com school.xsd">

<Name>School number 1</Name>
<Location>Near train station</Location>
<Size>1000m'2 </Size>

</school>


As you may have noticed :
Code:
xsi:schemaLocation="http://www.codemiles.com school.xsd"


Used as a reference to your XML schema.

This is an XML document that describes a school. The first line <?xml version="1.0"?> is the XML declaration, it specifies the version of XML used in the document. The next line <school xmlns="http://www.codemiles.com" is the root element of the XML document, it defines the namespace of the document, and in this case, it's "http://www.codemiles.com". The next line xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" defines the namespace used for the XML Schema instance. This is a standard namespace used to indicate that the document is associated with an XSD schema. The next line xsi:schemaLocation="http://www.codemiles.com school.xsd" is an attribute that associates the XML document with an XSD schema, it specifies the location of the XSD schema that is used to validate the document. The next lines <Name>School number 1</Name>, <Location>Near train station</Location>, <Size>1000m'2 </Size> are the elements of the document. These elements contain information about the school, such as its name, location, and size.



Post a reply
  Related Posts  to : lesson1 : <schema> Element
 define UUID in your XSD schema     -  
 Usage of big element tag.     -  
 Pop the element off the end of array     -  
 Array element navigation     -  
 access element in array C++     -  
 print element in 2d matrix     -  
 lesson2 : XSD simple element     -  
 UnSet array element     -  
 opacity of HTML element- transparent     -  
 Get the position of HTML element using JQuery     -  

Topic Tags

XSD Basics