support Click to see our new support page.

XML Coding Standards

XML Coding Standards
Author

Sahdiya MammoottyOct. 5, 2019

eXtensible Markup Language-XML

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.

The Difference Between XML and HTML

XML and HTML were design with different goals:

  • XML (Extensible Markup Language ) was design to carry data - that is, with a focus on what data is.
  • HTML (Hypertext Markup Language ) was design to display data - that is, with a focus on how data looks.
  • Furthermore, XML tags are not predefine like HTML tags are.

The Important XML standards are;

XML DTD, XML DOM, XML Path,XML XSLT, XML XQuery, XML schema.

 

XML DTD-XMLFile Type Definition/Declaration

  • Use to describe XML file Language precisely.
  • Use to define the structure of an XML document.
  • Contain the list of legal element.
  • It is also perform validation.

Syntax:

<!DOCTYPE element DTD identifier[declaration1 declaration2]>

Types of DTD

INTERNAL DTD: Elements are define within the XML files.

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>

EXTERNAL DTD: Elements are declare outside the files.

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>

 

XMLNamespace

  • It is use to avoid element name conflict.
  • It is a set of unique name.
  • Identified by URI ( Uniform Resource Identifier ).
  • Attributes name start with “xmlns” name.

Syntax:

<element xmlns:name “URL”>

Conflict:

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.

 

Solving the Name Conflict Using a Prefix:

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".

XML DOM-(Document Object Model)

WHAT IS DOM?

  • DOM is the Document object model.
  • The DOM defines a standard way for accessing and manipulating.
  • XML file documents like xml and html.
  • The DOM is a W3C(World Wide Web) standard.
  • The DOM is memory based, that is, which makes  it traversable and also editable.
  • DOM is not language specific, nor is it platform specific.

 

WHAT IS XML DOM?

  • A standard Object model.
  • The W3C(World Wide Web) standard.
  • A standard programming interface for xmlPlatform and also language independent.

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.

According to the XML DOM, everything in an XML file document is a node:

  • The entire document is a document node.
  • Every XML(Extensible Markup Language) element is an element node.
  • The text in the elements are text nodes.
  • Every attribute is an attribute node.
  • Also every comments are comment nodes.

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>

xml-dom-struc

 

XML DOM property

The property is used to get all the details about the nodes. Some of the DOM property is shown below;

  • x.nodeName - the name of x
  • x.nodeValue - the Value of x
  • x.parentNode - the parent node of x
  • x.childNodes - the child node of x
  • X.attribute - the attribute node of x
    Where ‘x’ is a node object.

 

Methods

  • x.getElementsByTagName(name); - get all elements with a specified tag name
  • x.appendchild(node); - insert a child node to x
  • x.removechild(node); - remove a child node from x
    Where ‘x’ is a node object.

Examples:

txt=xmlDoc.getElementsByTagname(“title”)[0].childNodeValue

  • xmlDoc-the XML DOM object
  • xmlDoc.getElementsByTagname(“title”)[0] - the first <title> element
  • Childnodes[0] - the first child of the <title> element (the text node)
  • NodeValue - the value of the node (the text itself)

 

Advantages

  • Extensible Markup Language structure is traversable.
    That is, each node can be randomly accessed(one or more times) by traversing the tree.
  • Structure is modifiable.
    Since the XML file structure is resident in memory, values can be added, changed, and removed.
  • The DOM standard is maintained by the word wide web consortium.

 

Disadvantages

  • Resource Intensive
    Since the XML structure is resident in memory the larger the XML structure is, the more memory it will consume.
  • Relative Speed
    In comparison to SAX, DOM can be much slower due to its resource usage/needs.

 

Uses

  • It defines a standard way for accessing and manipulating XML Document.
  • The DOM presents an XML file document as a structure.
  • Knowing it makes work easier with XML file.

XML-XPATH

  • XPath is a major element in the XSLT standard.
  • It can be used to navigate through elements and also the attributes in an XML document.
  • XPath is a syntax for defining parts of an XML document.
  • It uses path expressions to navigate in XML documents.
  • It contains a library of standard functions.
  • XPath is a major element in XSLT and XQuery.
  • It is a W3C recommendation.

extensible markup language-xpath

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>

Relationship of Nodes:

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:

  • an axis (defines the tree-relationship between the selected nodes and the current node)
  • a node-test (identifies a node within an axis)
  • zero or more predicates (to further refine the selected node-set)

The syntax for a location step is:

axisname::nodetest[predicate]

XML XSLT - eXtensible Style-sheet Language Translations.

With XSLT you can transform an XML document into HTML.

Displaying XML with XSLT:

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>

 

XML and XQuery

XQuery is:

  • XQuery is to XML what SQL is to databases.
  • It was designed to query XML data.
  • It is a language for querying XML data.
  • XQuery for XML is like SQL for databases.
  • It is built on XPath expressions.
  • It is also supported by all major databases.
  • XQuery is a W3C Recommendation.

extensible markup language-xquery

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:

  • Extract information to use in a Web Service.
  • Can also generate summary reports
  • Transform XML data to XHTML
  • It also search Web documents for relevant information

 

XML, XLink and XPointer

XLink is use to create hyperlinks in XML documents.

  • XLink is use to create hyperlinks within XML documents.
  • Any element in an XML document can behave as a link.
  • With XLink, the links can be define outside the linked files.
  • XLink is also a W3C Recommendation.

extensible markup language- xlink

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>
  • To get access to the XLink features we must declare the XLink namespace. The XLink namespace is: "http://www.w3.org/1999/xlink".
  • The xlink:type and the xlink:href attributes in the <homepage> elements come from the XLink namespace.
  • The xlink:type="simple" creates a simple "HTML-like" link (means "click here to go there").
  • The xlink:href attribute specifies the URL to link to.

 

XLink - Going Further:-

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>

XPointer

  • XPointer allows links to point to specific parts of an XML document.
  • It uses XPath expressions to navigate in the XML document.
  • Likewise XLINK , it is a W3C Recommendation.

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

  • An XML Schema describes the structure of an XML document, just like a DTD.
  • Document with correct syntax is called "Well Formed".
  • Document validated against an XML Schema is both "Well Formed" and also "Valid".

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>

 

Characteristics:

  • XML Schemas are written in XML.
  • It will be extensible to additions.
  • They also support data types.
  • They also support namespaces.

 

XML Schemas use XML Syntax

Another great strength about XML Schemas is that they are written in XML:

  • Don't have to learn a new language.
  • Can also use your XML editor to edit your Schema files.
  • Can also use your XML parser to parse your Schema files.
  • You can also manipulate your Schemas.
  • You can also transform your Schemas with XSLT.

Odoo_ERP_Services

LinkedIn LinkedIn

Leave a Comment

Comments