What Is XML? - SQL Server 2008

For those of you who have been living in a cave for the past ten years and haven’t heard the hype surrounding XML, it stands for eXtensible Markup Language. XML allows you to structure your data using a standardized schema. The standardization is the most important part, since that is the power of XML. Any other system can read or write data that adheres to the standard, with the usual caveats that different systems may interpret the data differently sometimes. XML also provides the ability for retrieving certain values from the XML using the XML Path Language (XPath) standard and transforming your XML using XML Stylesheet Language Transformations (XSLT). You’ll learn about XPath later in this chapter and XSLT in the next chapter.

One interesting discussion is deciding between XML and relational data. A lot of debate goes back and forth between the value of storing your data in a purely relational model, a hybrid XML/relational model, or a purely XML model. In our opinion, do whatever makes the most sense to the problem you are solving. If your expertise is in relational technology and you are getting great performance from your relational data, there is no need to switch to XML-based storage. You can easily expose your relational data as XML to the outside world, using FOR XML statements, and you can continue to store your data internally as relational data. Don’t fall prey to XML’s siren song without good reason to move to it. For example, XML is text based, so it is bigger than its binary equivalents. XML is verbose since it isn’t normalized like relational data, so you may have repeating sections of the same data in a single XMLdocument. Finally, XML has a different programming model than what a relational programmer is used to using.

Let’s start with some basic XML terminology. You’ll hear many people refer to documents, elements, and attributes. The easiest way to think about this is that an entire XML structure is the document, the document contains elements, and elements can contain attributes. The sample XML document here contains one document, three elements, and two attributes:

<?xml version="1.0"?>
<customer>
<name id="10">Tom Rizzo</name>
<state region="Northwest">WA</state>
</customer>

XML has schemas and namespaces. You are not required to put schemas on your XML, but schemas and namespaces help to uniquely define what is valid or invalid structure and data in your XML document. In the relational world, we have our table structures and constraints. You can map some of your relational concepts to XML schemas since XML schemas have datatypes and rules that control order, cardinality, and other aspects of the XML document. Schemas allow you to share your XML data with others but still have the other people understand your XML. An XML namespace is a collection of names, identified by a URI reference, that your element types and attribute names use in an XML document. Namespaces allow you to use the same names from different sources and avoid name collisions. For example, you can use the same element called customer from two different sources if you add namespaces that identify the elements as belonging to different namespaces. Schemas and namespaces will be important when we discuss the XML datatype in SQL Server and storing your XML natively in the database.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

SQL Server 2008 Topics