Hello Folks,
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:
- XPath
- value()
- nodes()
- exist()
- query()
- modify()
We will be going on dealing with each of them, one by one:
XPath:
- 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.,
<Students>
<S_Id>01</S_Id>
<S_Id>02</S_Id>
</Students>
- So, the path for the first element is “/Students/S_Id[1]” and for the second is “/Students/S_Id[2]”.
- Each element and attribute within an XML document can be uniquely identified and processed using an XPath expression.
Value ():
- 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" />' SELECT @a.value('(Student/@S_Id)[1]' , 'INT') AS S_Id, @a.value('(Student/@Grade)[1]' , '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!!
Regards
Piyush Bajaj
Like us on FaceBook | Follow us on Twitter | Join the fastest growing SQL Server group on FaceBook
Follow me on Twitter | Follow me on FaceBook