Tuesday, August 18, 2020

Types in XML

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

<book>
    <title>XML Basics</title>
    <author>John Doe</author>
</book>  

Attributes: Attributes provide additional information about an element. They are always specified within the opening tag of an element.

 Exxml

<book ISBN="978-1234567890">
    <title>XML Basics</title>
    <author>John Doe</author>
</book>  

Text Content: XML elements can contain text content. In the example above, the <title> and <author> elements contain text content.

Self-Closing Elements: Some elements don't have a separate closing tag and are self-closed with a "/" before the closing angle bracket.

Ex: xml
<image source="image.jpg" />

Comments: Comments in XML are enclosed in <!-- and -->.

Ex: xml
<!-- This is a comment -->

CDATA Sections: CDATA sections allow you to include character data that should not be treated as XML markup. They are enclosed in <![CDATA[ and ]]>.

Ex: xml
<![CDATA[This is some <data> that won't be parsed as XML]]>

Processing Instructions: Processing instructions provide information to applications processing the XML. They start with <? and end with ?>.

Ex: xml
<?xml version="1.0" encoding="UTF-8"?>

Entities: XML has predefined entities like &lt; for <, &gt; for >, &amp; for &, &quot; for ", and &apos; for '. These are used to represent special characters.

Ex: xml
<text>1 &lt; 2</text>

Namespaces: XML namespaces are used to avoid naming conflicts by qualifying element and attribute names with a namespace prefix.

Ex: xml
<ns:element xmlns:ns="http://example.com">
    <ns:subelement>Content</ns:subelement>
</ns:element>

DTD (Document Type Definition) and XSD (XML Schema Definition): These are used to define the structure and data types of XML documents. They specify what elements and attributes are valid in a document and their relationships.

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

SOA Overview Part-1

  Middleware It provides a mechanism for the process to interact with other processes running on multiple network machines. Advantages...