DATABASE

SQL Server 2008 : Explaining XML - Well-Formed XML

2/15/2011 7:49:31 PM
XML is a framework for storing information in a tree. The XML document must exactly have one root tag and must have a start-tag (< >) and end-tag (</>). An XML document must adhere to the XML syntax rules, or it is not a valid document. Figure 1 is a well-formed XML document.
Figure 1. A Well-Formed XML Document
<person>
<name>Steve Long</name>
</person>

In addition, text can be contained in the root tag. These are called attributes. For example, Figure 2 shows a sample XML document in which attributes are contained in the root tag.

Figure 2. Using Attributes in an XML Document
<car make="chevrolet">
<model>cavalier</model>
<year>2005</year>
</car>

XML requires that you properly nest elements in your XML document. Overlapping cannot occur, and as mentioned earlier, you need to end your XML with the end-tag (</>). Figure 3 is not a well-formed XML document. You will see that the <year> element overlaps the <car> element.

Figure 3. Not a Well-Formed XML Document
<car>hyundai<year>1997</year></car>

XML also has the flexibility to contain empty data. For example, in Figure 4, you will see that there is no price available for the car. An empty-element tag resembles a start-tag but contains a slash just before the closing angle bracket.

Figure 4. Empty Data Usage
<car make="chevrolet">
<model>cavalier</model>
<year>2005</year>
<price />
</car>

Notice that the price does not have an end-tag (</>). That’s because in XML there are different ways to specify the ending of a tag.

<price></price>
<price />
<price/>

XML documents must conform to syntax rules. Here are some of the rules to follow when you are creating a well-formed XML document.

  • All attribute values are quoted with either single (‘) or double (“) quotes. Single quotes close a single quote, and double quotes close a double quote.

  • Tags may be nested but must not overlap. Each nonroot element must be completely contained in another element.

  • Empty elements may be marked with an empty-element (self-closing) tag, such as <NoData/>. This is equal to <NoData></NoData>.

  • Element names are case-sensitive.

XML Examples

<person>
<name>Steve Long</name>
</person>

<car make="chevorlet">
<model>cavalier</model>
<year>2005</year>
<price/>
</car>

<root>
<customer id="17" firstname="Bob" lastname="Smith">
<address type="home" address1="763 Main Street" city="Anytown"
state="CA" zipcode="93762"/>
<order id="17">
<line_item part_no="12" qty="1" price="12.99"/>
<line_item part_no="73" qty="2" price="6.95"/>
<line_item part_no="17" qty="1" price="2.95"/>
</order>
</customer>
</root>
Other  
  •  SQL Server 2008 : Explaining XML - XML Schema
  •  SQL Azure : Tuning Techniques (part 5) - Provider Statistics & Application Design
  •  SQL Azure : Tuning Techniques (part 4) - Indexed Views & Stored Procedures
  •  SQL Azure : Tuning Techniques (part 3) - Indexing
  •  SQL Azure : Tuning Techniques (part 2) - Connection Pooling & Execution Plans
  •  SQL Azure : Tuning Techniques (part 1) - Dynamic Management Views
  •  Synching an On-Premises Database with SQL Azure
  •  SQL Server 2008 : Managing Query Performance - Finding Similar Queries
  •  SQL Server 2008 : Correlating Profiler and the Performance Monitor
  •  SQL Server 2008 : Explaining XML - XML Indexes
  •  SQL Server 2008 : Explaining XML - XQuery and XPath
  •  SQL Azure : Managing a Shard (part 2) - Working with Partial Shards
  •  SQL Server 2008 : OPENXML, sp_xml_preparedocument, and sp_xml_removedocument
  •  SQL Server 2008 : Retrieving and Transforming XML Data
  •  SQL Azure: Building a Shard (part 4) - Updating and Deleting Records in the Shard & Adding Records to the Shard
  •  SQL Azure: Building a Shard (part 3) - Reading Using the Shard & Caching
  •  SQL Azure: Building a Shard (part 2) - Managing Database Connections
  •  SQL Azure: Building a Shard (part 1) - Designing the Shard Library Object
  •  SQL Azure: Designing for High Performance - General Performance Concepts
  •  SQL Server 2008 : Explaining Advanced Query Techniques - Creating and Altering Tables
  •  
    Most View
    Secure Your Smartphone (Part 3)
    Red Redray Player - Dedicated 4K Player
    The Great Software Payola (Part 1)
    Silverlight : Print a Document
    The LTEdge (Part 1)
    Customizing the Windows Vista Interface : Customizing the Start Menu for Easier Program and Document Launching (part 2)
    Organize Videos In iPhoto
    The Reality Of Gaming Laptops (Part 2)
    UK tech skills crisis
    Rig Builder – May 2012 (Part 5)
    Top 10
    ADO.NET Programming : Microsoft SQL Server (part 4) - Working with Typed Data Sets
    ADO.NET Programming : Microsoft SQL Server (part 3) - Using Stored Procedures with DataSet Objects
    ADO.NET Programming : Microsoft SQL Server (part 2) - Using SQL Server Stored Procedures
    ADO.NET Programming : Microsoft SQL Server (part 1) - Connecting to SQL Server, Creating Command Objects
    Windows Phone 8 In-Depth Review (Part 6)
    Windows Phone 8 In-Depth Review (Part 5)
    Windows Phone 8 In-Depth Review (Part 4)
    Windows Phone 8 In-Depth Review (Part 3)
    Windows Phone 8 In-Depth Review (Part 2)
    Windows Phone 8 In-Depth Review (Part 1)