XML
Xml NameSpace:
- The name space concept of xml is equal to package concept in java.
- In XML, element names are defined by the developer. This often results in a conflict when trying to mix XML documents from different XML applications.So we use name spaces to uniquely identify them.
- For every xsd file we can define 1 targetNamespace
- It is an attribute which is used inside the schema element
- The namespace can be any nale start with any proocol and some text and end with some domain name.
.........
</schema>
- If we define name space in xsd document,then in corresponding xml file we need to prefix same namespace with all elements
- All the elements in the xsd belongs to "http://anyname.com" name space, so we need to prefix same namespace in all the ements of xml file in which we want to use this xsd.
<http://anyname.com:name>ramu<http://anyname.com:/name>
<http://anyname.com:salary>100000 <http://anyname.com:/salary>
<http://anyname.com:id>101<http://anyname.com:/id>
<http://anyname.com:/employee>
Above one looks weired,so we use, theare are 2 solutions o write the same in nice way
Solution 1:(Using xmlns in the root tag)
Eg:
<employee xmlns="http://anyname.com">
<name>ramu</name>
<salary>100000 </salary>
<id>101</id>
</employee>
Solution 2(Using prefix for every element)
<e:employee xmlns:e="http://anyname.com">
<e:name>ramu</e:name>
<e:salary>100000 </e:salary>
<e:id>101</e:id>
</e:employee>
- The xsd datatypes (complexType,int,String etc) and all elements are present in a predefined name space "http://www.w3.org/2001/XMLSchema".
- So each and every xsd file need to use above namespace either by defining in root tag using xmlns(Solution 1) or using some prefix of namespace(Solution 2).
Mapping XML doc with XSDL:
Emloyee.xsd:
<schema targetNamespace="http://anyname.com" xmlns:"http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<element name="employees">
<complexType>
<sequence>
<element name="employee" minOccurs="0" maxOccurs="10">
<complexType>
<sequence>
<element name="empid" type="int" />
<element name="courseName" type="string" />
<element name="salary" type="decimal" />
</sequence>
</complexType>
</element>
</sequence>
</complexType>
</element>
<element name="employees">
<complexType>
<sequence>
<element name="employee" minOccurs="0" maxOccurs="10">
<complexType>
<sequence>
<element name="empid" type="int" />
<element name="courseName" type="string" />
<element name="salary" type="decimal" />
</sequence>
</complexType>
</element>
</sequence>
</complexType>
</element>
</schema>
Employee.xml:
<employees xmlns="http://anyname.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://anyname.com C:\Users\bmaddi\Desktop\MyCollection\xml\EmployeeXSD.xsd">
<employee>
<empid> 101</empid>
<courseName> bhuvan</courseName>
<salary> 100</salary>
</employee>
<employee>
<empid> 101</empid>
<courseName> bhuvan</courseName>
<salary> 100</salary>
</employee>
</employees>
- We use schemaLocation attribute in the xml elemenet to map xsd to xml file
- syntax:schemaLocation="namespacedefinedinxsd1 locationofxsd1 namespacedefinedinxsd2 locationofxsd2 "
- we can define any number of name spaces for xml file it is like import in java.
- The namespace for schemaLocation attribute is "http://www.w3.org/2001/XMLSchema-instance", defined by w3 standard.
- So we need to use above namespace url fullyqualified name or prefix of namespace before it
- If we have xmlns wihout prefix more than one time, then it will oveeride.So always add prefix o 2nd one.(See in XSD root tag for example)
- All the elements in xml file should be in the same namespace provided in xsd file.(Use solution1 or solution 2 to maintain this)
XSD's:
Datatypes:
DataTypes
i)Simple Types
i)Builtin Types(44)
i)primitive
ii)derived
ii)User Derived Types
i)Atomic types
ii)Non Atomic types
ii)Complex Types
i)Empty
ii)Simple content
iii)ComplexContent
i)sequence
ii)choice
iii)all
Elements:
- Elemets are of 2 types i.e simple and complex types
- If a element has only text value and no attributes and no childs, then it is called simple
Complex Element:
- If an element has attribute or child elements or mixed etc atr called complex elements
- They are 4 types of complex elements
- Elements with Text-Data and attributes
- Elements with Empty Content and attributes
- Elements with Child Elements and/or Attributes
- Elements with Mixed Elements (Child elements+text data) and/or Attributes
Elements with Child Elements and/or Attributes:
<employee >
<empid> 101</empid><courseName> bhuvan</courseName>
<salary> 100</salary>
</employee>
- Since it is a complex data type we use complexType element
- For complex types we have 3datatypes(see in above table)
<element name="employee">
<complexType>
<sequence><element name="empid" type="int" />
<element name="courseName" type="string" />
<element name="salary" type="decimal" />
</sequence>
</complexType>
</element>
</schema>
Elements with Mixed Elements and/or Attributes:
<employee empid="101" >This is new employee is <name> bmaddi</name>and his salary is <salary>1000</salary></employee>
- mixed means it has child elements and it acceps text data also
- Since it is a complex data type we use complexType element
- For complex types we have 3datatypes(see in above table)
- we will use attribute mixed =true in complexType element
<element name="employee">
<complexType mixed="true">
<sequence><element name="empid" type="int" />
<element name="courseName" type="string" />
<element name="salary" type="decimal" />
</sequence>
</complexType>
</element>
</schema>
Elements with Empty Content and attributes:
<employee id="101" empid="bmaddi" courseName="java" salary="1000" />
- Just we need to skip element type attributes, since elements are empty
- we will use
<schema targetNamespace="http://anyname.com" xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<element name="employee">
<complexType>
<attribute name="empid" type="int" use="required" /><attribute name="courseName" type="int" default="optional" />
<attribute name="salary" type="int" fixed="1000" />
</complexType>
</element>
</schema>
Elements with Text-Data and attributes:
<employee id="101" empid="101" courseName="java" salary="1000>This is new employee</employee>
- Here attribute is mandatory,because if you remove attributes then it will become simple element.
- Since it is a complex data type we use complexType element
- For complex ypes we have 3datatypes(see in above table)
- we will use simpleContent data type (see in data types)
- To specify the text data ype we use <extension base="string">
- we can replace extension with restriction
- <restriction base="string">
<element name="employee">
<complexType>
<simpleContent><extension base="string" />
<attribute name="empid" type="int" use="required" />
<attribute name="courseName" type="int" default="optional" />
<attribute name="salary" type="int" fixed="1000" />
</simpleContent></complexType>
</element>
</schema>
User Dervied DataTypes:
There are 2 types of user defined data types:
1)Atomic
2)NonAtomic
i)list
ii)union
Atomic datatype:
- This data type is used when user want to create an user definbed data type and apply restrictions on that.
- Example if you want to apply restricion on salary of an employee, create a datatype called mysalary and apply restriction on that.
<xs:simpleType name="mySalary">
<xs:restriction base="xs:decimal">
<xs:minInclusive value="10000" />
<xs:maxInclusive value="90000" />
</xs:restriction>
</xs:simpleType>
Eg 2:
<xs:simpleType name="carBrands">
<xs:restriction base="xs:string">
<xs:enumeration value="bmw" />
<xs:enumeration value="audi" />
<xs:enumeration value="lamborgini" />
</xs:restriction>
</xs:simpleType>
Nonatomic datatype:
list type:
- If we want to collect multiple group of values as single unit then we will go for list
- Example, if we want to make a datatype of contactNumbers, which groups all the contact numbers then we will use list
Eg 1:
<xs:simpleType name="contactnumbers">
<xs:list itemType="xs:string" />
</xs:simpleType>
Eg 2:(To group dates as vacation)
<xs:simpleType name="vacations">
<xs:list itemType="xs:date" />
</xs:simpleType>
Union Type:
- If we want to combine one or more data types then we will use union type.
- example,we will create 2 atomic data types Runningrace and gymnastics and we will combine then uisng union.
<xs:simpleType name="RunningRace">
<xs:restriction base="xs:string">
<xs:enumeration value="100 meters" />
<xs:enumeration value="1 km" />
<xs:enumeration value="10 km" />
</xs:simpleType>
<xs:simpleType name="Gymnasics">
<xs:restriction base="xs:string">
<xs:enumeration value="floor" />
<xs:enumeration value="rings" />
<xs:enumeration value="beam" />
</xs:simpleType>
<xs:simpleType name="Event">
<xs:union memberTypes ="RunningRace,Gymnastics">
</xs:simpleType>
Example using all user datatypes:
userderivedtypes.xsd
<xs:schema targetNamespace="http://anyname.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ba="http://anyname.com" elementFormDefault="qualified">
<xs:simpleType name="mysalary">
<xs:restriction base="xs:decimal">
<xs:minInclusive value="10000" />
<xs:maxInclusive value="90000" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="contactnumbers">
<xs:list itemType="xs:string" />
</xs:simpleType>
<xs:simpleType name="vacations">
<xs:list itemType="xs:date" />
</xs:simpleType>
<xs:element name="employees">
<xs:complexType>
<xs:sequence>
<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element name="empid" type="xs:int" />
<xs:element name="courseName" type="xs:string" />
<xs:element name="salary" type="ba:mysalary" />
<xs:element name="vacataiondates" type="ba:vacations" />
<xs:element name="contactnumbers" type="ba:contactnumbers" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
xml file:
<employees xmlns="http://anyname.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://anyname.com C:\Users\bmaddi\Desktop\MyCollection\xml\datatypes.xsd">
<employee>
<employee>
<empid>101</empid>
<courseName>bhuvan</courseName>
<salary>30000</salary>
<vacataiondates>2021-09-12 2021-08-12</vacataiondates>
<contactnumbers>12356784 1737839890</contactnumbers>
</employee>
</employees>
Applying restrictions:
- if we want to apply any restrictions on an element,we can apply restrictions on salary of an employee as below.
<element name="salary" type="decimal" >
</element>
with restrictions:
<element name="salary" >
<simpleType>
<restriction base="decimal">
<minInclusive value="10000" />
<maxInclusive value="90000" />
</restriction>
</simpleType>
</element>
samplexsdfile.xsd
<schema targetNamespace="http://anyname.com" xmlns:"http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<element name="employees">
<complexType>
<sequence>
<element name="employee" minOccurs="0" maxOccurs="10">
<complexType>
<sequence>
<element name="empid" type="int" />
<element name="courseName" type="string" />
</complexType>
</element>
</sequence>
</complexType>
</element>
<element name="employees">
<complexType>
<sequence>
<element name="employee" minOccurs="0" maxOccurs="10">
<complexType>
<sequence>
<element name="empid" type="int" />
<element name="courseName" type="string" />
<element name="salary" >
<simpleType>
<restriction base="decimal">
<minInclusive value="10000" />
<maxInclusive value="90000" />
</restriction>
</simpleType>
</element>
</sequence></complexType>
</element>
</sequence>
</complexType>
</element>
</schema>
- Very very important
- If we want to apply restriction on 10 elements or more it will beome difficult.So in that case the best solution is create an atomic datatype and use that datatype whereever required to all elements, as shown below
- Another important point to consider here is name space.
- Since all the elements in the xsd belonds to "http://www.w3.org/2001/XMLSchema" namespace,if we declare our own atomic userdefined variable normally,then parser will check in default name space and will fail.
- So the user defined data type belongs to our own namespace defined in targetNamespace "http://anyname.com"
- we need to use "http://anyname.com" namespace prefix before our user defined datatype wvery time.So create a reference of it using xmlns and use it anywhere
<schema targetNamespace="http://anyname.com" xmlns:"http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" xmlns:ba="http://anyname.com">
<simpleType name="MySalary">
<restriction base="decimal">
<minInclusive value="10000" />
<maxInclusive value="90000" />
</restriction>
</simpleType>
<element name="employees">
<complexType><sequence>
<element name="employee" minOccurs="0" maxOccurs="10">
<complexType>
<sequence>
<element name="empid" type="int" />
<element name="courseName" type="string" />
<element name="salary" type="ba:MySalary" >
</sequence></complexType>
</element>
</sequence>
</complexType>
</element>
</schema>
Comments
Post a Comment