# XSD Tutorial – XSD Numeric Data Type

XSD Tutorial covers topics on XSD Data XSD Tutorial covers topic on XSD Data Types, XSD Text , XSD Mixed, XSD Indicators, XSD Substitution ,XSD Examples, XSD Numeric, XSD Elements.

We discuss XSD Numeric Data Type in detail

The XSD Numeric data are used to depicts the XML data in the form of Integer and Decimal.

## <xs:integer> numeric data type

<xs:integer> data type depicts the integer value in the XML document

<xs:integer> example:

<xs:element name=”age” type=“xs:integer”/>

<age>28</age>

## <xs:decimal> data type

<xs:decimal> data type depicts the decimal value in the XML document

<xs:decimal> example:

<xs:element name=”price” type=“xs:decimal”/>

<age>20.50</age>

## Numeric Data Types

 S. No Name Description 1 byte value with 8-bit integer 2 decimal decimal value 1.15 3 int value with 32-bit integer 4 integer integer value 5 long value with 64-bit integer 6 negativeInteger an integer with negative values (-1,-2,-3…) 7 nonNegativeInteger an integer with negative values (0,1,2…) 8 nonPostiveInteger an integer with non positive values  (0,-1,-2…) 9 positiveInteger an integer with positive values (1,2…) 10 short an unsigned 16-bit integer 11 unsignedShort an unsigned 16-bit integer 12 unsignedLong an unsigned 64-bit integer 13 unsignedInt an unsigned 32-bit integer 14 unsignedByte an unsigned 8-bit integer

# XSD Tutorial – XSD Date and Time

XSD Tutorial covers topics on XSD Data Types, XSD Date, XSD Empty, XSD Text , XSD Mixed, XSD Indicators, XSD Substitution ,XSD Examples, XSD Numeric, XSD Elements .

Lets discuss the XSD Date and Time <xs:date> Data Types in detail

The XSD Date <xs:date> data type depicts the Date and Time format in the XML Document.

## <xs:date> format

The <xs:date> format (YYYY-MM-DD) in the XML Document depicts

• YYYY depicts the Year
• MM depicts the Month
• DD depicts the Date

## <xs:date> example

The below example depicts the data representation in Date format in the XML document

<xs:element name=”createdDate” type =”xs:date” />

Output : <createdDate>2020-05-07</createdDate>

## XSD Date TimeZone

The TimeZone values can be handled in the date by applying the below formats

• Converting the Date with TimeZone , the date can be appended with “Z” for UTC TimeZone like <createdDate>2020-05-07Z</createdDate>
• Converting the Date with TimeZone, the data can be appended with Positive or negative time like

<createdDate>2020-05-07-04:00</createdDate> // reduces 4 hours from current system time

<createdDate>2020-05-07+04:00</createdDate> // increases 4 hours from current system time

## <xs:time> data type

<xs:time> data type depicts the time in the format hh:mm:ss where

• hh depicts the hour
• mm depicts the minute
• ss depicts the second

<xs:time> example:

<xs:element name=”creationTime” type =”xs:time”/>

<creationTime>05:07:21</creationTime>

## <xs:datetime> data type

<xs:date> data type depicts the date format (YYYY-MM-DD)

<xs:time> data type depicts the time format (hh:mm:ss)

<xs:datetime> data type depicts the date and time format together (YYYY-MM-DDThh:mm:ss) where

• YYYY depicts the Year
• MM depicts the Month
• DD depicts the Date
• T depicts the start of the Time vlaue
• hh depicts the hour
• mm depicts the minute
• ss depicts the second

<xs:element name=”creationDateTime” type =”xs:datetime”/>

<creationDateTime>2020-05-07T05:07:21</creationDateTime>

## <xs:duration> data type

<xs:duration> depicts the time interval using ” PnYnMnDTnHnMnS ” format where

• P depicts the period (required)
• nY depicts the number of years
• nM depicts the number of months
• nD depicts the number of days
• T depicts the start of a time vlaue
• nH depicts the number of hours
• nM depicts the number of minutes
• nS depicts the number of seconds

<xs:duration> example

<xs:duration>P1Y</xs:duration> // depicts period of 1 year

<xs:duration>P1Y2M</xs:duration> // depicts period of 1 year 2 month

<xs:duration>P1Y2M3D</xs:duration> // depicts period of 1 year 2 month 3 Days

# XSD Tutorial – XSD String Data Types

XSD Tutorial covers topics on XSD Data Types, XSD Date, XSD Empty, XSD Text , XSD Mixed, XSD Indicators, XSD Substitution ,XSD Examples, XSD Numeric, XSD Elements .

Lets discuss the XSD String Data Types in detail

## XSD String <xs:string> Data Type

The Data Types are required in the XML Document to represent the data in different formats like Text, Number etc. XSD String Data Type is provided to represent the characters in the XML Document. XSD String Data Type contains or supports

• Characters,
• Line Feed,
• Tab characters &
• Carriage Return

Lets see if we need to represent the User FirstName and LastName then element can be described as given below

<xs:element name=”firstName” type=“xs:string”/>

<xs:element name=”firstName” type=“xs:string”/>

Output:

<firstName>Mohit </firstName>
<lastName> Sharma </lastName>

or

<firstName> Mohit </firstName>
<lastName> Sharma </lastName>

XML Processor executes the received String Data Type element as it is

## Normalized String Data Type

Normalized String Data Type also represent String data but it does not supports Line Feed, Carriage Returns and Tab Characters.

<xs:element name=”firstName” type=“xs:normalizedString”/>

<firstName>Mohit </firstName>
or

<firstName> Mohit </firstName>

XML Process executes the above String Data Type by replacing tabs with spaces.

## XSD Token <xs:token> Data Type

The token data type has also derived from data type supporting characters, but while processing, the XML processor eliminates the line feeds, carriage returns, tabs, multiple spaces and leading and trailing spaces.

<xs:element name=”firstName” type=“xs:token”/>

<firstName>Mohit </firstName>
or

<firstName> Mohit </firstName>

## Derived String <xs:string> Data Type

 S. No Name Description 1 ID used in Schema for depicting the ID attribute in XML Document 2 IDREF used in Schema for depicting the IDREF attribute in XML Document 3 language depicts the valid language 4 Name depicts Valid XML Name 5 NMTOKEN used in Schema for depicting the NMTOKEN attribute in XML Document 6 normalizedstring contains characters that does not supports line feeds, carriage returns, or tabs 7 string contains characters that supports line feeds, carriage returns, or tabs 8 token contains charatcter that does not supports line feeds, carriage returns, tabs, multiple spaces and leading / trailing spaces

String Restrictions

XSD String data types applies the below given restrictions:

• Enumeration
• length
• minimum length
• maximum length
• pattern
• whitespace

# XQuery Tutorial – FLWOR expression

The XQuery Tutorial covers the topics on Xquery Functions, XQuery FLWOR, XQuery Syntax, XQuery Add, XQuery Select , extracting elements and attributes from XML documents, transform XML data to XHTML, XPath Operators , XPath Functions, XQuery Data Types, XQuery User-Defined Functions.

Letd discuss FLWOR expression in detail.

Xquery Tutorial on XQuery FLOWR expression shows how to binding XML nodes, XML sequences to variable, sorting the nodes and expected node return value.

## What is FLWOR expression in XQuery ?

FLWOR is an acronym for “For, Let, Where, Order by, Return”. FLWOR is pronounced as “flower”.

For – selects the sequence of nodes
Let – binds the sequence to a variable
Where – use for nodes filtering
Order by – use for nodes sorting
Return – what to return

## XQuery FLWOR node retrieval example

Lets consider the below given “order.xml” file and try to get the node retrieval based on price check where price for the order <30.

XQuery FLWOR Expression for price check:

doc(“order.xml”)/oracleappshelpOrder/order[price< 30]/orderId

The XQuery FLWOR Expression returns same result as per the below XQuery XPath expression

for \$x in doc(“order.xml”)/oracleappshelpOrder/order
where \$x/price<30
return \$x/orderId

Xquery Output:

<orderId>12346</orderId>

<orderId>12347</orderId>

```<oracleappshelpOrder>
<Order>
<orderId>12345</orderId>
<quantity>1</quantity>
<price>40<price>
</Order>
<Order>
<orderId>12346</orderId>
<quantity>1</quantity>
<price>25<price>
</Order>
<Order>
<orderId>12347</orderId>
<quantity>1</quantity>
<price>28<price>
</Order>
</oracleappshelpOrder>```

## XQuery FLWOR into HTML

Now , we can use the basic list tag to discplay the data into HTML

```<ul>
for \$x in doc("order.xml")/oracleappshelpOrder/order/orderId
order by \$x
return <li>{\$x}</li>
}
</ul> ```

<ul>

<li><orderId>12346</orderId></li>

<li> <orderId>12347</orderId> </li>

</ul>

We can also list the actual data without the orderId tag by using the data (\$x)

```<ul>
for \$x in doc("order.xml")/oracleappshelpOrder/order/orderId
order by \$x
return <li>{data{\$x}}</li>
}
</ul> ```

The output will be :

<ul>

<li>12346</li>

<li>12347</li>

</ul>

# XQuery Tutorial – Overview

The XQuery Tutorial covers the topics on Xquery Functions, XQuery FLWOR, XQuery Syntax, XQuery Add, XQuery Select , extracting elements and attributes from XML documents, transform XML data to XHTML, XPath Operators , XPath Functions, XQuery Data Types, XQuery User-Defined Functions.

Lets discuss XQuery in detail.

## What is XQuery?

The XML data includes the XML elements or called XML “nodes” which should be valid and could be parsed in a well formed XML Document. The XML data need to be queried like we use SQL Query to Query the Database data.

Thus , XQuery is designed to help in Querying the XML data.

## XQuery – Points to Consider

1. XQuery is W3C recommedation and XQuery Version 1.0 is recommendation by W3C in 2007.
2. XQuery is compatible with XML, Namespaces, XSLT, XPath, and XML Schema
3. XQuery is the language used for querying the XML data
4. XQuery is built on the XPath expressions

## How XQuery is useful ?

The XQuery can be used for the below given reasons :

1. XQuery is compatible with XML, Namespaces, XSLT, XPath, and XML Schema
2. XQuery helps in extracting the information in a heterogeneous environment integrated using XML
3. XQuery is useful in transforming the XML data to XHTML
4. helps in searching web documents for the related information.
5. XQuery helps in generating reports
```<oracleappshelpuser>
<student>
<firstName>Mohit</firstName>
<lastName>Sharma</lastName>
<registered>Yes<registered>
</student>
<student>
<firstName>Rahul</firstName>
<lastName>Jain</lastName>
<registered>Yes<registered>
</student>
<student>
<firstName>Abhishek</firstName>
<lastName>Gupta</lastName>
<registered>No<registered>
</student>
</oracleappshelpuser>```

## XQuery – Retrieve XML Node data from XML Document

XQuery provides the XQuery Functions which are used for extracting / retrieving the XML Data from the XML “nodes” in the XML Document.

Example:

doc(“student.xml”) is the XQuery function to open the Student.xml file for Query the XML Document.

We will discuss more in detail for the Query Functions in the next blog.

## XQuery – Navigate using XPath expressions

XQuery uses the in built XPath expressions for navigating to the XMl node in the XML Document

Example: If we need to navigate to the firstName node in the above provided student.xml , then the below expression should be used

doc(“student.xml”)/oracleappshelpuser/student/firstName

The XQuery output should be like:

```    <firstName>Mohit</firstName>
<firstName>Rahul</firstName>
<firstName>Abhishek</firstName>```

We will discuss more in detail for the Query XPath expression in the next blog.

# XML Tutorial – Schema- XSD (XML Schema Definition)

The tutorial provides the insight on the XML Schema – XSD ( XML Schema Validation) which defines the structure for the XML Document like XML DTD ( Document Type Definition) .

XML Schema (XSD) and XML DTD both are used to validate the XML Document. In other words, we can say the “well-formed” or “valid” XML Document is VALIDATED against XML Schema.

## XML Schema features

1. XML Schema defines the structure for the XML Document written in XML
2. XML Schema provides the description for the XSD format
3. XML Schema provides the support for data types to be defined for each XML element
4. XML Schema provides the support for XML namespaces
5. XML Schema can be verified with the sample XML Data

## XML Element

The defined XML Tags in the XML Document are elements. The XML elements should follow the below naming rules

1. XML element is case-sensitive
3. The letter “XML”, “xml” “Xml” “xMl” “xmL” are not support as element name as considered a Reserve word
4. The XML Element can consistes of letters, digits, hyphens, underscores, and periods
5. XML element with spaces is NOT supported

## XML Attributes

XML Attribute stores the data for a specific XML element.

<student gender=’male’>

## XML Element vs XML Attribute

XML Attributes are defined for the XML element. The first exmaple shows the usage of XML Attribute as gender in the student element whereas the next example shows the gender as the XML element belongs to the parent element “student”

## XML Attribute – Points to Consider

1. XML Attribute does not support multiple values
2. XML Attribute does not support Tree structure
3. XML Attribute is not flexible for change

```<student gender="male">
<firstname>Mohit</firstname>
<lastname>Sharma</lastname>
</student>

<student>
<gender> male </male>
<firstname>Mohit</firstname>
<lastname>Sharma</lastname>
</student> ```

## Empty XML Element

XML Schema supports the empty XML element. The empty XML element also support attributes.

<nickName category=”personal” ></nickName>

The empty XML element has an attribute with category =”personal”

or

<nickName />

The below given is the Student XML Schema Structure , created using XML .

<xs:element name=”student”> depicts the XML element name
<xs:complexType> depicts that the element “student” has the complex type , means it contains multple XML elements
<xs:sequence> depicts the sequence of elements mapped for the XML element student with complex type
<xs:element name=”fisrtName” type=”xs:string”/> depicts the data type as “string” for the element firstName
<xs:element name=”lastName” type=”xs:string”/> depicts the data type as “string” for the element lastName
<xs:element name=”registered” type=”xs:string”/> depicts the data type as “string” for the element registered

```<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="https://www.oracleappshelp.com"
xmlns="https://www.oracleappshelp.com"
elementFormDefault="qualified">

<xs:element name="student">

<xs:complexType>
<xs:sequence>
<xs:element name="fisrtName" type="xs:string"/>
<xs:element name="lastName" type="xs:string"/>
<xs:element name="registered" type="xs:string"/>
</xs:sequence>
</xs:complexType>

</xs:element>

</xs:schema>```

# XML DTD – Document Type Definition

To define the valid XML , DTD is being used. The Tutorial provides the details on how to Validate XML using DTD.

## What is DTD in XML ?

DTD in XML means Document Type Definition which defines the XML Structure for the XML elements in the XML Document. A well -formed or valid XML is validated against the XML DTD for its correctness.

## XML DTD Structure

The need of DTD ( Document Type Definition) is to ensure that the created XML Structure is Valid and can be parsed. Consider the below XML Data for the student which includes “student.dtd”. The DTD file defines the structure for each used element ( firstName, lastName, registered) in the XML Document.

```<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "student.dtd">
<student>
<firstName>Mohit</firstName>
<lastName>Sharma</lastName>
<registered>Yes<registered>
</student>```

```<?xml version="1.0" encoding="UTF-8"?>
<to>Mohit Sharma</to>
<subject>New Tutorial on XML DTD </subject>
<message>We have published the new turotial on XML DTD, XML Document Validation Process</message>

## XML DTD Structure Definition

The below Student DTD is interpreted as per the below provided definition.

#PCDATA” stands for Parsed Character Data which means that the data element is parsable by XML parser

!DOCTYPE student – depicts the root element of the document which is “student”
!ELEMENT student – depicts that the “Student” element should have the elements ( firstName, lastName, registered)
!ELEMENT firstName – depicts that the element to be of type “#PCDATA”
!ELEMENT lastName – depicts that the element to be of type “#PCDATA”
!ELEMENT registered – depicts that the element to be of type “#PCDATA”

```<!DOCTYPE student
[
<!ELEMENT student (firstName,lastName,registered)>
<!ELEMENT firstName (#PCDATA)>
<!ELEMENT lastName (#PCDATA)>
<!ELEMENT registered (#PCDATA)>
]>	```

We can also provide DTD within the XML Document

```<?xml version="1.0" encoding="UTF-8"?>
<!-- XML DTD declaration  -->
<!DOCTYPE student
[
<!ELEMENT student (firstName,lastName,registered)>
<!ELEMENT firstName (#PCDATA)>
<!ELEMENT lastName (#PCDATA)>
<!ELEMENT registered (#PCDATA)>
]>
<!-- XML DTD declaration  -->
<student>
<firstName>Mohit</firstName>
<lastName>Sharma</lastName>
<registered>Yes<registered>
</student>
```

# Difference between JSON and XML

The XMl and JSON are the medium for the data exchange among different applications. The tutorial provides the commonalities and comparison between JSON and XML data attributes.

## Sample JSON data

```{"student":[
{"firstname":"Mohit", "lastname":"Sharma"},
{"firstname":"Rahul", "lastname":"Jain"},
{"firstname":"Pankaj", "lastname":"Verma"}
]}```

## Sample XML Data

To understand the XML Basics, please refer the below published blog

XML Overview

```<student>
<firstName>Mohit</firstName>
<lastName>Sharma</lastName>
<registered>Yes<registered>
</student>```

## Commonalities between XML and JSON

1. JSON and XML Data supports Unicode and thus widely used by multiple technology languages like Java, php,phython,.Net
2. JSON and XML data supports XMLHttpRequest
3. JSON and XML data supports parsing
4. JSON and XML are both human readable language

## Comparison between JSON and XML

1. XML represents data in XML tags whereas JSON does not require tag for data representation
2. JSON format is more easier and simple to READ & WRITE when compared with XML data
3. XML does not supports ARRAY Data representation whereas JSON supports array and makes it easier to combine homogeneous data as a common set
4. XML data parsing is difficult when compared to JSON data
5. JSON data is short and precise compared to XML. XML document is lengthy, redundant and includes additional XML tags for namespace and other XML validation.
6. JSON is data -oriented whereas XML is document- oriented
7. XML data is more secured in comparison to JSON data

# Top XML Tutorial for beginners

## Introduction

XML (Extensible Markup Language) is derived from Standard Generalized Markup Language (SGML) and is a text based markup language used for exchanging messages among different applications
XMl markup language is independent of platform and technology languages and primarily used to store and transfer data among systems irrespective of their hardware and software compatible.
The Tutorial provides the insight for XML Basics, Advanced XML, and XML tools by providing the relevant XML examples.

## What is a Markup Language ?

XML being the markup language provides the certain set of rules for encoding the documents. XML is the Simple Document with representation of application data structured in the used defined self-descriptive XML tags.

## Characteristics of XML

1. XML is developed by World Wide Web Consortium (W3C) and is available as the Open standard.
2. XML does not provide pre-defined tags like HTML
3. XMl markup language is independent of platform and software and thus makes it a common and most popular way of interacting and sharing message data.
4. XML provides the mechanism to store and transport data rather than how data is presented
5. XML is Extensible as it provides developers to create self-descriptive tags

## How XML can be useful ?

1. XML can be used to store and arrange the data as per the application need
2. Applications uses XML for exchanging message data among different applications
3. Style Sheet can be applied to XML data
4. XML uses the Public Standard and thus supported by many languages ( java,.Net, Php, Python,Perl,etc)
5. XML document syntax correction can be validated using DTD or XML schema
6. XML supports Unicode and thus makes it easier to communicate the information
7. XML being the common standard for data exchange eliminates the data conversion issue with incompatible formats.

## XML Prolog

The below mention line should be the first line in the XML and termed as XML Prolog. This depicts the XML version and the encoding used in the XML document.

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

## XML Syntax

XML Document is case-sensitive and thus cannot be considered Valid. In the below XML document if the <firstName>Mohit Sharma</FirstName> are not same then XML validation fails. Consider the 2nd tag, where the 1st letter is mentioned as Capital.

## XML should have a open and close tag

The XML Document follows the Tree Structure where each element should have and open and close tag. This requires to ensure the correct interpretation of the XML data for each specified tag.

```<student>
<firstName>Mohit</firstName>
<lastName>Sharma</lastName>
<registered>Yes<registered>
</student>```

The bellow syntax allows to provide comments in the XML Document.

`<!-- This is just a comment -->`

There is another way to comment the XML Tag

```<student>
<firstName>Mohit</firstName>
<lastName>Sharma</lastName>
<!--registered>Yes<registered-->
</student>```

Let’s consider the sample student XML Record. XML Records looks like a Tree structure or document where the top most element in the XML Tree is termed as the ROOT element and other element(s) are termed as CHILD element(s) also. The CHILD element could have sib-child elements.

In the below given XML Document, lets categorize the elements

root element– oracleappshelp.

Child element – student, firstName, lastName, Address,registered

Sub-Child element – street, city,state, pincode, country

```<?xml version="1.0" encoding="UTF-8"?>
<oracleappshelp>
<student>
<firstName>Mohit</firstName>
<lastName>Sharma</lastName>
<street>347 Pitt St </street>
<city>Sydney</city>city>
<state>New South Wales </state>
<pincode>2000</pincode>
<country>Australia<country>
<registered>Yes<registered>
</student>
<student>
<firstName>Rahul</firstName>
<lastName>Jain</lastName>