Sahdiya MammoottyOct. 5, 2019
XML file is an extension for Extensible Markup Language file format. XML was design to store and transport data. It was also design to be both human- and machine-readable.
XML and HTML were design with different goals:
The Important XML standards are;
XML DTD, XML DOM, XML Path,XML XSLT, XML XQuery, XML schema.
Syntax:
<!DOCTYPE element DTD identifier[declaration1 declaration2]>
Syntax:
<!DOCTYPE root-element[element-declaration]>
Example;
<?xml version="1.0"?> <!DOCTYPE note [ <!ELEMENT note (to,from,heading,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)> ]> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend</body> </note>
Syntax:
<!DOCTYPE root-element SYSTEM “file-name”>
Example;
<?xml version="1.0"?> <!DOCTYPE note SYSTEM "note.dtd"> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
Syntax:
<element xmlns:name “URL”>
Generally Conflict occurs when we try to mix documents from different xml application.
Example of conflict:
1.xml
<class> <name>TECH</name> <class>
2.xml
<class> <name>TECH</name> <class>
Here conflict occurs due to the same element name.
Name conflicts in XML (Extensible Markup Language) file can easily be avoided using a name prefix.
This carries information about an HTML table, and a piece of 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>
So, in the above example there will be no conflict, because the two <table> elements have different names.
When using prefixes, a namespace for the prefix must be defined. The namespace can be defined by an xmlns attribute in the start tag of an element. The namespace declaration has the following syntax: xmlns:prefix="URI".
The DOM defines a standard for accessing and manipulating documents:
“The W3C Document Object Model (DOM) is a platform and language-neutral interface that allows programs and scripts to dynamically access and update the content, structure, and style of a document.”
The HTML DOM defines a standard way for accessing and manipulating HTML documents. It presents an HTML document as a tree-structure. The XML DOM defines a standard way for accessing and manipulating XML documents. It also presents an XML file document as a tree-structure. All HTML elements can be access through the HTML DOM.
Example:
<?xml version="1.0" encoding="UTF-8"?> <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> </bookstore>
The property is used to get all the details about the nodes. Some of the DOM property is shown below;
Examples:
txt=xmlDoc.getElementsByTagname(“title”)[0].childNodeValue
XPath Path Expressions: XPath uses path expressions to select nodes or node-sets in an XML document. These path expressions look very much like the expressions you see when you work with a traditional computer file system.
XPath is also used in XSLT. XPath is a major element in the XSLT standard. So, with XPath knowledge you will be able to take great advantage of XSL.
Exampe:
<?xml version="1.0" encoding="UTF-8"?> <bookstore> <book> <title>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> </bookstore>
XPath Nodes: In XPath, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document nodes. XML documents are treated as trees of nodes. That is, the topmost element of the tree is called the root element.
Example:
<?xml version="1.0" encoding="UTF-8"?> <bookstore> <!--root element node--> <book> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <!--element node--> <year>2005</year> <price>29.99</price> </book> </bookstore>
1) Parent:
Each element and attribute has one parent.
In the following example; the book element is the parent of the title, author, year, and price.
<bookstore> <book> <title>Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> </bookstore>
2) Children:
Element nodes may have zero, one or more children.
In the above example; the title,author, year, and price elements are all children of the book element.
3) Siblings:
Nodes that have the same parent.
In the above example; the title, author, year, and price elements are all siblings.
4) Ancestors:
A node's parent, parent's parent, etc.
In the following example; the ancestors of the title element are the book element and the bookstore element.
5) Descendants:
A node's children, children's children, etc.
In the following example; descendants of the bookstore element are the book, title, author, year, and also price elements.
XPath Syntax: XPath uses path expressions to select nodes or node-sets in an XML document. The node is selected by following a path or steps.
XPath Axes: An axis represents a relationship to the context (current) node, and is used to locate nodes relative to that node on the tree.
Location Path Expression: A location path can be absolute or relative. An absolute location path starts with a slash ( / ) and a relative location path does not. In both cases, the location path consists of one or more steps, each separated by a slash.
An absolute location path: /step/step/... A relative location path: step/step/...
Each step is evaluated against the nodes in the current node-set.
A step consists of:
The syntax for a location step is:
axisname::nodetest[predicate]
With XSLT you can transform an XML document into HTML.
XSLT (eXtensible Stylesheet Language Transformations) is the recommended style sheet language for XML.
XSLT is far more sophisticated than CSS. With XSLT you can add/remove elements and attributes to or from the output file. You can also rearrange and sort elements, perform tests and make decisions about which element to hide and display, and a lot more.
XSLT uses XPath to find information in an XML document.
Example:
<?xml version="1.0" encoding="UTF-8"?> <breakfast_menu> <food> <name>Belgian Waffles</name> <price>$5.95</price> <description>Two of our famous Belgian Waffles with plenty of real maple syrup</description> <calories>650</calories> </food> <food> <name>Berry-Berry Belgian Waffles</name> <price>$8.95</price> <description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description> <calories>900</calories> </food> <food> <name>French Toast</name> <price>$4.50</price> <description>Thick slices made from our homemade sourdough bread</description> <calories>600</calories> </food> </breakfast_menu>
Use XSLT to transform XML into HTML, before it is displayed in a browser.
Example of XSLT style-sheet:
<?xml version="1.0" encoding="UTF-8"?> <html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE"> <xsl:for-each select="breakfast_menu/food"> <div style="background-color:teal;color:white;padding:4px"> <span style="font-weight:bold"><xsl:value-of select="name"/> - </span> <xsl:value-of select="price"/> </div> <div style="margin-left:20px;margin-bottom:1em;font-size:10pt"> <p> <xsl:value-of select="description"/> <span style="font-style:italic"> (<xsl:value-of select="calories"/> calories per serving)</span> </p> </div> </xsl:for-each> </body> </html>
Example of XQuery:
for $x in doc("books.xml")/bookstore/book where $x/price>30 order by $x/title return $x/title
XQuery is About Querying XML:-XQuery is a language for finding and extracting elements and attributes from XML documents.
Here is an example of what XQuery could solve:
"Select all CD records with a price less than $10 from the CD collection stored in cd_catalog.xml".
XQuery and XPath:-XQuery 1.0 and XPath 2.0 share the same data model and support the same functions and operators. If you have already studied XPath you will have no problems with understanding XQuery.
XQuery can be use to:
XLink is use to create hyperlinks in XML documents.
XLink Syntax:- In HTML, the <a> element defines a hyperlink. However, this is not how it works in XML. In XML documents, you can use whatever element names you want - therefore it is impossible for browsers to predict what link elements will be called in XML documents.
Example of how to use XLink to create links in an XML document:
<?xml version="1.0" encoding="UTF-8"?> <homepages xmlns:xlink="http://www.w3.org/1999/xlink"> <homepage xlink:type="simple" xlink:href="https://www.w3schools.com">Visit W3Schools</homepage> <homepage xlink:type="simple" xlink:href="http://www.w3.org">Visit W3C</homepage> </homepages>
In the example above we have demonstrated simple XLinks. XLink is getting more interesting when accessing remote locations as resources, instead of standalone pages.
If we set the value of the xlink:show attribute to "embed", the linked resource should be processed inline within the page. When you consider that this could be another XML document you could, for example, build a hierarchy of XML documents.
You can also specify WHEN the resource should appear, with the xlink:actuate attribute.
Example:
<?xml version="1.0" encoding="UTF-8"?> <bookstore xmlns:xlink="http://www.w3.org/1999/xlink"> <book title="Harry Potter"> <description xlink:type="simple" xlink:href="/images/HPotter.gif" xlink:show="new"> As his fifth year at Hogwarts School of Witchcraft and Wizardry approaches, 15-year-old Harry Potter is....... </description> </book> <book title="XQuery Kick Start"> <description xlink:type="simple" xlink:href="/images/XQuery.gif" xlink:show="new"> XQuery Kick Start delivers a concise introduction to the XQuery standard....... </description> </book> </bookstore>
Example:
<?xml version="1.0" encoding="UTF-8"?> <dogbreeds> <dog breed="Rottweiler" id="Rottweiler"> <picture url="https://dog.com/rottweiler.gif" /> <history>The Rottweiler's ancestors were probably Roman drover dogs.....</history> <temperament>Confident, bold, alert and imposing, the Rottweiler is a popular choice for its ability to protect....</temperament> </dog> <dog breed="FCRetriever" id="FCRetriever"> <picture url="https://dog.com/fcretriever.gif" /> <history>One of the earliest uses of retrieving dogs was to help fishermen retrieve fish from the water....</history> <temperament>The flat-coated retriever is a sweet, exuberant, lively dog that loves to play and retrieve....</temperament> </dog> </dogbreeds>
Note that the XML document above also uses id attributes on each element!
So, instead of linking to the entire document (as with XLink), XPointer allows you to link to specific parts of the document. To link to a specific part of a page, add a number sign (#) and an XPointer expression after the URL in the xlink:href attribute, like this: xlink:href="https://dog.com/dogbreeds.xml#xpointer(id('Rottweiler'))". The expression refers to the element in the target document, with the id value of "Rottweiler".
XPointer also allows a shorthand method for linking to an element with an id. You can use the value of the id directly, like this: xlink:href="https://dog.com/dogbreeds.xml#Rottweiler".
XML Schema is an XML-based alternative to DTD.
Example:
<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>
Another great strength about XML Schemas is that they are written in XML:
3