Category Archives: xml

XML Sibling

An XML Element is said to be sibling of another XML Element only when both the element have the same parent.

Example:

In the above example <from></from>,<subject></subject> and <body></body> element are said to be siblings of one another.

XML Child

An XML Element for example element “B” is said to be child of element “A” if element “B” is directly defined inside element “A”.

Example:

In the above example <from></from>,<subject></subject> and <body></body> element are said to be child element of <message></message> tag.

XML Parent

An XML Element for example element “A” is said to be parent of element “B” if element “B” is directly defined inside element “A”.

Example:

In the above example <message></message> is parent element for <from></from>,<subject></subject> and <body></body> element.

XML Tree

XML Document gives way to represent information in a structured and self describing manner. When we closely analyze an XML Document it forms a tree structure.

Lets take a look on an example XML Document

The first line is the XML declaration. It defines the XML version and the character encoding used.

Next to it is a root element “inbox” of XML body, there is one child element and three sub child element inside it, and after all there is closing tag of root element.

The terms parent, child, and sibling are used to describe the relationships between elements.

 

XML Validity

An XML Document is said to valid if it is well formed and adhere the rules defined in Document Type Definition (DTD).

A well formed XML Document must be syntactically correct:

  • XML documents must have a root element
  • XML elements must have a closing tag
  • XML tags are case sensitive
  • XML elements must be properly nested
  • XML attribute values must be quoted
  • XML elements should be closed in proper order

Lets take a look at Well formed XML Document

XML DTD

XML DTD is used to define the structure for an XML document. It defines the structure with a set of legal elements that can be used to create XML Document.

XML Namespace

XML Namespace helps us to prevent conflict that may occur due to the naming of the elements.

As XML allow us define our custom tags there is always the chances of having element name exactly same as other element in another XML document.This is okay if we are not using both documents together. But in case if we want to include the content of both documents, what happen? we’ll run into problem due to name conflict. Here we are having two different elements, with different use but both are having same name.

Lets take look at below XML document

And now if we want to include the above XML Document in below HTML Document

Now if we try to include above XML Document in the the above HTML documents we’ll run into a problem. Because they both have an element called body. One is the body of the message and other is the body of the HTML Document.
By creating a namespace for the XML document. We can solve this problem.

XML Namespace Example

Lets change above XML Document using namespace

By adding xmlns:{prefix} attribute to the root element and assigning it a unique value which is usually in the form of a Uniform Resource Identifier (URI). It defines a namespace.

Now in this way we have created a namespace and we have added ib as prefix to our XML Element names.

Now when we call both documents together XML processor will have two different body element.

 

XML Nesting

When an XML Element is defined inside another elements it is said to be nested inside another element and this process is known as “XML Element Nesting”. All the nested elements are child of the container tag (Parent Tag).

Example:

In the above example “from”,”subject” and “body” tags are nested inside the “message” tag which is itself nested inside root element “inbox”.

It is necessary to have all XML elements must be properly nested within each other and child element should be closed before parent element in specified order.

 

XML Prolog

XML Prolog

This is first and most important part of an XML Document. It tells the browser that this document is marked up in XML.The prolog contains an XML declaration, possibly followed by a document type declaration (DTD).

XML Declaration

When we look at the XML Document we notice first line as below –

It is used as the XML declaration it tell the browser that the document is written in XML and specifies which version of XML. It also specify the language of character encoding.

Although XML declaration is not necessary but it is best practice to include it in your XML documents.

Document Type Definition (DTD)

Document Type Declaration is used to specify the set of rules for current XML Document structure as per the requirement. Set of rules or instructions can be defined in a separate file and this file can be used to validate XML document in which it is included and application stops to process the XML Document further if the document doesn’t adhere to the DTD.

Example

XML Comment

XML comments begin with <!-- and ends with -->. Like any other programming language XML Comments is used to add annotations or remarks to enhance the readability of the XML Code for the programmers. Comments can be placed anywhere in an XML document.

Example:

Commenting XML Element:

Sometimes we may want to temporarily remove some XML code in our XML document. XML Comments allow us to do this easily.

Example:

 

XML Attribute

XML Elements can have an optional list of attributes like HTML elements that provides the additional information about the element.Attributes is made of key / value pair separated by the equals (=) sign.Attributes is placed in opening tag and attribute values must always be quoted. Either single or double quotes can be used.

Syntax:

Example:

In the above example “msgid” is used as attribute.

 

XML Entity

We are familiar to HTML Entities. HTML Entity is symbolic representation of the information.

Lest take look at some HTML Entity –

  • &copy; = ©
  • &lt; = <
  • &amp; = &
  • &quot; = “

XML also supports the entity , An entity in XML is represented using an ampersand (&), followed by the name of the symbol, and ends with a semicolon.

Example:

Creating an XML Entity

We can create our own custom entity, by defining it in DTD. Once we defined it we can use it in our XML document.

Syntax:

Here is syntax to define entity in DTD

Using Your Custom Entity

Once we have created XML entity we can use it in following way, let take look in below example code