Escolar Documentos
Profissional Documentos
Cultura Documentos
0 Technical Articles
&;posing relational data as +ML Shredding +ML doc (ents into ro) sets Creating +ML vie)s .! (apping +ML sche(as to data.ase sche(as sing +ML64ata 7ed ced /+472 sche(as Creating < eries on +ML vie)s sing +9ath &;posing data in SQL Server thro gh =TT9
This s pport )as f rther enhanced in s .se< ent SQL+ML >e. releases. &nhance(ents incl de#
S pport for annotated +ML Sche(a definition lang age /+S42 to descri.e (appings /s pport for +47 still e;ists . t its se is not reco((ended2 Client6side '87 +ML SQL+ML Managed Classes S pport for >e. services
The Microsoft .%&T 'ra(e)or* :.0 has e;tensive s pport for reading- )riting- and processing +ML doc (ents. This s pport has .een f rther enhanced in the .%&T 'ra(e)or* 2.0 to i(prove the perfor(ance and sa.ilit! of vario s +ML classes. %e) classes provided .! the .%&T 'ra(e)or* in the S!ste(.+(l na(espace can .e sed to (ap +ML data to and fro( relational data. SQL+ML is a set of li.raries and technologies that ena.les the sea(less integration of relational data in SQL Server data.ases )ith +ML. 3t is a (iddle6tier co(ponent and does not incl de the server6 side +ML s pport provided .! '87 +ML and 89&%+ML. SQL+ML provides a sche(a6driven (apping approach to generating +ML fro( relational so rce data and loading +ML that represents relational infor(ation .ac* into relational ta.les. SQL+ML classes provide +ML s pport to SQL Server 2000 data.ases and a.ove. Microsoft SQL Server 200" adds . ilt6in s pport for +ML in the for( of the +ML data t!pe. +ML data can .e stored nativel! in +ML data t!pe col (ns. Additionall!- an +ML data t!pe col (n can .e f rther constrained .! associating an +ML sche(a collection )ith this col (n. +ML val es stored in +ML data t!pe col (ns can .e (anip lated )ith the help of +Q er! and an +ML 4ata Modification Lang age /4ML2. 3nde;es can .e . ilt on +ML data to i(prove < er! perfor(ance. ' rther(ore'87 +ML and 89&%+ML have .een enhanced to s pport the ne) +ML data t!pe. %e) feat res introd ced in SQL Server 200" to store and process +ML data co pled )ith +ML feat res offered in earlier versions of SQL Server provide developers )ith several )a!s in )hich +ML data can .e stored and processed in +ML applications. As there are ( ltiple )a!s in )hich +ML applications can .e . ilt sing the alternative approaches provided .! SQL Server 200"- it is i(portant to nderstand the scenarios- trade6offs- and s!nergies of the different technologies in order to (a*e the right choice. This article provides g idance in selecting the appropriate alternative for developing +ML applications )ith SQL Server 200".
XML or !usiness Integration: ? siness integration, also *no)n as enterprise application integration /&A32- incl des A2A /application6to6application2- ?2? /. siness6to6. siness2- and ?2C /. siness6to6cons (er2 applications. Applications )or*ing on disparate s!ste(s co(( nicate )ith each other sing +ML6.ased (essages.
XML or Content Management: Content (anage(ent s!ste(s .ased on +ML ena.le sers to store- retrieve- (odif!- and < er! +ML doc (ents. These s!ste(s store +ML doc (ents in their native for(at.
%e;t a fe) scenarios that fall nder the previo sl! (entioned categories are disc ssed. The sol tions for these scenarios are presented in s .se< ent sections )ith detailed treat(ents of the different +ML options availa.le in the SQL Server 200",$is al St dio 200" environ(ent.
An a to ins rance co(pan! providing services on the 3nternet allo)s its ins rance . !ers or agents to enter ins rance clai(s thro gh the co(pan!@s >e. site. These clai(s )ill .e processed .! the centraliAed s!ste( located at the corporate head < arters. 8nce the processing is co(plete- the s!ste( is re< ired to store specific infor(ation related to the clai( in a specified +ML for(at. &;act copies of these +ML doc (ents ( st .e (aintained in the s!ste( for legal p rposes. This scenario sho)s the se of +ML for content (anage(ent.
Scenario #: Data $%change !et&een 'utomo(ile Manu acturer and )arts Suppliers I
An a to(o.ile (an fact rer interacts )ith ( ltiple parts s ppliers to proc re the parts re< ired for the co(pan!. C rrentl! the (an fact rer receives invoices fro( s ppliers. The data corresponding to these invoices is then (an all! fed to a legac! invoice processing s!ste(. The invoice processing s!ste( stores the data in relational for(at. The (an fact rer no) )ants to a to(ate the invoice data feeding process to the legac! invoice processing s!ste(. This scenario is an e;a(ple of the se of +ML for . siness integration.
Scenario *: Data $%change !et&een 'utomo(ile Manu acturer and )arts Suppliers II
This scenario involves an a to(o.ile (an fact rer that interacts )ith ( ltiple parts s ppliers as (entioned in the previo s scenario. The (an fact rer@s present s!ste( does not provide the facilit! for the s ppliers to chec* the stat s of an invoice or to get a cop! of pa!(ent instr ctions fro( the (an fact rer. C rrentl! this infor(ation is availa.le to the s ppliers onl! over the phone. The a to(o.ile (an fact rer needs to .e a.le to e;pose this infor(ation over the >e. so that the s ppliers can perfor( these tas*s a to(aticall!. This scenario de(onstrates the se of +ML for . siness integration.
System.Xml.Schema C Contains classes to deal )ith +ML Sche(as definition lang age /+S42 sche(as.
System.Xml.Seriali4ation C 9rovides classes for the serialiAation of o.Bects into +ML for(at doc (ents or strea(s. System.Xml.X)ath C 3ncl des classes for navigating thro gh +ML doc (ents sing +9ath e;pressions. System.Xml.Xsl C =as classes for perfor(ing &;tensi.le St!lesheet Transfor(ations /+SLT2.
+ML Sche(a validation s pport has .een added to the XmlDocument class. The Xml1eader and Xml2riter classes have .een enhanced to provide significant perfor(ance i(prove(ents and to s pport +ML sche(a t!pes. Also- static Create (ethods have .een added to provide an easier )a! to create instances of Xml1eader and Xml2riter sing an Xml1eaderSettings and an Xml2riterSettings class to config re the t!pe created.
'or (ore infor(ation on enhance(ents in S!ste(.+(l see the >hat@s %e) in S!ste(.+(l for $is al St dio 200" and the .%&T 'ra(e)or* 2.0 7elease D http#,,technet.(icrosoft.co(,en6 s,li.rar!,aa1EF""E/printer2.asp; G )hite paper. The classes in the S!ste(.+(l na(espace can .e sed to i(ple(ent c sto( +ML parsing(anip lation- and storage logic. Leveraging the co((on lang age r nti(e /CL72 hosting capa.ilit! of SQL Server 200" and sing +ML classes in $is al St dio 200"- +ML processing can .e perfor(ed either in the (iddle tier or in the data.ase tier. 5se of the .%&T 'ra(e)or* +ML classes involves storing +ML doc (ents as a col (n of t!pe 5n6-archar7ma%8 or -ar(inary7ma%8 in a data.ase- or as files on the file s!ste(- and processing these doc (ents in the (iddle tier or in the data.ase sing classes in the S!ste(.+(l na(espace. +ML classes in the .%&T 'ra(e)or* can also .e sed to operate on data stored in +ML data t!pe. The .%&T 'ra(e)or* +ML classes are a s ita.le choice if#
Ho )ant to get access to all of the .%&T 'ra(e)or* +ML f nctionalit!- s ch as strea(ing parsers- doc (ent t!pe definition /4T42 and +S4 validation- +SLT processing- etc. Ho )ant to se SQL Server (erel! as a data store for +ML doc (ents and do not re< ire fine6grained access inside the data.ase. Ho do (ost or all processing on the +ML doc (ent sing the .%&T 'ra(e)or* +ML classes and perfor( pdates at the doc (ent level.
Ho can se 5n6-archar7ma%8- -ar(inary7ma%8- or the +ML data t!pe to store +ML. 3f !o se 5n6-archar7ma%8 or -ar(inary7ma%8 !o get the follo)ing .enefits#
Ho preserve te;t al fidelit! for +ML doc (ents )ith an e;act cop! of the doc (ent incl ding )hite space and for(atting. Ho r application gets the fastest possi.le perfor(ance for insert and retrieval operations on the doc (ent as a )hole.
The .enefits of sing the +ML data t!pe are given in a later section. )er orming XML )rocessing in the Middle Tier +ML processing can .e perfor(ed in the (iddle tier sing the vario s +ML classes provided .! the .%&T 'ra(e)or*. As (entioned earlier- )hen this approach is adopted- +ML doc (ents can .e stored as a col (n of t!pe 5n6-archar7ma%8 or +ML in a data.ase or as files on the file s!ste(. 3n the (iddle tier- these doc (ents are retrieved fro( the data.ase and processed according to the re< ire(ents of the ser as follo)s#
3f !o need to read an +ML doc (ent- se an Xml1eader created via the Xml1eader.Create78 (ethod to load the doc (ent o.tained fro( the data.ase. %avigate thro gh the doc (ent sing 1ead78. The Xml1eader class provides fastest possi.le read6 onl!- for)ard6onl!- non6cached access to the +ML doc (ent.
3f !o need )rite access to an +ML doc (ent and re< ire f ll navigational access to +ML data- se the XmlDocument class to load and access the +ML doc (ent. XmlDocument is an i(ple(entation of the 4oc (ent 8.Bect Model /48M2 in the .%&T 'ra(e)or*- )hich is an in6(e(or! tree representation of an +ML doc (ent that ena.les navigation and editing of this doc (ent.
3f !o need to validate an +ML doc (ent .ased on 4T4,+S4 or to o.tain +S4 infor(ation at r nti(e- se the Xml1eader class. Create (ethods )ith either +sd$alidation or 4T4$alidation set to tr e on the Xml1eaderSettings class. A 3alidation$-ent9andle78 event handler can .e also set to handle validation errors enco ntered d ring reading.
3f !o need to appl! an +SL transfor(ation to an +ML doc (ent- se the X)athDocument class to load the +ML doc (ent and appl! the transfor(ation sing the XslCommand class. The X)athDocument class provides a fast- high6perfor(ance cache for +ML doc (ent processing sing +SLT.
3f !o need to < er! an +ML doc (ent sing +9ath e;pressions- load the +ML doc (ent sing either X)athDocument /read6onl!2 or XmlDocument /read,)rite2. Create an instance of an X)ath.a-igator sing the Create.a-igator78 (ethod- and pass the re< ired +9ath e;pression as an arg (ent to the Select78 (ethod of X)ath.a-igator.
)er orming XML )rocessing in the Data(ase The integration of SQL Server 200" )ith CL7 ena.les developers to se the +ML classes provided .! the .%&T 'ra(e)or* to perfor( processing in the data.ase tier as )ell. This integration offers the capa.ilit! of )riting stored proced res- f nctions- triggers- and ser6defined t!pes in an! lang age s pported .! the .%&T 'ra(e)or*. ' rther(ore- CL7 hosting also provides access to the co(plete .%&T 'ra(e)or* .ase class li.rar!. As a res lt- the different +ML processing options descri.ed in the previo s section can also .e perfor(ed in the data.ase. The advantages of sing CL7 integration are as follo)s#
3t provides the a.ilit! to se o.Bect6oriented lang ages li*e CI and $is al ?asic .%&T for a thoring data.ase o.Bects in (anaged code. Managed data.ase o.Bects are (ore sec re than the e;tended stored proced res availa.le in earlier versions of SQL Server. 3t provides the a.ilit! to define ser6defined data t!pes and ser6defined aggregates. 5nder certain conditions- co(piled (anaged data.ase o.Bects deliver i(proved perfor(ance over Transact6SQL.
3n SQL Server 200"- data.ase developers have t)o options for )riting stored proced res- triggersand ser6defined f nctions. The! are Transact6SQL and an! lang age availa.le in the .%&T 'ra(e)or*- s ch as CI or $is al ?asic .%&T. The choice of the lang age depends on the t!pe of operations that are perfor(ed on the data. Transact6SQL is .est s ited )hen the code )ill perfor( (ostl! data access )ith little or no proced ral logic. Managed classes are .est s ited for (athe(aticall! intensive f nctions and proced res s ch as string handling- date operations- s!ste( reso rce access- file access- or i(age processing. The steps involved in sing the +ML classes of the .%&T 'ra(e)or* in the data.ase tier are as follo)s#
De-eloping a managed assem(ly. 5sing an! lang age availa.le in the .%&T 'ra(e)or*i(ple(ent processing f nctionalit! as an asse(.l! and pac*age this asse(.l! as a 4LL. Asse(.lies can also refer to other asse(.lies.
1egistering an assem(ly and granting permissions. Asse(.lies developed sing the .%&T 'ra(e)or* can .e registered )ith SQL Server .! sing the C7&AT& ASS&M?LH T6SQL state(ent. >hile registering an asse(.l!- it is also possi.le to specif! the code access per(issions granted to the asse(.l!. An asse(.l! can .e nregistered sing the 4789 ASS&M?LH T6SQL state(ent.
$%posing managed types to T:SQL. 9rocessing f nctionalit! offered .! an asse(.l! can .e e;posed to T6SQL via scalar6val ed ser6defined f nctions- ta.le6val ed ser6defined f nctions- ser6defined proced res /549s2- or ser6defined triggers. Scalar ser6defined f nctions can .e sed in an! scalar e;pression. Ta.le6val ed ser6defined f nctions can .e sed in an! '78M cla se. 549s can .e invo*ed in an &+&C state(ent.
'naly4ing Scenarios
3ns rance clai(s contain data6centric infor(ation s ch as Clai( 34- 9olic! % (.er- Clai( Settle(ent 4ata- etc.- and doc (ent6centric infor(ation s ch as a description of the accident da(age. +ML doc (ents are e;cellent at aggregating data6centric and doc (ent6centric infor(ation. The *e! re< ire(ent in the present scenario /see the Scenario :# 3ns rance Clai(s section2 is the need to (aintain an e;act cop! of ins rance clai(s in +ML for(at. >ith SQL Serverthis re< ire(ent can .e (et easil! .! storing the ins rance clai(s as a col (n of t!pe 5n6-archar7ma%8 or -ar(inary7ma%8 in the data.ase. 3t is i(portant to note that the +ML data t!pe sho ld not .e sed to store the doc (ents if infor(ation s ch as insignificant )hite spacesorder of attri. tes- na(espace prefi;es- and the +ML declaration need to .e preserved.
!ene its
The .enefits of sing 5n6-archar7ma%8 or -ar(inary7ma%8 as a storage (edi ( and classes fro( the S!ste(.+(l na(espace for the (anip lation of +ML doc (ents are as follo)s#
3t is fle;i.le )hen !o need to (a*e changes to the sche(a of an +ML doc (ent. 3t is also sef l )hen !o )ant to store +ML doc (ents )ith different sche(as in the sa(e col (n. 3t provides te;t al fidelit! for +ML doc (ents )hen 5n6-archar7ma%8 or -ar(inary7ma%8 is sed to store +ML. This (a! .e a re< ire(ent for applications that deal )ith legal doc (ents s ch as ins rance clai(s.
Ho can ta*e advantage of data.ase feat res s ch as transactional pdates- conc rrent access- .ac* p- replication- etc.- co(pared to storing +ML instances as files in a file s!ste(. As this approach does not depend on +ML s pport offered .! the data.ase- the application can easil! .e e;tended to s pport ( ltiple data.ase servers s ch as SQL Server- 8racle- etc. 3t can se the processing po)er of the client s!ste(- there.! red cing the load on the server. ?! perfor(ing C956intensive +ML processing on the (iddle tier- the server is relieved of so(e of the load and is availa.le for other i(portant tas*s.
3t offers the .est possi.le perfor(ance for doc (ent6level insertion and retrieval operations. Co(ple; operations s ch as +SL transfor(s can .e done in the data.ase as stored proced res- triggers- or f nctions.
Limitations
The li(itations of sing 5n6-archar7ma%8 or -ar(inary7ma%8 for storing and classes fro( the S!ste(.+(l na(espace for processing +ML instances can .e s ((ariAed as follo)s#
Coding co(ple;it! is high co(pared to the +ML data t!pe /see the +ML 4ata T!pe in SQL Server 200" section2 or the SQL+ML option /see the SQL+ML section2. &ven tho gh the data.ase logic is si(ple- i(ple(entation of the code to handle the parsing and processing of +ML in the (iddle tier or data.ase tier is co(plicated.
The a(o nt of code re< ired to i(ple(ent this sol tion is high. As a res lt- the cost of (aintenance is also high co(pared to SQL+ML option. Since the +ML doc (ent is stored as 5n6-archar7ma%8 in the data.ase- fine6grained pdates- inserts- or deletes on the +ML doc (ent are not possi.le. 3t offers li(ited < er!ing capa.ilities.
The siAe of an +ML doc (ent that can .e stored in 5n6-archar 7ma%8 data t!pe is li(ited to 2J?. 3t is ver! costl! to search a col (n of doc (ents stored this )a! .ased on the +ML content.
:. 2. 0. 1.
After processing the clai(- the application either approves or reBects the clai(. 5sing classes in the S!ste(.+(l na(espace- the +ML doc (ent for the clai( is generated. The generated +ML doc (ent is sent to a stored proced re. The stored proced re inserts the +ML doc (ent into the ta.le.
The follo)ing code e;a(ple generates an +ML doc (ent sing the clai( details availa.le )ith the s!ste( and inserts the +ML doc (ent into the data.ase. Cop! Code
using System; using System.Xml; using System.IO; using System.Text; using System.Data; using System.Data.SqlClient; namespace InsuranceClaim { class Insurance { static void Main string!" args# { Insurance.InsertInsuranceClaim #; $ static void InsertInsuranceClaim # { String%riter str%riter & null; Xml%riter 'riter & null; Xml%riterSettings settings & null; SqlConnection connection & null; SqlCommand command & null; try { str%riter & ne' String%riter #; settings & ne' Xml%riterSettings #; (()se indenting *or reada+ility. settings.Indent & true; settings.,ncoding & System.Text.,ncoding.)T-.; 'riter & Xml%riter.Create str%riter/ settings#; ((%rite t0e XM1 delcaration.
'riter.%riteStartDocument #; 'riter.%riteStart,lement 2InsuranceClaim2#; 'riter.%riteStart,lement 2ClaimIn*o2#; 'riter.%rite,lementString 2ClaimID2/ 2C34562#; 'riter.%rite,lementString 2ClaimType2/ 252#; 'riter.%riteStart,lement 2SettlementDetails2#; 'riter.%riteStart,lement 27aymentDetails2#; 'riter.%rite,lementString 27aidTo2/ 28e**2#; 'riter.%rite,lementString 29mount2/ 24:::2#; 'riter.%rite,lementString 2Date2/ 2:;(34(4::42#; 'riter.%rite,lementString 29pproved<y2/ 2Mi=e2#; 'riter.%rite,nd,lement #;((,nd o* 7aymentDetails 'riter.%rite,nd,lement #;((,nd o* SettlementDetails 'riter.%rite,nd,lement #;((,nd o* ClaimIn*o 'riter.%riteStart,lement 2Damage>eport2#; 'riter.%riteString 2Minor accident occured on 2#; 'riter.%rite,lementString 29ddress2/ 29<C Street/ Sample City/ Sample State2#; 'riter.%riteString 2 due to 2#; 'riter.%rite,lementString 2Cause2/ 2+ad 'eat0er2#; 'riter.%riteString 2 resulted in damage to 2#; 'riter.%rite,lementString 2DamagedItem2/ 2?ead 1ig0ts2#; 'riter.%rite,lementString 2DamagedItem2/ 2,ngine2#; 'riter.%rite,nd,lement #;((,nd o* Damage>eport 'riter.%rite,nd,lement #;((,nd o* InsuranceClaim 'riter.%rite,ndDocument #; ((%rite t0e XM1 to *ile and close t0e 'riter. 'riter.-lus0 #; connection & ne' SqlConnection #; connection.ConnectionString & @2server&local0ost; data+ase&9dventure%or=s;Integrated Security&SS7I;2; command & connection.CreateCommand #; command.CommandText & 2InsertInsuranceClaim2; command.CommandType & System.Data.CommandType.Stored7rocedure; command.7arameters.9dd 2@CustomerID2/ System.Data.SqlD+Type.C0ar#; command.7arameters.9dd 2@Claim2/
System.Data.SqlD+Type.AarC0ar#; String xml & str%riter.ToString #; string strCustomerID & 23::32; command.7arameters!:".Aalue & strCustomerID; command.7arameters!:".SiBe & strCustomerID.1engt0; command.7arameters!3".Aalue & xml; command.7arameters!3".SiBe & xml.1engt0; connection.Open #; command.,xecuteConDuery #; connection.Close #; $ *inally { i* connection.State && ConnectionState.Open# connection.Close #; i* 'riter E& null# 'riter.Close #; i* str%riter E& null# str%riter.Close #; $ $ $ $
=ere is the script that creates the data.ase ta.le# Cop! Code
C>,9T, T9<1, !InsuranceClaim" !CustomerID" !c0ar" 6# COT C)11/ !Claim" !varc0ar" max# COT C)11/ !Modi*iedDate" !datetime" COT C)11 D,-9)1T #
getdate ##
The follo)ing stored proced re is sed to insert the +ML doc (ent into the data.ase. Cop! Code
C>,9T, 7>OC,D)>, !d+o".!InsertInsuranceClaim" @CustomerID !c0ar" 6#/ @Claim !varc0ar" max# 9S <,FIC S,T COCO)CT OC; ICS,>T ICTO !InsuranceClaim" CustomerID/ Claim # A91),S @CustomerID/ @Claim #
,CD;
SQLXML
3ntrod ced )ith SQL Server 2000- SQL+ML enco(passes a )hole range of feat res related to +ML processing on the client side. 3t is a set of li.raries and technologies ena.ling the sea(less integration of relational data in SQL Server data.ases )ith +ML descri.ing relationall! str ct red data. 3n the pre6SQL 2000 da!s- the developer had to provide the code la!er for interaction .et)een the relational data and the data in +ML for(at. >ith the advent of SQL+ML- life )as (ade easier- for it provided the lin* .et)een relational data and +ML. The topic of disc ssion in this paper is confined to the SQL+ML Managed Classes. 'or (ore infor(ation on the applica.ilit! of other feat res of the li.rar!- see the SQL+ML D http#,,technet.(icrosoft.co(,en6 s,li.rar!,aa2FE"27/printer2.asp; G page on MS4%. SQL+ML co(prises (an! of the feat res introd ced in SQL Server to s pport +ML. These feat res are#
A.ilit! to transfor( the res lt of a < er! into +ML on the client side A.ilit! to create an +ML vie) of relational data .! sing an annotated +S4 (apping sche(a file- )hich allo)s !o to 4efine an +9ath < er! against the +ML vie) 9erfor( an insert- pdate- or delete on data in the data.ase sing +ML te(plates *no)n as pdategra(s 9erfor( +ML . l* load operations A.ilit! to access SQL Server sing =TT9- )hich ena.les !o to# Specif! SQL state(ents in the 57L Specif! te(plate < eries in the 57L Specif! te(plate files in the 57L Specif! +9ath < eries )ritten against the annotated +S4 (apping sche(a files in the 57L
A.ilit! to e;pose f nctionalit! offered .! stored proced res- ser6defined f nctions- and te(plate < eries as S8A96.ased >e. services A.ilit! to )rite code in the .%&T 'ra(e)or* to ta*e advantage of the +ML feat res offered .! SQL+ML sing SQL+ML Managed Classes
Client:side XML ormatting. Specif!ing a '87 +ML cla se on the client side ca ses the (iddle tier to perfor( the '87 +ML transfor(ation on the ro)set ret rned .! the server in response to a < er!. To perfor( +ML for(atting on the client side#
Set the ClientSideXml propert! of the S;lXmlCommand o.Bect to tr e if !o are sing SQL+ML Managed Classes. Set the ClientSideXML provider6specific propert! to Tr e if !o are sing SQL+ML8L&4? 9rovider. Specif! client:side:%ml<="= in the te(plate- if !o are sing te(plate < eries.
Select the 1un on the Client option on the virt al director! in the Settings ta. if !o are accessing SQL Server sing =TT9. The +ML for(atting (odes that are valid )ith '87 +ML on the client side are 7A>%&ST&4- and &+9L3C3T. >hen 7A> (ode is sed- the res lting +ML doc (ent contains an +ML ele(ent for each ro) in the < er! res lt and an attri. te corresponding to each col (n in a ro). >hen %&ST&4 (ode is specified- the .ase ta.le na(es are ret rned as the ele(ent na(es of the res lting +ML doc (ent. The &+9L3C3T (ode generates +ML doc (ents of an! shape .! allo)ing !o to specif! the for(at of the desired +ML in the < er! itself.
XML -ie&s. +ML vie)s are created .! sing annotated +S4 sche(as that define the (appings .et)een relational and +ML data. These +ML vie)s can .e < eried sing +9ath < eries. The relational data e;posed thro gh +ML vie)s can also .e (odified and the (odifications can then .e s .(itted to the data.ase sing pdategra(s. ' rther(ore- +ML vie)s are also sef l for inserting h ge +ML doc (ents into the data.ase )ith the help of the C8M6.ased XML !ul0 Load o.Bect.
9TT) access to SQL Ser-er. SQL+ML provides a tilit! *no)n as the 33S $irt al 4irector! Manage(ent tilit!- )hich can .e sed to set p an 33S virt al director! to e;pose the +ML feat res of an SQL Server via =TT9. S pport for specif!ing the SQL state(ents- stored proced res- te(plates < eries- te(plate files- and +9ath < eries directl! at the 57L is provided )ith the help of SQL 3SA93 e;tension.
2e( ser-ices support in SQLXML. S pport for e;posing the f nctionalit! of an SQL Server as a S8A96.ased >e. service )as added in SQL+ML 0.0. This feat re allo)s the SQL Server to receive S8A9 =TT9 re< ests fro( clients to e;ec te stored proced res- ser6defined f nctions- and te(plates.
SQLXML Managed Classes. Access to SQL+ML f nctionalit! in the .%&T 'ra(e)or* is provided thro gh SQL+ML Managed Classes. There are three (anaged classes in SQL+ML# S;lXmlCommand 6 handles the data.ase connectivit! and < er! e;ec tion aspects S;lXml)arameter 6 helps specif! the para(eters in < eries S;lXml'dapter 6 facilitates interaction )ith the dataset in the .%&T 'ra(e)or*
&;ec te an SQL < er! )ith '87 +ML cla se &;ec te an +9ath < er! against a (apping sche(a &;ec te a te(plate < er! &;ec te a te(plate < er! file &;ec te an pdategra( &;ec te a 4iffJra(
5sing SQL+ML to e;pose relational data as +ML doc (ents is a s ita.le choice if#
Ho r application receives +ML data- )hich is highl! str ct red and (aps )ell into relational ta.les. Ho r application ( st load h ge +ML doc (ents received fro( e;ternal applications into a data.ase and persist the( in relational for(at. Ho r application does not have a re< ire(ent to preserve the doc (ent order. Ho r application is re< ired to present the sa(e data to ( ltiple data cons (ers in different for(ats. 9erfor(ance of 4ML operations is critical for !o r application. Ho r application has a re< ire(ent to tiliAe the f ll potential of the opti(iAer for < er! opti(iAation. Ho r application perfor(s fine6grained data (anip lations. Ho r application is re< ired to e;pose e;isting relational data as +ML.
'naly4ing Scenarios
3n the first data e;change scenario descri.ed in the +ML sage scenarios /see the Scenario 2# 4ata &;change ?et)een A to(o.ile Man fact rer and 9arts S ppliers 3 section2- the interaction .et)een an a to(o.ile (an fact rer and ( ltiple parts s ppliers presents a classic case for sing SQL+ML. The (an fact rer has to co(( nicate )ith different s ppliers to e;change invoice data. The proposed sol tion ses a >e. service and SQL+ML to solve this pro.le(. The (an fact rer e;poses a >e. service that can .e sed .! the s ppliers to send invoices to the (an fact rer. The >e. service ses an +SLT that is specific to the c sto(er to transfor( the invoice fro( the s pplier for(at to a co((on for(at sed .! the (an fact rer. The >e. service then shreds the +ML doc (ent sing an +ML vie) that (aps the content of the invoice doc (ent to the col (ns of a relational ta.le. The legac! invoice processing s!ste( )ill .e a.le to pic* the data fro( the relational ta.le and proceed )ith processing. The advantages of sing +ML vie)s for this scenario are as follo)s#
Lo) (aintenance costs. An! changes (ade to the invoice sche(a .! the s pplier can .e acco((odated easil! .! (odif!ing the +SLT file that is specific to the s pplier. The coding co(ple;it! is less co(pared to '87 +ML /see the Server6Side S pport for 7elational,+ML 3ntegration /'87 +ML,89&%+ML2 section2. %e) s ppliers can .e s pported easil! .! creating an +SLT file specific to the s pplier.
!ene its
The advantages of sing SQL+ML are s ((ariAed as follo)s#
Creating an annotated (apping sche(a to (ap relational data to the +ML data is a relativel! si(ple and (ore (aintaina.le sol tion than the '87 +ML &+9L3C3T on the server side.
SQL+ML offers the a.ilit! to create pdata.le .i6directional +ML vie)s co(pared to read6 onl! +ML representations of relational data created sing '87 +ML. Mapping +S4 provides the facilit! for acco((odating the change re< ests in +ML for(at )itho t (aBor code changes. This provides ease for (aintenance.
SQL+ML provides the a.ilit! to perfor( +ML for(atting on the client side .! allo)ing the ser to set the ClientSideXML propert! to tr e for the S;lXMLCommand class and there.! red cing the load on the server.
Limitations
8n the do)nside- there are a fe) li(itations )hen SQL+ML is sed fro( the client side#
+ML vie)s are not s ita.le for cases )here the hierarch! of an +ML doc (ent is too deep or rec rsive of n*no)n depth. SQL+ML is not s ita.le for descriptive doc (ents containing (i;ed content (ar* p and ordered data s ch as prod ct catalogs- ne)s reports- etc. ?eca se the doc (ent order is not preserved- it is diffic lt to reconstr ct the original +ML doc (ent. >hile deco(posing +ML doc (ents into relational ta.les gives e;cellent searching perfor(ance- the transfor(ation to and fro( +ML can .e e;pensive. >hen defa lt (apping is sed in +S4 (apping sche(as- it can e;pose the data.ase ta.le na(e and col (n infor(ation- )hich can lead to nintentional infor(ation lea*age. This ris* can .e avoided .! specif!ing e;plicit (apping for ta.les and col (ns.
SQL state(ents in the 57L sho ld onl! .e sed in tr sted intranets. The se of s ch < eries on the 3nternet can lead to potential sec rit! ris*s.
parentI=ey&2CustomerID2 c0ild&2Sales.SalesOrder?eader2 c0ildI=ey&2CustomerID2 (J GsqlHrelations0ip name&2Order?eaderOrderDetail2 parent&2Sales.SalesOrder?eader2 parentI=ey&2SalesOrderID2 c0ild&2Sales.SalesOrderDetail2 c0ildI=ey&2SalesOrderID2 (J G(xsdHappin*oJ G(xsdHannotationJ GxsdHelement name&2Customer2 sqlHrelation&2Sales.Customer2 J GxsdHcomplexTypeJ GxsdHsequenceJ GxsdHelement name&2Order2 sqlHrelation&2Sales.SalesOrder?eader2 sqlHrelations0ip&2CustomerOrder?eader2 maxOccurs&2un+ounded2 J GxsdHcomplexTypeJ GxsdHsequenceJ GxsdHelement name&2OrderDetail2 sqlHrelation&2Sales.SalesOrderDetail2 sqlHrelations0ip&2Order?eaderOrderDetail2 maxOccurs&2un+ounded2 J GxsdHcomplexTypeJ GxsdHattri+ute name&2SalesOrderID2 type&2xsdHinteger2 (J GxsdHattri+ute name&27roductID2 type&2xsdHinteger2 (J GxsdHattri+ute name&2OrderDty2 type&2xsdHinteger2 (J G(xsdHcomplexTypeJ G(xsdHelementJ G(xsdHsequenceJ GxsdHattri+ute name&2SalesOrderID2 type&2xsdHinteger2 (J GxsdHattri+ute name&2CustomerID2 type&2xsdHinteger2 (J GxsdHattri+ute name&2OrderDate2 type&2xsdHdate2 (J GxsdHattri+ute name&2S0ipDate2 type&2xsdHdate2 (J G(xsdHcomplexTypeJ G(xsdHelementJ G(xsdHsequenceJ
GxsdHattri+ute name&2CustomerID2 type&2xsdHinteger2 (J G(xsdHcomplexTypeJ G(xsdHelementJ G(xsdHsc0emaJ class ,xportOrders { ((( GsummaryJ ((( ((( ((( ((( T0is met0od use SqlXmlCommand class to select t0e records *rom Sales.Customer/ Sales.SalesOrder?eader and Sales.SalesOrderDetail ta+les.T0e data is *etc0ed as suc0 *rom server and *ormatted into xml at client side. Cote t0at t0e ClientSideXml is set to true.
((( G(summaryJ static void Main string!" args# { i* { Console.%rite1ine 2)sage2#; Console.%rite1ine 2CustomerOrders GCustomerIDJ !OrderID"2#; return; $ args.1engt0 G 3#
try { String<uilder str<uilder & ne' String<uilder #; str<uilder.9ppend 2(Customer!@CustomerID&K2#; str<uilder.9ppend args!:"#; str<uilder.9ppend 2K"2#; i* { str<uilder.9ppend 2(Order!@SalesOrderID&K2#; str<uilder.9ppend args!3"#; str<uilder.9ppend 2K"2#; $ SqlXmlCommand xmlCommand & ne' SqlXmlCommand @27rovider& SD1O1,D<; Server&local0ost; data+ase&9dventure%or=s; Integrated Security&SS7I;2#; args.1engt0 J 3#
& true; & 2CustomerOrders2; & @2CustomerOrderDetails.xsd2; & SqlXmlCommandType.X7at0; & str<uilder.ToString #;
& xmlCommand.,xecuteStream
#;
-ileStream *sOut & -ile.Create 2CustomerOrder.xml2#; Stream%riter s' & ne' Stream%riter *sOut#; using { s'.%rite sr.>eadTo,nd ##; $ s'.-lus0 #; s'.Close #; *sOut.Close #; $ catc0 { Console.%rite1ine $ $ $
The (ethod provided a.ove e;ports the sales order details for a C sto(er 34 specified as a co((and line arg (ent to the application. The data is converted into +ML for(at on the client sidethere.! avoiding perfor(ance iss es on the server side. %ote that the annotated +S4 sche(a (apping provided a.ove has to .e saved as C sto(er8rder4etails.;sd for the a.ove code frag(ent to )or* properl!. .ote The code that goes into retrieving the data as +ML fro( the data.ase is (ini(al as can .e seen in the e;a(ple.
,xception exception#
exception.ToString # #;
/?1 XML
Server6side '87 +ML s pports fo r +ML transfor(ation (odesK7A>- A5T8- &+9L3C3T- and 9AT=. ?! defa lt- 7A> (ode (aps each ro) in the < er! res lt set to an +ML ele(ent and each col (n in the ro) to an attri. te. >hen the &L&M&%TS option is specified )ith 78> (ode- each col (n in a
ro) is (apped to a s .ele(ent of the ele(ent that is generated for the ro). 3t is also possi.le to re< est an inline sche(a for the +ML that is generated .! specif!ing +MLSC=&MA option. A5T8 (ode provides s pport for generating nested +ML ele(ents )here each ta.le in the '78M cla se- for )hich at least one col (n is listed in the S&L&CT cla se- is (apped to an +ML ele(ent and the col (ns listed in the S&L&CT cla se are (apped to attri. tes /or s .ele(ents if the &L&M&%TS option is specified2 .! defa lt. &+9L3C3T (ode provides the (a;i( ( control over the shape of the +ML that is generated fro( a < er! res lt. 3t allo)s !o to generate +ML )ith an! shape .! specif!ing the for(at for the desired +ML in the < er! itself. Co(posing co(ple; +ML doc (ents sing the &+9L3CT (ode is c (.erso(e. 5sing the 9AT= (ode together )ith the a.ilit! to )rite nested '87 +ML < eries and the TH9& directive to ret rn +ML t!pe instances provides an alternative to )riting co(ple; &+9L3C3T (ode < eries. The 9AT= (ode (aps the col (ns in the ro)set ret rned .! the S&L&CT < er! to attri. tes and s .ele(ents .! interpreting the col (n na(es as +9ath6li*e s!nta;. 'or (ore infor(ation on '87 +ML enhance(ents in SQL Server 200" see the >hat@s %e) in '87 +ML in Microsoft SQL Server 200" D http#,,technet.(icrosoft.co(,en6 s,li.rar!,(s01":07/printer2.asp; G )hite paper.
?)$.XML
89&%+ML- together )ith the sp@%ml@preparedocument and sp@%ml@remo-edocument s!ste( stored proced res- provides a relational ro)set vie) of an +ML doc (ent. 3n order to se 89&%+ML on an +ML doc (ent- sp@%ml@preparedocument ( st .e sed to create an in6(e(or! representation of the +ML doc (ent. This stored proced re parses the +ML doc (ent sing the MS+ML parser and ret rns a handle to the +ML doc (ent that can .e sed )ith 89&%+ML. %o)para(eters s ch as +ML doc (ent handle- ro)pattern- )hich is an +9ath e;pression that (aps nodes of +ML data to ro)s- ro)set sche(a- and (apping .et)een the ro)set col (ns and the +ML nodes can .e passed to 89&%+ML to o.tain the ro)set. The +ML doc (ent ( st .e nloaded fro( (e(or! sing the sp@%ml@remo-edocument stored proced re )hen it is no longer re< ired.
A.ilit! to t!pecast the res lts of '87 +ML sing the ne) TH9& directive A.ilit! to assign the res lts of '87 +ML to a varia.le of t!pe +ML A.ilit! to nest '87 +ML < eries to generate +ML hierarchies A.ilit! to se the ne) 9AT= (ode to generate co(ple; +ML doc (ents A.ilit! to generate an inline sche(a in +47 or +S4 for(at sing the +ML4ATA and +MLSC=&MA options- respectivel! A.ilit! to se the &L&M&%TS directive )ith 7A> (ode to generate ele(ent6centric +ML A.ilit! to se the +S3%3L option )ith the &L&M&%T directive to (ap %5LL val es to an ele(ent )ith an attri. te ;si#nilLMtr eM
$nhancements in ?)$.XML
3n SQL Server 200"- 89&%+ML has .een enhanced to s pport the follo)ing feat res#
5sing '87 +ML and 89&%+ML to co(pose and deco(pose +ML doc (ents is a s ita.le choice if#
Ho r application has a re< ire(ent to store data relationall! and e;pose this infor(ation to another application as +ML. Ho r application does not have the re< ire(ent to preserve the order of +ML. Ho r application perfor(s heav!6ele(entClevel 4ML operations. Ho r application re< ires fine6grained data access and pdate. Ho r application re< ires !o to e;pose the relational data thro gh a >e. service.
'naly4ing Scenarios
The re< ire(ent in scenario 0 /see the Scenario 0# 4ata &;change ?et)een A to(o.ile Man fact rer and 9arts S ppliers 33 section2 is to provide >e. services that s ppliers can se to o.tain the stat s of an invoice or get a cop! of pa!(ent instr ctions. '87 +ML in conB nction )ith a >e. service provides a sol tion that allo)s the (an fact rer to e;pose these services on the 3nternet. The s pplier ses a >e. service to en< ire a.o t the stat s of his invoice. The >e. service then ses the invoice 34 provided .! the s pplier and generates the response in +ML for(at fro( the relational data sing a '87 +ML state(ent. The generated +ML doc (ent )ill .e ret rned to the s pplier. 3n the c rrent scenario- sing the '87 +ML state(ent6.ased approach provides the follo)ing .enefits#
'87 +ML provides an eas! )a! to d!na(icall! co(pose si(ple +ML doc (ents fro( relational data. Maintaining a '87 +ML < er! is eas! co(pared to an +ML vie) )hen the! are sed to co(pose si(ple +ML doc (ents.
!ene its
So(e of the .enefits of sing '87 +ML,89&%+ML are as follo)s#
'87 +ML offers a si(ple )a! of generating +ML fro( relational data on the server. '87 +ML provides the a.ilit! to e;pose . siness infor(ation thro gh >e. services. 89&%+ML allo)s !o to pass a ro)set to a stored proced re in +ML for(at )hich ena.les !o to perfor( . l* 3%S&7T- 594AT&- and 4&L&T& operations )ith B st one net)or* ro ndtrip. '87 +ML together )ith +SL can .e sed for application integration or . siness integration.
Limitations
The li(itations of sing '87 +ML,89&%+ML for co(posing and deco(posing +ML doc (ents are#
Shaping +ML str ct res sing '87 +ML )ith the &+9L3CT option is a diffic lt tas*. 3t is diffic lt to (aintain co(ple; < eries )ritten sing '87 +ML &+9L3C3T.
+ML doc (ents generated .! '87 +ML A5T8 can e;pose the data.ase ta.le na(e and col (n infor(ation leading to inadvertent infor(ation disclos re. This sit ation can .e avoided .! specif!ing aliases for ta.les and col (ns.
S,1,CT Cust.CustomerID/ Order?eader.CustomerID/ Order?eader.SalesOrderID/ Detail.SalesOrderID/ Detail.1ineCum+er/Detail.7roductID/ 7roduct.Came/ Detail.OrderDty ->OM Sales.Customer Cust/ Sales.SalesOrder?eader Order?eader/ Sales.SalesOrderDetail Detail/ 7roduction.7roduct 7roduct %?,>, Cust.CustomerID & Order?eader.CustomerID 9CD 9CD 9CD Order?eader.SalesOrderID & Detail.SalesOrderID Detail.7roductID & 7roduct.7roductID Cust.CustomerID <,T%,,C 66 9CD 6L#
GCust CustomerID&2662J GOrder?eader CustomerID&2662 SalesOrderID&2;5;N;2J GDetail SalesOrderID&2;5;N;2 1ineCum+er&242 7roductID&2O;42 OrderDty&242J G7roduct Came&2C0ain2 (J G(DetailJ GDetail SalesOrderID&2;5;N;2 1ineCum+er&232 7roductID&2OLO2 OrderDty&232J G7roduct Came&2TouringI3::: <lue/ L:2 (J
G(DetailJ GDetail SalesOrderID&2;5;N;2 1ineCum+er&252 7roductID&2ON42 OrderDty&232J G7roduct Came&2TouringI4::: <lue/ ;62 (J G(DetailJ G(Order?eaderJ GOrder?eader CustomerID&2662 SalesOrderID&2;O:462J
GDetail SalesOrderID&2;O:462 1ineCum+er&232 7roductID&2ON42 OrderDty&252J G7roduct Came&2TouringI4::: <lue/ ;62 (J G(DetailJ GDetail SalesOrderID&2;O:462 1ineCum+er&242 7roductID&2O;N2 OrderDty&242J G7roduct Came&2TouringI3::: Mello'/ L:2 (J G(DetailJ G(Order?eaderJ G(CustJ GCust CustomerID&26L2J GOrder?eader CustomerID&26L2 SalesOrderID&26.5;62J GDetail SalesOrderID&26.5;62 1ineCum+er&232 7roductID&2N5:2 OrderDty&232J G7roduct Came&211 >oad -rame I >ed/ L42 (J G(DetailJ G(Order?eaderJ G(CustJ
The follo)ing e;a(ple e;tracts order details specified in the +ML doc (ent sing 89&%+ML and +9ath e;pressions. $%ample: Using ?)$.XML Cop! Code
D,C19>, @XmlDocument?andle int D,C19>, @XmlDocument nvarc0ar max# S,T @XmlDocument & CKG>OOTJ GCust CustomerID&2662J GOrder?eader CustomerID&2662 SalesOrderID&2;5;N;2J GDetail SalesOrderID&2;5;N;2 1ineCum+er&242 7roductID&2O;42 OrderDty&242J G7roduct Came&2C0ain2 (J G(DetailJ GDetail SalesOrderID&2;5;N;2 1ineCum+er&232 7roductID&2OLO2 OrderDty&232J G7roduct Came&2TouringI3::: <lue/ L:2 (J
G(DetailJ GDetail SalesOrderID&2;5;N;2 1ineCum+er&252 7roductID&2ON42 OrderDty&232J G7roduct Came&2TouringI4::: <lue/ ;62 (J G(DetailJ G(Order?eaderJ GOrder?eader CustomerID&2662 SalesOrderID&2;O:462J GDetail SalesOrderID&2;O:462 1ineCum+er&232 7roductID&2ON42 OrderDty&252J G7roduct Came&2TouringI4::: <lue/ ;62 (J G(DetailJ GDetail SalesOrderID&2;O:462 1ineCum+er&242 7roductID&2O;N2 OrderDty&242J G7roduct Came&2TouringI3::: Mello'/ L:2 (J G(DetailJ G(Order?eaderJ G(CustJ GCust CustomerID&26L2J GOrder?eader CustomerID&26L2 SalesOrderID&26.5;62J GDetail SalesOrderID&26.5;62 1ineCum+er&232 7roductID&2N5:2 OrderDty&232J G7roduct Came&211 >oad -rame I >ed/ L42 (J G(DetailJ G(Order?eaderJ G(CustJ G(>OOTJK II Create an internal representation o* t0e XM1 document. ,X,C spPxmlPpreparedocument @XmlDocument?andle O)T7)T/ @XmlDocument II ,xecute a S,1,CT statement using O7,CXM1 ro'set provider. S,1,CT Q ->OM O7,CXM1 %IT? @XmlDocument?andle/ K(>OOT(Cust(Order?eader(DetailK/4# varc0ar 3:# K..(@CustomerIDK/ int int int int K..(@SalesOrderIDK/ K@1ineCum+erK/ K@7roductIDK/ K@OrderDtyK#
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII 66 66 66 66 66 6L ;5;N; ;5;N; ;5;N; ;O:46 ;O:46 6.5;6 4 3 5 3 4 3 O;4 OLO ON4 ON4 O;N N5: 4 3 3 5 4 3
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
Typed XML
T!ped +ML is ideal for sit ations )here !o have +ML sche(as descri.ing !o r +ML data. 3n s ch cases- !o can associate a collection of +ML sche(as )ith the +ML col (n to !ield t!ped +ML. $alidation on +ML t!pe col (ns is perfor(ed .ased on the +ML sche(a collection associated )ith the col (n. Additionall!- the perfor(ance of < eries that involve t!ped +ML data is .etter co(pared to nt!ped +ML data- as it re< ires no r nti(e conversion of node val es.
Untyped XML
5sing nt!ped +ML is s ita.le if !o have sche(as- . t !o do not )ant the server to validate the data- or if no sche(a is availa.le. Ho (a! )ant to store nt!ped +ML even )hen a sche(a is present if an application#
=as no fi;ed sche(a 9erfor(s the validation on the client side .efore storing the data at the server Te(poraril! stores +ML data that is invalid according to the sche(a 5ses sche(a co(ponents that are ns pported at the server /e.g.- *e!,*e!ref2
5nt!ped +ML doc (ents are chec*ed to ens re that the! are )ell for(ed- even tho gh the! are not associated )ith an! sche(a. 3t is i(portant to note that nt!ped +ML i(poses a perfor(ance penalt! d e to the r nti(e conversion of node val es- as the node val es are stored internall! as 5nicode strings.
Create a ta.le )ith one (ore col (ns of t!pe +ML in addition to the relational col (ns. Create a t!ped +ML col (n t!pe .! associating it )ith an +ML sche(a collection. Create a constraint on an +ML col (n involving other +ML or non6+ML t!pe col (ns to enforce . siness r les. Create a varia.le of t!pe +ML that can .e sed to store an instance of an +ML data t!pe. Create a para(eter of t!pe +ML to a stored proced re or ser6defined f nction. 7et rn an +ML t!pe val e fro( a ser6defined f nction. Assign the res lt of a '87 +ML < er! o.tained sing the ne) TH9& directive to a varia.le of t!pe +ML. 7 n a s .set of +Q er! to < er! into the +ML str ct re and transfor( +ML data. Create a co(p ted col (n .ased on a col (n of t!pe +ML. Create an +ML inde; on a col (n of t!pe +ML to i(prove the perfor(ance of < eries. 9erfor( ele(ent6level insert- delete- and pdate operations on +ML instances sing +ML 4ML. 9ass an instance of +ML t!pe data to sp@%ml@preparedocument to prepare an in6 (e(or! representation of the +ML doc (ent. >rite cross6do(ain < eries that )ill incl de .oth relational and +ML col (ns sing +Q er! and +ML 4ML. Cast or convert the +ML t!pe to -archar or n-archar t!pes sing CAST and C8%$&7Trespectivel!. Convert or cast string data t!pes s ch as 5n6-archar- 5n6te%t- -ar(inary- and image into +ML t!pe sing CAST or C8%$&7T.
XML Inde%ing
The parsing and shredding involved in < er! processing operations on +ML data t!pe col (ns can cons (e a prohi.itivel! long ti(e if the siAe of the +ML instance is ver! .ig. Q er! perfor(ance on the +ML data t!pe can .e i(proved .! creating inde;es on these col (ns. The siAe of the +ML data and sage scenario pla! an i(portant role in dictating the t!pe of inde; that is re< ired. SQL Server s pports t)o t!pes of inde;esKpri(ar! +ML inde;es and secondar! +ML inde;esN the latter cannot e;ist )itho t the for(er. The creation of a pri(ar! +ML inde; on an +ML col (n shreds the +ML ?L8? and stores these val es in an internal ta.le. This res lts in i(proved perfor(ance d ring < er! e;ec tion ti(e as there is no shredding involved at r nti(e. 4epending on the sage scenario- < er! perfor(ance can .e f rther i(proved .! creating secondar! +ML inde;es. Three t!pes of secondar! +ML inde;esK 9AT=- 9789&7TH- and $AL5&Kcan .e created to i(prove the perfor(ance of < eries .ased on path- propert!- and val e respectivel!. More infor(ation on selecting an appropriate secondar! inde; for an +ML t!pe col (n is availa.le in the 9erfor(ance 8pti(iAations for the +ML 4ata T!pe )hite paper. Storing +ML doc (ents as an +ML data t!pe is a s ita.le choice if#
Ho r application has a re< ire(ent to preserve the 3nfoset content of +ML instances. The 3nfoset content of an +ML doc (ent incl des doc (ent hierarch!- order of ele(ents- val es of ele(ents and attri. tes- and so on. 3nfor(ation s ch as order of attri. tes- prefi;es of na(espaces- insignificant )hite spaces- and +ML declaration is not preserved.
Ho r application re< ires ele(ent6level (odif! and < er! operations on +ML doc (ents. Ho r application needs inde;es on +ML data t!pe col (ns to speed p < er! processing. Ho r +ML data (a! or (a! not have a sche(a. Ho r application ses +ML doc (ents )ith a )ide range of str ct res- or +ML doc (ents confor(ing to different or co(ple; sche(as that are too hard to (ap to relational str ct res.
'naly4ing Scenarios
'naly4ing Scenario: Content Management System %o)- let s anal!Ae the content (anage(ent s!ste( descri.ed in the +ML sage scenarios /see the Scenario 1# Content Manage(ent S!ste( section2. 9 .lishing co(panies deal )ith infor(ation in vario s for(s incl ding te;t- i(ages- a dio- video- etc. ?loc*s of infor(ation that can .e sed independentl! are collected fro( vario s so rces and (aintained in a data.ase. These .loc*s of infor(ation are *no)n as components. ?! asse(.ling individ al co(ponents- doc (ents are created. >hich co(ponents are incl ded in a doc (ent depends on the re< ire(ents of the sers. These doc (ents )ill .e delivered to the s .scri.ed sers thro gh vario s channels. Content (anage(ent s!ste(s t!picall! have re< ire(ents s ch as the a.ilit! to store- retrieve- search- and pdate the content )ith high perfor(ance and scala.ilit!. +ML as a nified data (odel provides an attractive option for storing .oth +ML data and +ML content in the sa(e doc (ent. +ML also provides the a.ilit! to separate the presentation fro( the data itself- )hich is i(portant .eca se the presentation of the sa(e infor(ation to different sers (a! differ. The native +ML data t!pe offered .! SQL Server 200" can .e sed to (eet the re< ire(ents of this content (anage(ent s!ste(. >ith the +ML data t!pe- it is possi.le to store +ML doc (ents- (odif! +ML doc (ents at ele(ent6level sing +ML 4ML- and perfor( < eries on +ML doc (ents sing +Q er!. 'naly4ing Scenario: Customer Sur-ey
3n the c sto(er s rve! +ML sage scenarios /see Scenario "# C sto(er S rve! section2- the *e! re< ire(ent is the a.ilit! to store the s rve! infor(ation )ith ( ltiple sche(as. 4ata that does not have a fi;ed sche(a cannot .e (odeled sing a single relational ta.le. A relational ta.le )ith an +ML col (n provides an e;cellent option for storing s ch infor(ation. An additional col (n can .e added to the relational ta.le to store the t!pe of s rve!. 3nfor(ation corresponding to a partic lar t!pe of s rve! can .e anal!Aed .! fetching all records for the s rve! t!pe sing the s rve! t!pe col (n. 3n a t!pical s rve!- c sto(ers s all! don@t ans)er all the < estions. Therefore- instead of creating several col (ns /one for each < estion in the s rve!2 and storing %5LL val es in the data.ase for nans)ered < estions- storing the per6c sto(er s rve! infor(ation as an +ML doc (ent in one col (n )o ld .e .eneficial. Storing the c sto(er s rve! infor(ation as an +ML t!pe col (n is (ore appropriate for this scenario .eca se#
5sing +ML t!pe col (ns allo)s the s rve! infor(ation )ith different sche(as to .e stored in a single +ML t!pe col (n. Associating the +ML t!pe col (n )ith an +ML sche(a collection allo)s the ser to store data for ( ltiple s rve! t!pes.
$alidation of s rve! infor(ation can .e perfor(ed .! the ser interface and there is no need to store this as a t!ped +ML data t!pe col (n to enforce validation at the data.ase level. +Q er! can .e sed to perfor( anal!sis on the data.
!ene its
The .enefits of storing +ML data in an +ML data t!pe col (n can .e s ((ariAed as follo)s#
The +ML data t!pe offers a si(ple and straightfor)ard )a! of storing !o r +ML data at the server )hile preserving doc (ent order and doc (ent str ct re. This is especiall! tr e in the case of doc (ents )here the doc (ent order and doc (ent str ct re are ver! i(portant. Ass (e a sa(ple scenario )here !o r application gets an +ML doc (ent fro( so(e data so rce and !o r application is s pposed to store the doc (ent. Storing it in an n-archar or te%t col (n does not g arantee )ell6for(ed +ML and does not provide eas! access to its content. 3n s ch cases- it is appropriate to store the inco(ing +ML doc (ent nativel! in the +ML col (n.
The +ML data t!pe provides the a.ilit! to perfor( fine6grained < er! and (odif! operations on +ML data. 9rior to SQL Server 200"- there )as no )a! to store +ML nativel! in the data.ase. =ence- if !o )anted to (odif! or < er! +ML data- the data )o ld have to .e loaded fro( the n-archar or te%t col (n- an +ML doc (ent created o t of the string- and then (odified. Si(ilar steps )ere re< ired to )rite the (odified data .ac* to the data.ase. %o) )ith the +ML data t!pe- these steps .eco(e ( ch easier.
>ith the +ML data t!pe- it is possi.le to create inde;es on +ML data t!pe col (n for faster < er! processing. >hen the +ML data t!pe is sed- !o can se +ML sche(a collection and constraints on +ML data to enforce . siness r les. The +ML sche(as are sed to validate the data- add t!pe6 .ased operational se(antics- perfor( (ore precise t!pe chec*s than nt!ped +ML d ring co(pilation of < er! and data (odification state(ents- and opti(iAe storage and < er! processing.
As the +ML t!pe data is stored in the data.ase- it is incl ded in a variet! of data.ase activities s ch as .ac* p and restore- SQL Server sec rit!- transactions- logging- and so on.
Limitations
The follo)ing are so(e of the li(itations that one ( st .e a)are of )hen )or*ing )ith the ne) +ML data t!pe#
An e;act cop! of the data is not stored. 3nsignificant )hite spaces- na(espace prefi;esorder of attri. tes- and +ML declaration are not preserved. The (a;i(al depth of hierarch! in an +ML doc (ent is :2F. The (a;i(al siAe of the internal .inar! representation of an +ML doc (ent is 2J?. +ML instances cannot .e co(pared. Th s# An +ML col (n cannot .e part of a pri(ar! or foreign *e! constraint. An +ML col (n cannot .e sed in a J7859 ?H state(ent as a gro ping val e. +ML cannot .e cast to te%t- nte%t- and image data t!pes as these t!pes are deprecated in SQL Server 200". =o)ever- the +ML data t!pe can .e cast to 5n6-archar and 5n6-ar(inary t!pes.
4ispla! the list of all the c sto(ers along )ith their de(ographic infor(ation. 4ispla! the de(ographic infor(ation for a partic lar c sto(er. 3nsert a ne) c sto(er in the Sales.Store ta.le along )ith the de(ographic infor(ation. Modif! so(e ele(ents /s ch as Ann al Sales- Ann al 7even e- etc.2 of the de(ographic infor(ation for a given c sto(er. 4elete the de(ographic infor(ation for a given c sto(er.
The application ses the System.Data.S;lTypes.S;lXml class to retrieve the data fro( an +ML col (n. The SQL+ML class is a direct (apping to the +ML col (n. 5sing the SQL+ML class- it is possi.le to directl! retrieve the data fro( the +ML col (n )itho t an! (appings or conversions. =ere- let s loo* at an e;a(ple of ho) to retrieve the Ann al 7even e ele(ent for the c sto(er 34 :2. The follo)ing code e;a(ple ill strates the second f nction (entioned a.ove. Cop! Code
SqlConnection conn & ne' SqlConnection #; conn.ConnectionString & @2Server&local0ost; Data+ase&9dventure%or=s; integrated security&SS7I2; conn.Open #; SqlCommand command & conn.CreateCommand #; command.CommandText & @2select Demograp0ics.query Kdeclare namespace SS&20ttpH((sc0emas.microso*t.com(sqlserver(4::6(:N(adventureI'or=s(StoreSurvey2 GStoreIn*oJ
G9nnual>evenueJ {data (SSHStoreSurvey(SSH9nnual>evenue#$ G(9nnual>evenueJ G(StoreIn*oJK# as >esult *rom Sales.Store '0ere CustomerID&342; SqlData>eader datareader & command.,xecute>eader #; System.Text.String<uilder +uilder & ne' System.Text.String<uilder #; %0ile { SqlXml sqlxml & datareader.FetSqlXml :#; +uilder.9ppend sqlxml.Aalue#; $ ((CoteH xml3 is a XM1 'e+ control t0is.xml3.DocumentContent & +uilder.ToString #; t0is.xml3.Trans*ormSource & @2StoreIn*o.xslt2; $
The a.ove (ethod creates an instance of the S;lConnection o.Bect to the 'd-enture2or0s data.ase. The co((and te;t propert! contains the < er! to retrieve the val e of Ann al 7even e field for the c sto(er 34 :2. This is an +Q er! that )ill get directl! e;ec ted on the +ML col (n. The ret rn val e of the < er! )ill .e an +ML frag(ent- )hich is then (apped to the SQL+ML class. The +ML frag(ent can then .e retrieved sing the 3alue propert! of the SQL+ML class. The retrieved +ML frag(ent is then displa!ed in the client application- sing an +ML >e. server control.
datareader.>ead ##
XML classes in the ..$T /?1 XML > /rame&or0 ?)$.XML SQLXML =igh. There are no classes that directl! (ap .et)een +ML data and relational data.
Medi (. Lo). The Lo). Since >riting classes provide +ML data is < eries sing a (echanis( stored as '87 +ML to (anip late s ch in the &+9L3C3T is the relational col (ndiffic lt. data as +ML co(ple;it! data and the is less. pdategra(s ?esidesprovide a $is al facilit! to St dio 200" pdate the provides records as classes to )ell. (anip late the +ML data t!pe. +ML 4ML can .e sed for (odif!ing the +ML data. &as!. 3n (ost cases(odif!ing the (apping +S4 files can acco((odate changes. &as!. +Q er! provides eas! s!nta; for < er!ing the +ML col (ns in the data.ase.
Maintaina.ilit!
Co(plicated. Changes in 4iffic lt. the fields of the ta.le or Changes in +ML re< ire code change. the fields of the ta.le or +ML re< ire changes to the < er!.
3nstallation
%o special 7e< ires that %o special installations SQL+ML li.rar! installation re< ired. is installed on re< ired. the client (achines. Sec re if proper care is ta*en to avoid disclos re of ta.le na(es and col (n na(es. S pported The design has Sec re to sec re the (apping +S4 file if it is stored client side instead of (id6tier. %ot s pported %ot s pported. 3f +ML data t!pe col (ns fro( SQL Server are s!nced to SQL Server (o.ile- it )ill .e converted to
Sec rit!
Sec re to a ver! good e;tent- since the data t!pe and for(ats are not e;posed on the client side in nor(al case.
Li(ited s pport. +(l4ata4oc (ent is not s pported in the Microsoft .%&T Co(pact 'ra(e)or*.
nte;t. 4ata $alidation Can .e enforced .! the client and the server. Can .e Can .e enforced .! perfor(ed .! the server. the client. Can .e enforced .! the server sing +ML Sche(a.
4ata Storage
DnGvarchar/(a;2- +ML- or 7elational 7elational +ML data var.inar!/(a;2 ta.les /can ta.les /can se t!pe se +ML as a +ML as a field2. field2. Te;t al fidelit! /preserves 7elational 7elational the +ML data at .!te level2 fidelit! fidelit! /preserves the hierarchical str ct re of the data- . t the order a(ong ele(ents is ignored2 S pports pdates at doc (ent level. S pports fine6grained data access and pdates. S pports fine6 grained data access and pdates. 3nfoSet fidelit! /preserves the 3nfoSet content of the +ML data2
'idelit!
Conclusion
This paper provides the ser )ith the different options for handling +ML in SQL Server 200". The S!ste(.+(l na(espace- SQL+ML- and the +ML data t!pe are disc ssed )ith the relative .enefits and li(itations of each- and incl de sa(ple scenarios. The perfor(ance e;hi.ited in ideal scenarios ena.les the ser to choose the appropriate +ML option for the ser@s application.