SOA Basics – Introduction to XML

June 17, 2019 XML

Introduction to XML

XML stands for extensible Markup Language and is
designed to transport and store data. </strong >

E.g. 
<Library>
<Book>
<Name>XML</Name>
<Author>W3 Schools</Author>
<Date>1/1/2001</Date>
<Price currency=”USD”>5.99</Price>
..
</Book>
<Book>
<Name>SOA Suite 11g Essentials</Name>
<Author>W3 Schools</Author>
<Date>1/1/2001</Date>
<Price currency=”USD”>19.99</Price>

</Book>
</Library>

XML Syntax

XML is used in many aspects of web development, often to simplify
data storage, transport and sharing.

XML documents form a tree structure that starts at “the root” and branches
to “the leaves”. Note in the above Example, you have Library that is the
“root” element with leaf elements “Book” and sub-child “Name”, “Author”,
“Date” etc.

The syntax rules of XML are very simple and logical. The rules are easy to
learn, and easy to use.

    • All XML Elements Must Have a Closing Tag
    • XML Tags are Case Sensitive
    • XML Elements Must be Properly Nested
    • XML Documents Must Have a Root Element
    • XML Attribute Values Must be Quoted
    • Entity References

 </strong >XML Element

 </strong >An XML element is everything from (including) the element’s start tag to
(including) the element’s end tag.

 </strong >An element can contain:

    • other elements
    • text
    • attributes
    • or a mix of all of the above…

 </strong >XML elements must follow these naming rules:

  •  </strong >Names can contain letters, numbers, and other characters
  • Names cannot start with a number or punctuation character
  • Names cannot start with the letters xml (or XML, or Xml, etc)
  • Names cannot contain spaces

XML Attributes

XML elements can have attributes, just like HTML. Attributes provide
additional information about an element. Attributes often provide
information that is not a part of the data. In the example below, the file
type is irrelevant to the data, but can be important to the software that
wants to manipulate the element:

In the E.g. XML above, you notice that the “currency” is attribute about the
“Price” element

XML Namespaces

XML Namespaces provide a method to avoid element name conflicts. Lets take
an example of the following two XML’s


If these XML fragments were added together, there would be a name conflict.
Both contain a <table> element, but the elements have different
content and meaning.
<table>
<item>apples<item>
<item>oranges<item>
<item>banana<item>
</table>

Use Prefix to resolve such a conflict.A well formed XML should refer to its
element and content without any ambiguity. In the above example, there are
two table, one table is furniture and another table is container to hold
item.

<f:table>
<f:width>1.0</f:width>
<f:length>2.0</f:length>
<f:height>3.0</f:height>
</f:table>
<c:table>
<c:item>apples</c:item>
<c:item>oranges</c:item>
<c:item>banana</c:item>
</c:table>

When using prefixes in XML, a so-called namespace for the prefix must be
defined. The namespace is defined by the xmlns attribute in the start tag of
an element. The namespace declaration has the following syntax.
xmlns:prefix=”URI”.
<f:table xmlns:f=”http://xmlns.vivid-egd.com/furniture”>
<f:width>1.0</f:width>
<f:length>2.0</f:length>
<f:height>3.0</f:height>
<f:table>
<c:table xmlns:c=”http://xmlns.demo.com/container”>
<c:item>apples<c:item>
<c:item>oranges<c:item>
<c:item>banana<c:item>
<c:table>

In the example above, the xmlns attribute in the <table> tag give the
f: and c: prefixes a qualified namespace. When a namespace is defined for an
element, all child elements with the same prefix are associated with the
same namespace.

Namespaces can be declared in the elements where they are used or in the XML
root element:

<?xml version=”1.0″?>
<root  xmlns:f=”http://xmlns.demo.com/furniture”
xmlns:c=”http://xmlns.demo.com/container”>
<f:table>
<f:width>1.0</f:width>
<f:length>2.0</f:length>
<f:height>3.0</f:height>
<f:table>
<c:table >
<c:item>apples<c:item>
<c:item>oranges<c:item>
<c:item>banana<c:item>
<c:table>
</root>

An XML Schema describes the structure of an XML document.What is XML schema?

<?xml version=”1.0″?>
<xs:schema xmlns:xs=”http://www.w3.org/2001/XMLSchema&#8221; xmlns:lib=”http://xmlns.demo.com/lib” targetNamespace=”http://xmlns.demo.com/lib”xmlns:book=”http://xmlns.demo.com/book”>
<xs:import location=”./booktype.xsd” xmlns=”http://xmlns.demo.com/book”>
<xs:complexType name=”MfgInfoType”>
<xs:sequence>
<xs:element name=”MfgName” type=”xs:string”/>
</xs:sequence>
</xs:complexType>
<xs:element name=”Library”>
<xs:complexType>
<xs:sequence>
<xs:element name=”Book” type=”book:BookType”/>
<xs:element name=”Mfg” type=”lib:MfgInfoType”/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

An XML Schema:The purpose of an XML Schema is to define the legal building
blocks of an XML document, just like a DTD.

  • defines elements that can appear in a document
  • defines attributes that can appear in a document
  • defines which elements are child elements
  • defines the order of child elements
  • defines the number of child elements
  • defines whether an element is empty or can include text
  • defines data types for elements and attributes
  • defines default and fixed values for elements and attributes

Comparison between RDBMS and XML

RDBMS XML
Row in table XML Document
Table XML Schema
SQL XPATH Queries (E.g. /Library/Book[1]/Name)
Data Manipulations / PLSQL XSL – Transformation
User Namespace

Other Topics

XPATH, XSL, XML Parsers