Articles → XSD → Introduction To XSD
Introduction To XSD
What Is XSD?
Back To Basics - Types Of XML Element
- Simple element – Simple elements are those XML elements which don’t have child elements or attributes.
<element_name>text</element_name>
- Complex element – Complex elements are those XML elements which contain child elements or attributes.
<element_name attribute1=”attribute_value”>
.. Child nodes or text
</element_name>
Validate XML Against XSD
- Open query analyzer
- Write following code
-- Create a XML schema collection
Create XML schema collection TestSchemaCollection As '
XSD ' -- XSD against which XML will be validated
GO
-- Create a table with a single column of data type XML
Create table TestTable
(
MyXMLSample XML (TestSchemaCollection)
)
-- Insert a XML into the table
-- Insertion will be success if XML is valid
-- else insert statement will throw an error.
Insert into TestTable values('XML')
-- Finally drop a table and XML schema collection
drop table TestTable
drop xml schema collection TestSchemaCollection
- Run the query.
Schema Element In XSD
<schema xmlns="http://www.w3.org/2001/XMLSchema">
..
</schema>
Simple XML Element In XSD
<element name="element_name" type="data_type" />
Complex XML Element In XSD
<element name="name_of_complex_element">
<complexType>
<group_indicator>
..
List of simple types of xml elements
</ group_indicator>
</complexType>
</element>
XML Attributes In XSD
<attribute name="" type=""/>
Data Types Supported By XSD
- string
- decimal
- integer
- boolean
- date
- time
Examples
<Blogs>
<blog category="Technical">
<title>Code Project</title>
<URL>CodeProject.com</URL>
</blog>
</Blogs>
<schema
xmlns="http://www.w3.org/2001/XMLSchema">
<element name="Blogs">
<complexType>
<sequence>
<element name="blog">
<complexType>
<sequence>
<element name="title" type="string" />
<element name="URL" type="string" />
</sequence>
<attribute name="category" type="string"/>
</complexType>
</element>
</sequence>
</complexType>
</element>
</schema>