XML (Extensible Markup Language) allows you to define your own custom tags and structure data in a hierarchical format. There are several common types of elements and attributes used in XML:
Elements: Elements are the building blocks of an XML document. They consist of an opening tag, content, and a closing tag. Elements can be nested within other elements, forming a tree-like structure.
EX: xml
<title>XML Basics</title>
<author>John Doe</author>
</book>
Types
1)Simple
Type :
It contain only text
Ex- <xs:element
name=”age” type=”xs:integer”/>
a)UserDerived
->1.atomic
2.nonatomic(list type, union type)
b)Built-intype
->primitive
19 of which primitive
type:
string, boolean, decimal, float, double, datetime,
duration, time, date, gYearMonth, gYear,
gMonthDay, gDay, gMonth, hexbinary, base64binary, anyURL, QName, NOTATION
25 of which build-in
derived data types:
normalizedString, token, language, NMTOKENS, Name,
NCName, ID, IDREF, IDREFS, ENTITY, ENTITIES, integer, nonPositiveInteger,
negativeInteger,long,int,short,byte,nonNegativeInteger, unsignedLong,
unsignedInt, unsignedShort, unsignedByte, positiveInteger
2)Complex Type
The complex type supports adding attribute types to
simple types.
4 kinds of complex
elements:
Empty element
Element that contain other
elements
Element that contain only
text
Elements that contain both
other elements and text.
-> empty element
-> simple
content (Empty content means that a corresponding XML instance has no text
or embedded elements.)
-> complex
content - (sequence,choice,all)
Simple Element: only contain text type,(doesnt contain child-element and
attributes)
syntax:
<element name="element-name"
type="datatype"> </element>
Ex: <element name="empNo" type="int"/>
Complex Element:
-> Element with text-data and attribute.
-> Element with Empty content and attributes.
-> element with child elements and/or attributes.
-> element with mixed content and/or attributes
1)complex element with child element
syntax:
<element name="element-name">
<complex
type>
<sequence,all,choice>
<element
name="child1" type="dt"/>
<element
name="child2" type="dt"/>
</sequence,all,choice>
</complex
type>
</element>
Ex:
<schema>
<element
name="employee">
<complex type>
<sequence>
<element name="empNo"
type="int"/>
<element
name="name" type="string"/>
<element
name="salary" type="decimal"/>
</sequence>
</complex
type>
</element>
</schema>
2) element with (mixed content)- [mixed- allow child
element and text data too]
<schema>
<element
name="employee">
<complex type mixed="true">
<sequence>
<element name="empNo"
type="int"/>
<element
name="name" type="string"/>
<element
name="salary" type="decimal"/>
</sequence>
</complex
type>
</element>
</schema>
in xml:
<employee>
The EmpNo is <empNo>101<empNo> and Name is
<name> Diksha <name> and Salary is <salary>55000
</salary>
</employee>
3) element with text data and attribute
(simpleContent)
<schema>
<element
name="employee">
<complex type>
<simpleContent>
<extension base="string"> // Extension and restriction is same, boyh
define datatypes.
<attribute name="empNo"
type="int"/>
<attribute name="name" type="string"/>
</extension>
</simpleContent>
</complex
type>
</element>
</schema>
1)
element with empty
content and attributes
Empty content means that a corresponding XML instance has no text or embedded elements.
<schema>
<element
name="Cource">
<complex type>
<element name="courceId"
type="int"/>
<element
name="courceName" type="string"/>
<element
name="courceFee" type="decimal"/>
</sequence>
</complex
type>
</element>
</schema>
xml: <cource courceId=""
courceName="" CourceFee=""/>
UserDefined Datatypes:
Atomic
dataTypes
NonAtomic
datatypes
Atomic data type:
Scenario: Suppose 10 element are their, for 10 elements
providing restriction is increasing the burden so create one atomic with
restriction wherever restriction is required instead of providing restriction
direct use atomic type.
Ex 1:
<simpleType
name="NameType">
<restriction
base="string">
<pattern
value="[a-zA-Z]{5}"/>
</restriction>
</simpleType>
</element>
Ex 2:
<simpleType
name="NumberType">
<ristriction
base="decimal">
<minInclusive value="100"/>
<maxInclusive
value="99999">
</restriction>
</simpleType>
-> <element
name="salary" type="emp:NumberType">
NonAtomic datatypes:
a) listType
:collections of value
b) UnionType
: combining two datatypes
a) ListType: when we want to
collect multiple values into single unit of list
xml: <contactNumbers> 7829740932 95106864
9430434681 </contactNumbers>
Ex1:
<xs:simpleType name="contactList">
<xs:list item
type="xs:string">
</xs:simpleType>
Ex2:
<xs:simpleType name="DateList">
<xs:list item
type="xs:date">
</xs:simpleType>
Ex3:
<xs:schema
xmlns:xs="http://www.w3.org/2001/xmlSchema"
smlns:nit="http://empinfo.in/nit"
targetNamespace="http://empinfo.in/nit"
elementFromDefault="qualified">
<xs:simpleType name="DateList">
<xs:list item
type="xs:date">
</xs:simpleType>
<xs:simpleType name="contactList">
<xs:list item
type="xs:string">
</xs:simpleType>
<xs:element name="employee">
<xs:complex
type>
<xs:sequence>
<xs:element name="empNo"
type="xs:integer"/> //
builtin type
<xs:element name="name" type="xs:string"/> //
builtin type
<xs:element name="salary"
type="nit:salaryType"/>
//userdefined non atomic
<xs:element name="vacationDays"
type="nit:DateList"/> //
acting as non-atomic type
<xs:element name="phoneNumbers"
type="nit:contactList"> //// acting as non-atomic type
<xs:simpleType
name="salaryType"> // acting as atomic datatype
<xs:restriction
base="xs:decimal">
<xs:mininclusive
value="1000">
<xs:maxinclusive
value="90000">
</xs:restriction>
</xs:simpleType>
</sequence>
</complex
type>
</element>
b) unionType: used to combine two or more number of datatypes.(event)
<xs:simpleType name="Event">
<xs:Union
memberTypes="RunningRace Gymnastics"/>
</xs:simpleType>
Attribute:
-> Used in complex type element.
-> Duplicate attribute will not allow in an element.
-> Attribute orders is not important in an element.
-> Attribute values must be enclose with either single
quotes (or) double quotes.
Ex: <employee empNo="101" empName='diksha'> </employee>
Syntax:
<attribute name="attribute-name">
<type="attribute-type"/>
Ex: <attribute name="courceid" type="int"/>
<attribute
name="courceName" type="string"/>
In XSD:
<schema>
<element
name="cource">
<complex type>
<sequence>
<element name="courceId"
type="int"/>
<element
name="courceName" type="string"/>
<element name="courceFee"
type="decimal"/>
</sequence>
</complex
type>
</element>
</schema
In XML:
<cource courceId="101"
courceName="java" courceFee="20000"/>
Mandatory
attribute:
Syntax:
<attribute name="id" type="int"
use="required"/> // if 'use' is not their then it means this
field is optional by default.
Default
attribute:
Syntax:
<attribute name="courceName"
type="string" default="java"/>
Fixed
attribute:
Syntax:
<attribute name="courceFee"
type="decimal" fixed="20000"/>
RESTRICTION in XSD:
1) minInclusive
2) maxInclusive
3) minExclusive
4) maxExclusive
5) totalDigits
6) fractionDigits
7) pattern
8) whiteSpace
9) enumeration
10) length
11) maxLength
12) minLength
Ristriction Syntax:
<element name="element name">
<simpleType>
<restriction base="datatype">
use the above constraints
</restriction>
</simpleType>
</element>
Ex 1: 10000>=, 90000<=
<element name="salary">
<simpleType>
<restriction
base="decimal">
<minInclusive
value="10000">
<maxInclusive
value="90000">
</restriction>
</simpleType>
</element>
Ex 2: acceptable value is 3
characters only (A-Z (or) a-z)
<element name="lastName">
<simpleType>
<restriction
base="string">
<pattern
value="[A-Za-z][A-Za-z][A-Za-z]"/>
</restriction>
</simpleType>
</element>
Ex 3:
acceptable value is 5 digit : <56100>
<element name="Zipcode">
<simpleType>
<restriction
base="string">
<pattern
value="[0-9][0-9][0-9][0-9][0-9]"/> //or value="[0-9]{5}"
</restriction>
</simpleType>
</element>
Ex 4:
acceptable value is M or F
<element name="gender">
<simpleType>
<restriction
base="string">
<pattern
value="[MF]"/>
</restriction>
</simpleType>
</element>
Ex 5:
acceptable value is MALE or FEMALE
<element name="gender">
<simpleType>
<restriction
base="string">
<pattern
value="[MALE|FEMALE]"/>
</restriction>
</simpleType>
</element>
Ex 6:
<xs:simpleType name="string1000">
<xs:restriction base="xs:string">
<xs:maxLength value="1000"/>
</xs:restriction>
</xs:simpleType>
No comments:
Post a Comment