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>
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
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
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.
Wild cart expressions:
* Matches any element node
@* Matches any attribute node
node() Matches any node of any kind
Example:
for $x in doc("books.xml")/bookstore/book
where $x/price>30
order by $x/title
return $x/title
The XML Example Document
We will use the following XML document in the examples below.
"books.xml":
<?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
Post a Comment