XML basics

XML: Extensible Markup Language: (XML) lets you define and store data in a shareable manner. XML supports information exchange between computer systems such as websites, databases, and third-party applications, 

This are 2 types 

1. well-formed XML ....XSD

2. valid XML.......DTD

syntax:

<root>

  <child>

    <subchild>.....</subchild>

  </child>

</root>

Parts in XML:

Header declaration
Root Element
Child Elements
Sub child elements  
Tags --- < >
Data
attributes  

Example: 

<?xml version="1.0" encoding="UTF-8"?> 
<Address>
<Hno>5678</Hno>
<Street>Ameerpet</Street>
<Pincode>500001</Pincode> 
<City>Hyderabad</City>
<State>Telangana</State>
</Address>

XML Family:

XML - Extensible Markup Language

XSD - Extensible Schema Definition

XSLT - Extensible Style Sheet Language Transformation

Xpath

Xquery

Mandate rules to write XML:

1) Root element is Must ---- Define

2) Properly Defined Tags  --- <  >

3) Tags must follow Case Sensitive --- Case Sensitive

4) Properly Close Root element at the end of the message

5) Attributes should be Quoted ' ' or " "  ( Optional)


Well-Formed XML: Soa is now using well-formed XML it is advance version of valid XML 

1) Root element is Must ---- Define

2) Properly Defined Tags  --- <  >

3) Tags must follow Case Sensitive --- Case Sensitive

4) Properly Close Root element at the end of the message

5) Attributes should be Quoted ' ' or " "  ( Optional)

6) Well formed XML will Support XSD norms  (Extensible Schema Definition)

Valid XML:

1) Root element is Must ---- Define  

2) Properly Defined Tags  --- <  >

3) Tags must follow Case Sensitive --- Case Sensitive

4) Properly Close Root element at the end of the message

5) Attributes should be Quoted ' ' or " "  ( Optional)

6) Valid XML will Support DTD norms  (Document Type Definition)


Attribute: Providing more information about your element is called attribute this are optional 

If you are writing attribute it should be in ' ' or " ".

Example: 

<bookstore>

  <book>

    <title>Everyday Italian</title>

    <author sex="Male">Giada De Laurentiis</author>----> attribute 

    <DOB>12/02/2001</DOB>

    <year>2005</year>

    <price>30.00</price>

  </book>

  <book>

    <title>Harry Potter</title>

    <author>J K. Rowling</author>

    <year>2005</year>

    <price currency="USD">29.99</price>

  </book>

  </bookstore>

XML Namespaces:

Namespace will help you to avoid the element name conflicts in XML

There are 3 types of namespaces

1. Namespace

2.Defult Namespace

3.Target Namespace 


1) Namespace: The Name spaces can be defined by an xmlns attribute in the start tag of an element and all the child elements with the same prifixs are associated with the same namespace 

Syntax: 

xmlns:prefix= "URI" or  xmlns:prefix = "URL"

EG:1

<root>

<h:table xmlns:h="http://www.w3.org/TR/html4/">

  <h:tr>

    <h:td>Apples</h:td>

    <h:td>Bananas</h:td>

</h:tr>

</h:table>


<f:table xmlns:f="https://www.w3schools.com/furniture">

  <f:name>African Coffee Table</f:name>

  <f:width>80</f:width>

  <f:length>120</f:length>

</f:table>

</root>

  • we can also define all the namespace in root element it self 

<root xmlns:h="http://www.w3.org/TR/html4/"
xmlns:f="https://www.w3schools.com/furniture">

<h:table>
  <h:tr>
    <h:td>Apples</h:td>
    <h:td>Bananas</h:td>
  </h:tr>
</h:table>

<f:table>
  <f:name>African Coffee Table</f:name>
  <f:width>80</f:width>
  <f:length>120</f:length>
</f:table>

</root>

2) Default namespace: Defining a Default namespace for an element saves us from using prifixs in all chaild elements 

syntax:
     xmlns = "namespaceURI"

EG:
<table xmlns="http://www.w3.org/TR/html4/">
  <tr>
    <td>Apples</td>
    <td>Bananas</td>
  </tr>

3) Target Namespace:The namespace of all schema components in this schema as well as any schema included using the include element. Included schemas must either have the same target namespace as the containing schema or have no target namespace specified.

Syntax: 
targetnamespace = "URI"

targetnamespace = http://xmlns.oracle.com/POProcessing/Hyderabad/Ameerpet

targetnamespace = http://xmlns.oracle.com/POProcessing/Hyderabad1/Ameerpet

XSD - Extensible Schema Definition: XSD is used to describe the structure  of a XML documents with Data types,  XSD will carry inputs to the webservice(IRCTC) and bring output from the webservice

XSD's are two types:

1. Complex XSD
2. Simple XSD


1. Complex XML : If you have more than one element, that is complex type of XSD

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

<xs:element name="note">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="to" type="xs:string"/>
      <xs:element name="from" type="xs:string"/>
      <xs:element name="heading" type="xs:string"/>
      <xs:element name="body" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
</xs:schema> 

2) Simple Type: if have one elemet we called as simple elemet 

Example:
 
<xs:element name="note">
<number> 12 </number> 
</xs:element>

XSLT - Extensible Style Sheet Language Transformation: XSLT is used to transform an XML document into another XML document, or another type of document that is recognized by a browser, like HTML and XHTML. Normally XSLT does this by transforming each XML element into an (X)HTML element.

XSLT stands for XSL Transformations. In this tutorial you will learn how to use XSLT to transform XML documents into other formats, like XHTML.

XSL stands for EXtensible Stylesheet Language

XSL consists of three parts:

      •   XSLT - a language for transforming XML documents

      •   XPath - a language for navigating in XML documents

      •    XSL-FO - a language for formatting XML documents


XPath: a language for navigating/Read in XML documents

By Using Xpath expressions we will read the XML document

We have following Xpath expressions:

/   - It will read from Root element

// -- It will read child elements

.

..

@  - It will read attributes



Example: Let's take an example to see the usage of XPath expression. Here, we use an xml file "employee.xml" and a stylesheet for that xml file named "employee.xsl". The XSL file uses the XPath expressions under select attribute of various XSL tags to fetchvalues of id, firstname, lastname, nickname andsalary of each employee node.


<?xml version = "1.0" encoding = "UTF-8"?>   
<xsl:stylesheet version = "1.0">   
   xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">     
   <xsl:template match = "/">   
      <html>   
         <body>   
            <h2> Employees</h2>   
            <table border = "1>   
               <tr bgcolor = "pink">   
                  <th> ID</th>  
                  <th> First Name</th>   
                  <th> Last Name</th>   
                  <th> Nick Name</th>   
                  <th> Salary</th>   
               </tr>   
               <xsl:for-each select = "class/employee">   
                  <tr>  
                     <td> <xsl:value-of select = "@id"/> </td>   
                     <td> <xsl:value-of select = "firstname"/> </td>   
                     <td> <xsl:value-of select = "lastname"/> </td>   
                     <td> <xsl:value-of select = "nickname"/> </td>   
                     <td> <xsl:value-of select = "salary"/> </td>   
                  </tr>   
               </xsl:for-each>   
            </table>   
         </body>   
      </html>   
   </xsl:template>   
</xsl:stylesheet>   

Out put:












Wild cart expressions:

*         Matches any element node

@*         Matches any attribute node

node() Matches any node of any kind



XQuery: XQuery is a functional query language used to retrieve information stored in XML format. It is same as for XML what SQL is for databases. It was designed to query XML data.

Example: 

for $x in doc(&quot;books.xml&quot;)/bookstore/book

where $x/price&gt;30

order by $x/title

return $x/title

The XML Example Document

We will use the following XML document in the examples below.

&quot;books.xml&quot:

<?xml version="1.0" encoding="ISO-8859-1"?>


<bookstore>


<book category="COOKING">

  <title lang="en">Everyday Italian</title>

  <author>Giada De Laurentiis</author>

  <year>2005</year>

  <price>30.00</price>

</book>


<book category="CHILDREN">

  <title lang="en">Harry Potter</title>

  <author>J K. Rowling</author>

  <year>2005</year>

  <price>29.99</price>

</book>


<book category="WEB">

  <title lang="en">XQuery Kick Start</title>

  <author>James McGovern</author>

  <author>Per Bothner</author>

  <author>Kurt Cagle</author>

  <author>James Linn</author>

  <author>Vaidyanathan Nagarajan</author>

  <year>2003</year>

  <price>49.99</price>

</book>


<book category="WEB">

  <title lang="en">Learning XML</title>

  <author>Erik T. Ray</author>

  <year>2003</year>

  <price>39.95</price>

</book>


</bookstore> 


How to Select Nodes From "books.xml"?

The doc () function is used to open the "books.xml" file:

doc("books.xml") 


doc("books.xml")/bookstore/book/title

The XQuery above will extract the following:

<title lang="en">Everyday Italian</title>

<title lang="en">Harry Potter</title>

<title lang="en">XQuery Kick Start</title>

<title lang="en">Learning XML</title> 


doc("books.xml")/bookstore/book[price<30]

<book category="CHILDREN">

  <title lang="en">Harry Potter</title>

  <author>J K. Rowling</author>

  <year>2005</year>

  <price>29.99</price>

</book>



Comments

Popular posts from this blog

BEPl composite

EM console future