XML Data in MS SQL Server: Query and Modification
XML Data in MS SQL Server: Query and Modification
Publishing
FORXML Queries
Approaches for XML integration to DB
Mid-tier
Bi-directional XML view
Query view using XPath
Server-side
rowset-to-XML aggregator for XML Publishing - FOR XML
Query-driven shredding mechanism- Open XML
SQL SERVER 2005 XML Architecture
XML Storage - Native
Checks Well Formedness
Validation is optional
XML Documents or Fragments
XML Storage - DB
SQL BLOB
Allows for utilization BLOB optimizations
Streaming
Parsing
Compression
Unicode (UTF-16) Strings - UNTYPED
Requires Conversion
XML Schema - TYPED
Encode to match schema
Much more efficient
XML Storage - The Numbers
Advantages of Binary Storage
20to 30% Size Reduction
Faster
Limitations
2gb of stored binary per instance
Hierarchy is limited to 128 Levels
Storage - Schema
Storage Optimization
Size
Processing
Metadata Entity
Certain Type are Format Constrained
i.e. Date must use ISO 8601 format
Uses the XML Infoset
Validation - Schema Collection
CREATE XML SCHEMA COLLECTION myCollection AS
'<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://myBooks"
elementFormDefault="qualified"
targetNamespace="http://myBooks">
<xsd:element name="bookstore" type="bookstoreType" />
<xsd:complexType name="bookstoreType">
<xsd:sequence maxOccurs="unbounded">
<xsd:element name="book" type="bookType" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="authorName">
<xsd:sequence>
<xsd:element name="first-name" type="xsd:string" />
<xsd:element name="last-name" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>'
Validation - How?
At Data Entry
Validness checked when typed data is
inserted
Checking Data
Applied using Cast
Updated Schema
Does not Require re-validation
XML DT Methods
All evaluate an XQuery
Methods
query() - returns XML output
exists() - checks if expression results in nodes
nodes() - returns XML DT values
value() - returns value as SQL DT
modify() - modify XML data
query() & nodes() methods
Both take XQuery string
query() returns list of untyped XML nodes
Can be converted to strings
For SELECT output
Title
Design Patterns
Example: value() w/ nodes()
SELECT book.value(‘(title)[1]’,
‘NVARCHAR(255)’) AS Title
FROM Test CROSS APPLY
data.nodes(‘/bibliograph/book’) AS R(book)
Title
Design Patterns
All about XML
…
Example: exist()
SELECT book.value(‘(title)[1]’,
‘NVARCHAR(255)’) AS Title
FROM Test CROSS APPLY
data.nodes(‘/bibliograph/book’) AS R(book)
WHERE data.exist(‘/bibliograph/book’) = 1
Title
Design Patterns
All about XML
…
modify() method
Uses extended XQuery
insert, delete, and replace keywords
Used in SQL UPDATEs
Example: modify()
UPDATE docs SET xCol.modify(‘
insert
<section num="2">
<title>Background</title>
</section>
after (/doc//section[@num=1])[1]')
Example 2: modify()
UPDATE XmlCatalog
SET Document.modify ('
declare namespace bk = "http://myBooks";
replace value of (/bk:bookstore/bk:book [@ISBN="1-861003-11-
0"]/bk:price)[1] with 49.99')
XML Publishing
select CustomerID as "@CustomerID",
City as "address/city",
PostalCode as "address/zip",
ContactName as "contact/name",
Phone as "contact/phone",
from Customers
for xml path('Customer'), root('Doc')
Additional Papers Used
XML Support in Microsoft SQL Server 2005
Shankar Pal, Mark Fussell, and Irwin Dolobowsk
http://msdn2.microsoft.com/en-us/library/ms345117.aspx
XML Best Practices for Microsoft SQL Server 2005
Shankar Pal, Vishesh Parikh, Vasili Zolotov, Leo Giakoumakis,
Michael Rys
http://msdn2.microsoft.com/en-
us/library/ms345115(d=printer).aspx
Questions