You would have surely heard about XML which is been getting a significant increase in the popularity and usage of XML in the past few years.
In-fact almost all the modern applications seem to be touching XML in one way or the other. They would either generate XML for consumption by other applications or components, or they would either consume the XML produced by the others.
For e.g., .NET applications use XML files to store configuration information. ASP.NET web pages are XML documents, and almost all modern websites generate and publish information as XML feeds.
The XML data type supports a number of methods that allow various operations on the XML document. The most common operations needed on an XML document might be reading values from elements or attributes, querying for specific information, or modifying the document by inserting, updating, or deleting XML elements or attributes.
The several methods for the XML Data Type methods are as follows:
We will be going on dealing with each of them, one by one:
- XPath is used for locating XML elements and attributes within an XML document and locating through the XML tree.
- Every element and attribute within an XML document has a unique “path”.
- For e.g.,
- So, the path for the first element is “/Students/S_Id” and for the second is “/Students/S_Id”.
- Each element and attribute within an XML document can be uniquely identified and processed using an XPath expression.
- It is being used to retrieve scalar values from an XML document as a relational column.
- It takes an XQuery expression and evaluates it to a single node, casts results to the specified SQL Server data type, and so it returns the value.
- For e.g. , this is a query to find out the S_Id and Grade of a Student.
DECLARE @a XML
SELECT @a = '<Student S_Id = "1" Grade = "A1" />'
@a.value('(Student/@S_Id)' , 'INT') AS S_Id,
@a.value('(Student/@Grade)' , 'CHAR(2)') AS Grade
The result can be seen as:
- The Value () method accepts an XPath expression pointing to the element or attribute to read data from.
- It also specifies the data type of the result column.
Well, this was enough for this article-post. In the next article-post, will like to deal with the rest!!
So be tuned!!
Hope you got it understood well
And also comments on this!!