Attribute centric and Element centric XML

2
849

This post is composed of 2 main keywords. One is XML and other is your favorite SQL Server. In this article, you will learn how to read XML form of data in SQL server and then convert SQL Server data into XML form.  Before starting with my technical explanation on this topic, I would like to make you aware that XML data can be of three forms. These 3 XML forms are specified below –

  1. Element centric XML.
  2. Attribute centric XML.
  3. Combination of both Attribute Centric XML and Element Centric XML.

1st – Element Centric XML

In an Element centric XML, an element contains the child elements that denote the properties of that element. In this Element centric XML,

  • Parent element – <Product>,
  • Its sub element – <Details> and all the columns can be seen as a node.

Below XML format is in Element centric form.
Element Centric XML by PhpRing Tutorials
A. Generate Element centric XML – I am using tables of ‘AdventureWorksDW ‘database to explain below example. I really like AdventureWorks database because they have made it in such a way that we can try any example using this. The below query will generate the result in element centric XML format.

Result of above SQL query – Element centric XML

NOTE :- The above XML query is only a part of complete total result.

B. Reading Element centric XML query in SQL – The below example will show you how to read an element centric XML result of the above query in SQL again.

SQL Query –

Result of the above query –
Reading Element centric XML query in SQL by PhpRing Tutorials

2nd – Attribute centric XML

In an Attribute centric XML, an element contains the child element and presents the same as attribute of that parent element. You can see the below XML format which is in Attribute centric form.

In this Element centric XML,

  • Parent element – <Product>,
  • Its sub element – <Details>

This child element <Details> will contain all the columns and their values, particularly a one complete row. Then for a second row will have again child element with same column but different values that becomes your another row. And goes on…
A. Generate Attribute-centric XML – Query used in Element centric is also used here for generating attribute centric XML query.

Result of above SQL query – Attribute centric XML

NOTE :- The above XML query is only a part of complete total result.
B. Reading Attribute centric XML query in SQL – The below example will show you how to read an Attribute centric XML result of the above query in SQL again.
SQL Query –

Result of the above query –
Reading Attribute centric XML query in SQL by PhpRing Tutorials

3rd – Generate a Combination of Attribute centric and Element centric XML

The below query will generate combination of both Attribute centric and Element centric XML query.

Result of the above Query – 

We reached to an end of our post on Attribute centric XML and Element centric XML. I hope you all must have enjoyed this techno walk. Your queries and feedback will be highly appreciated. Stay tune to PhpRing Tutorials for more.

VN:F [1.9.22_1171]
Your Feedback hels to Improve
Rating: 10.0/10 (7 votes cast)
VN:F [1.9.22_1171]
Rating: +8 (from 8 votes)
Attribute centric and Element centric XML, 10.0 out of 10 based on 7 ratings
  • Nice work Syed. Great job!

    This is the new thing I learnt from your tip.

    Keep rocking.
    Thanks,
    Avi

    • Its my pleasure to help every one. 🙂