Sie sind auf Seite 1von 16

2007 Microsoft Corporation. All rights reserved. Microsoft SQL Server 9.

0 Technical Articles

What's New in FOR XML in Microsoft SQL Server 2005


Michael R s Microsoft Corporation !"ne 200# "pdated !"ne 200$ Applies to% Microsoft SQL Server 200$ Summary: This first in a series of articles & Michael R s details the 'a(or ne) capa&ilities of the server*side +,R -ML cla"se in the ne.t version of SQL Server. These ena&le o" to f"rther enhance the -ML s"pport in o"r applications and to )rite eas *to*'aintain relational data*to*-ML aggregations. /02 printed pages1 Contents 2ntrod"ction 3ac4)ards*Co'pati&ilit )ith SQL Server 2000 2ntegration )ith the -ML 5ata T pe Assigning +,R -ML Res"lts 6esting of +,R -ML 7.pressions The 6e) 8AT9 Mode Adding -ML 6a'espaces Rec"rsion and +,R -ML More +,R -ML 7.tensions Concl"sion

Introduction
2n Microsoft SQL Server 2000: )e introd"ced the +,R -ML cla"se to the SELECT state'ent. This cla"se provides the a&ilit to aggregate the relational ro)set ret"rned & the SELECT state'ent into -ML. +,R -ML on the server s"pports three 'odes;RA<: A=T,: and 7-8L2C2T;that provide different transfor'ation se'antics. RA< 'ode generates single ele'ents: )hich are na'ed row: for each ro) ret"rned. The A=T, 'ode e'plo s a he"ristic to infer a si'ple: one ele'ent na'e*per*level hierarch &ased on the lineage infor'ation and the order of the data in a SELECT state'ent. +inall : the 7-8L2C2T 'ode re>"ires a specific ro)set for'at that can &e 'apped into al'ost an -ML shape: )hile still &eing for'"lated & a single SQL >"er . All three 'odes are designed to generate the -ML in a strea'a&le )a in order to &e a&le to prod"ce large doc"'ents efficientl . The 7-8L2C2T 'ode for'at is highl s"ccessf"l in achieving its goals. There are onl a fe) things it cannot do /ar&itrar rec"rsive part*list trees are one of the'1. The preferred SQL e.pression to generate the ro)set for'at is a for'ida&le ?>"er fro' hell:? ho)ever. =nfort"natel : +,R -ML res"lts in SQL Server 2000 can onl &e cons"'ed on the client side: and a"thoring co'ple. -ML str"ct"res "sing +,R -ML 7-8L2C2T is a co'ple. e.ercise. As one of the designers of the ro)set for'at: 2 have all the respect for people )ho )rite: and then 'aintain:

'"lti*tho"sand line 7-8L2C2T 'ode >"eries. 2 also: ho)ever: "nderstand the "sa&ilit : 'aintaina&ilit : and co'ple.it iss"es. 2n SQL Server 200$: )e no) have alternatives to the ?>"er fro' hell.? 2n the follo)ing: )e )ill ta4e a loo4 at the 'ain feat"res added to +,R -ML in SQL Server 200$% 2ntegration )ith the -ML data t pe: assigna&ilit and nesting of e.pressions: and the ne) 8AT9 'ode. <e provide so'e 6orth)ind sche'a*&ased e.a'ples that sho) ho) 7-8L2C2T 'ode >"eries can &e 'ore easil )ritten "sing the ne) feat"res. 3esides these ne) feat"res: +,R -ML also provides a co"ple of additional ne): s'aller feat"res that are s"''ari@ed at the end of this section and s"pports an easier )a to generate na'espace* a)are doc"'ents. Note +,R -ML contin"es to &e a ro)set aggregation cla"se of the SQL S7L7CT state'ent: and th"s cannot transfor' side*effect o"tp"t fro' stored proced"res. 2f o" )ant res"lts of so'e for' of processing transfor'ed into -ML: please "se either a "ser*defined f"nction or a vie).

Backwards-Compatibility with SQL Ser er !"""


An i'portant aspect of +,R -ML in SQL Server 200$ is the &ac4)ards*co'pati&ilit )ith +,R -ML in SQL Server 2000. The &ac4)ards*co'pati&ilit aspects can &e split into the follo)ing categories% &"g fi.es: co'pati&ilit 'ode changes: and preserving +,R -ML &ehavior. 2n the first categor : &"g fi.es: )e incorporated so'e &"g fi.es )ith respect of treat'ent of vie)s and s"&>"eries in the A=T, 'ode. <e also changed the entiti@ation r"les in order to preserve )hitespaces: and to 'ini'i@e entiti@ation /this change has no se'antic i'pact1. Since these are considered &"g fi.es: the occ"r regardless of the server co'pati&ilit flag. Man "sers felt that instances of the SQL t pe timestamp sho"ld have &een reported as a /&aseA#*encoded1 &inar val"e instead of the n"'&er representation. <e therefore changed the treat'ent of ti'esta'p val"es in SQL Server 200$ to &e seriali@ed as &aseA# encoded &inar val"es. The SQL Server 2000 &ehavior can still &e re>"ested & setting the serverBs co'pati&ilit level to SQL Server 2000. +inall : for perfor'ance reasons SQL Server 2000 ret"rned res"lts in a strea'ing )a : )itho"t chec4ing for )ell*for'edness. This 'eans that the res"lt co"ld contain invalid -ML characters s"ch as =C0007 /the ASC22 37LL character1: or /& 'eans of a corr"pted #$mlte$t field1 have non* )ellfor'ed str"ct"re. All s"ch cases are nor'all detected & the client*side parsers: &"t a "ser 'a not "se an -ML parser to cons"'e the data: instead "sing a si'ple s"&string e.traction. 2t )as therefore decided to preserve this &ehavior in a &ac4)ards*co'pati&le )a in SQL Server 200$ regardless of co'pati&ilit level. Since the ne) -ML data t pe does not accept s"ch invalid -ML /altho"gh it can deal )ith the frag'ent res"lt for' of +,R -ML1: a nor'al +,R -ML >"er in SQL Server 200$ )ill ret"rn its res"lt as an instance of t pe n archar%ma$&. More precisel : it )ill ret"rn its res"lt as a single ro): single col"'n ro)set )here the cell contains the string val"e of t pe n archar%ma$&.

Inte'ration with the ()L *ata Type


<ith the introd"ction of the ()L data t pe: )e )anted to also give +,R -ML the a&ilit to generate an instance of ()L directl /'ore precisel : it generates a single ro): single col"'n ro)set )here the cell contains the ()L data t pe instance1. 3eca"se of the &ac4)ards*co'pati&ilit considerations o"tlined a&ove: )e added a ne) T+,E directive to generate the res"lt as ()L. +or e.a'ple: Cop Code

SELECT * FROM Customers FOR XML AUTO, TYPE

ret"rns the Customers ele'ents as an ()L data t pe instance: instead of the n archar%ma$& instance that )o"ld have &een the case )itho"t the T+,E directive. This res"lt is g"aranteed to confor' to the )ell*for'edness constraints provided & the ()L data t pe. Since the res"lt is an ()L data t pe instance: o" can also "se (Query e.pressions to >"er and reshape the res"lt. +or e.a'ple: the follo)ing e.pression retrieves the Customer contact na'e into a ne) ,erson ele'ent. Cop Code

SELECT (SELECT * FROM Customers FOR XML AUTO, TYPE).query( '< o!"# $or %! &' (Customers retur' <Perso' ')me*+# )t)(%!(,Co't)!t-)me).+(" .<( o!"')
ret"rns /onl first ele'ents sho)n1: Cop Code

< o!" <Perso' ')me*+M)r&) A' ers+ (" <Perso' ')me*+A') Tru/&00o+ (" <Perso' ')me*+A'to'&o More'o+ (" ... <( o!"

-ssi'nin' ./0 ()L 0esults


Since +,R -ML >"eries no) ret"rn assigna&le val"es: the res"lt of a +,R -ML >"er can &e assigned to a varia&le: or inserted into a col"'n. Cop Code

1ECLARE ,!ust XML2 SET ,!ust * (SELECT * FROM Customers FOR XML AUTO, TYPE) CREATE TA3LE T(& &'t, 4 XML) 5-SERT 5-TO T SELECT 6, (SELECT * FROM Customers FOR XML AUTO, TYPE)

Nestin' o1 ./0 ()L E$pressions


+,R -ML: in SQL Server 200$: recogni@es ()L data t pe col"'ns: and )ill inline the' as s"&* ele'ents. Th"s: )e can nest +,R -ML >"eries to generate hierarchies: instead of having to rel on the A=T, 'ode he"ristic: or )riting an 7-8L2C2T 'ode >"er . LetBs loo4 at the e.a'ple. The follo)ing +,R -ML 7-8L2C2T >"er is ret"rning Customer ele'ents: containing their orders and the e'plo ees that )or4 on their orders. +or si'plification: )e are onl ret"rning a single propert per ele'ent. Cop Code

SELECT 6 )s TA7,

-ULL )s P)re't, Customer51 )s +Customer868Customer51+, -ULL )s +Or er898Or er51+, -ULL )s +Em:0oyee8;8L)st-)me+ FROM Customers U-5O- ALL SELECT 9, 6, Customers.Customer51, Or ers.Or er51, -ULL FROM Or ers <O5- Customers O- Or ers.Customer51 * Customers.Customer51 U-5O- ALL SELECT 15ST5-CT ;, 6, Customers.Customer51, -ULL, Em:0oyees.L)st-)me FROM Customers <O5- Or ers O- Customers.Customer51 * Or ers.Customer51 <O5- Em:0oyees O- Or ers.Em:0oyee51 * Em:0oyees.Em:0oyee51 OR1ER 3Y +Customer868Customer51+,+Em:0oyee8;8L)st-)me+,+Or er898Or er51+ FOR XML EXPL5C5T
ret"rns /onl first c"sto'er sho)n1: Cop Code

<Customer Customer51*+ALF=5+" <Or er Or er51*+6>?@;+ (" <Or er Or er51*+6>?A9+ (" <Or er Or er51*+6>B>9+ (" <Or er Or er51*+6>C;D+ (" <Or er Or er51*+6>AD9+ (" <Or er Or er51*+66>66+ (" <Em:0oyee L)st-)me*+1)Eo0&o+ (" <Em:0oyee L)st-)me*+LeEer0&'F+ (" <Em:0oyee L)st-)me*+Pe)!o!G+ ("

<Em:0oyee L)st-)me*+Suy)m)+ (" <(Customer" ...


As o" can see: )e need a select state'ent for each ele'ent. <e also repeat the parentBs identification )ith ever child: so that the order by gro"ps the children )ith their parents. The strea'ing seriali@ation fro' the ro)set to -ML depends on this gro"ping to get the correct nesting. 6o) letBs loo4 at ho) )e can re)rite this & "sing nesting +,R -ML e.pressions. <e can leverage the ne) T+,E directive to generate ()L data t pe instances /other)ise: o" )ill get a te.t"al res"lt that )ill &e entiti@ed if it is e'&edded in another +,R -ML >"er 1 and nest s"& selections to define the hierarch . <e "se a separate +,R -ML >"er for each of the three ?entit ? ele'ents: and nest the' to e.press the hierarch . <e no) re)rite the previo"s 7-8L2C2T 'ode >"er "sing the A=T, 'ode and nesting% Cop Code

SELECT Customer51 )s +Customer51+, (SELECT Or er51 )s +Or er51+ FROM Or ers +Or er+ HIERE +Or er+.Customer51 * Customer.Customer51 FOR XML AUTO, TYPE), (SELECT 15ST5-CT L)st-)me )s +L)st-)me+ FROM Em:0oyees Em:0oyee <O5- Or ers +Or er+ O- +Or er+.Em:0oyee51 * Em:0oyee.Em:0oyee51 HIERE Customer.Customer51 * +Or er+.Customer51 FOR XML AUTO, TYPE) FROM Customers Customer FOR XML AUTO, TYPE
This ret"rns: e.cept for g"aranteeing the ele'ent order: the sa'e res"lt as the 7-8L2C2T 'ode >"er /if o" care a&o"t the order: o" can add an order & state'ent1. ,&vio"sl : this >"er is easier to )rite: "nderstand: and 'aintain: even tho"gh it contains &asicall the sa'e n"'&er of select state'ents and (oin conditions.

The New ,-T2 )ode


2n the a&ove section: )e loo4ed at ho) )e can "tili@e so'e of the +,R -ML capa&ilities to re)rite a si'ple +,R -ML 7-8L2C2T >"er into a si'pler >"er that e.ploits the nesta&ilit of +,R -ML: the A=T, 'ode: and the ne) -ML data t pe. ,ne 'a sa : ho)ever: that the previo"s >"er )as too si'ple to reflect a real )orld scenario )ith +,R -ML 7-8L2C2T. +or e.a'ple: so'e of the strengths of the e.plicit 'ode are to 'i. attri&"tes and ele'ents at )ill: create )rappers and nested: co'ple. properties: and even create space*separated val"e lists and 'i.ed content. 6one of these res"lts can &e achieved & nesting +,R -ML A=T, >"eries. So do )e still have to )rite 7-8L2C2T 'ode >"eries to get these 'apping capa&ilitiesD

5onBt despairE A ne) +,R -ML 'ode gives o" the sa'e fle.i&ilit in a '"ch si'pler )a . The ne) 8AT9 'ode: together )ith the a&ilit to nest +,R -ML e.pressions: has &eco'e the si'plest )a to generate co'ple. -ML doc"'ents. The 8AT9 'ode allo)s o" to "se an -8ath*li4e s nta. as a col"'n na'e: )hich then is 'apped into an attri&"te /e.g.: 34a31: ele'ent /e.g.: 3e31: s"& ele'ent str"ct"re /3e56e!31: ele'ent content /3731: te.t node /3te$t%&31: or data val"e /3data%&31. As )ith the RA< 'ode: the defa"lt na'e for the ro) ele'ent is row and can &e over)ritten )ith an 6C6a'e /a na'e )itho"t a prefi.1. LetBs loo4 at so'e e.a'ples. +irst: let "s give the 8AT9 'ode for'"lation of the a&ove 7-8L2C2T 'ode >"er . Cop Code

SELECT Customer51 )s +,Customer51+, (SELECT Or er51 )s +,Or er51+ FROM Or ers HIERE Or ers.Customer51 * Customers.Customer51 FOR XML PATI('Or er'), TYPE), (SELECT 15ST5-CT L)st-)me )s +,L)st-)me+ FROM Em:0oyees <O5- Or ers O- Or ers.Em:0oyee51 * Em:0oyees.Em:0oyee51 HIERE Customers.Customer51 * Or ers.Customer51 FOR XML PATI('Em:0oyee'), TYPE) FROM Customers FOR XML PATI('Customer')
This e.a'ple is si'ilar to the A=T, 'ode version and ret"rns the sa'e res"lt. 6o) letBs loo4 at so'e of the 8AT9 'ode specific capa&ilities. The ne.t >"er ta4es the c"sto'er infor'ation and gro"ps the address and contact infor'ation into separate s"& ele'ents "sing a 'ore co'ple. path e.pression as col"'n alias: and;"sing the ne) R,,T directive;adds a root node aro"nd it for good 'eas"re. Cop Code

SELECT Customer51 )s +,Customer51+, Com:)'y-)me, A ress )s +) ress(street+,

C&ty )s +) ReF&o' )s +)

ress(!&ty+, ress(reF&o'+, ress(J&:+,

Post)0Co e )s +) Cou'try )s +)

ress(!ou'try+,

Co't)!t-)me )s +!o't)!t(')me+, Co't)!tT&t0e )s +!o't)!t(t&t0e+, PKo'e )s +!o't)!t(:Ko'e+, F)4 )s +!o't)!t($)4+

FROM Customers FOR XML PATI('Customer'), ROOT(' o!')


This >"er res"lts in the follo)ing doc"'ent /onl sho)ing the first c"sto'er ele'ent1. Cop Code

< o!" <Customer Customer51*+ALF=5+" <Com:)'y-)me"A0$re s FutterG&ste<(Com:)'y-)me" <) ress"

<street"OLere Str. DB<(street" <!&ty"3er0&'<(!&ty" <J&:"699>A<(J&:" <!ou'try"7erm)'y<(!ou'try" <() ress"

<!o't)!t" <')me"M)r&) A' ers<(')me" <t&t0e"S)0es Re:rese't)t&Ee<(t&t0e" <:Ko'e">;>M>>B@;96<(:Ko'e" <$)4">;>M>>B?D@D<($)4" <(!o't)!t" <(Customer" ... <( o!"
9o) )o"ld this >"er loo4 li4e if it "sed the 7-8L2C2T 'odeD <e need fo"r select cla"ses;one for each non*leaf ele'ent;instead of onl one selection. Cop Code

SELECT to: 6 6 )s TA7, -ULL )s P)re't, 6 )s + o!868 ummy8K& e+, -ULL )s +Customer898Customer51+, -ULL )s +Customer898Com:)'y-)me8e0eme't+, -ULL )s +) -ULL )s +) -ULL )s +) -ULL )s +) -ULL )s +) ress8;8street8e0eme't+, ress8;8!&ty8e0eme't+, ress8;8reF&o'8e0eme't+, ress8;8J&:8e0eme't+, ress8;8!ou'try8e0eme't+,

-ULL )s +!o't)!t8@8')me8e0eme't+, -ULL )s +!o't)!t8@8t&t0e8e0eme't+, -ULL )s +!o't)!t8@8:Ko'e8e0eme't+, -ULL )s +!o't)!t8@8$)48e0eme't+ FROM Customers U-5O- ALL SELECT 9, 6, 6, Customer51, Com:)'y-)me, -ULL, -ULL, -ULL, -ULL, -ULL, -ULL, -ULL, -ULL, -ULL FROM Customers U-5O- ALL SELECT ;, 9, 6, Customer51, -ULL, A ress, C&ty, ReF&o', Post)0Co e, Cou'try,

-ULL, -ULL, -ULL, -ULL FROM Customers U-5O- ALL SELECT @, 9, 6, Customer51, -ULL, -ULL, -ULL, -ULL, -ULL, -ULL, Co't)!t-)me, Co't)!tT&t0e, PKo'e, F)4 FROM Customers OR1ER 3Y + o!868 ummy8K& e+,+Customer898Customer51+ FOR XML EXPL5C5T, TYPE
6o) )e 4no) )h the 7-8L2C2T 'ode is so'eti'es called the ?>"er fro' hell.? Last &"t not least: the follo)ing gives "s an e.a'ple of generating a val"e list: and sho)s the "se of a te.t node. Cop Code

SELECT Customer51 )s +,51+, (SELECT Or er51 )s + )t)()+ FROM Or ers HIERE Customers.Customer51*Or ers.Customer51

FOR XML PATI('') ) )s +,Or er51s+, Com:)'y-)me, Co't)!tT&t0e )s +Co't)!t-)me(,Co't)!tT&t0e+, Co't)!t-)me )s +Co't)!t-)me(te4t()+, Post)0Co e )s +A A ress )s +A ress(,N5P+,

ress(Street+,

C&ty )s +A FROM Customers

ress(C&ty+

FOR XML PATI('Customer')


This creates a res"lt of the for' /sho)ing one e.a'ple c"sto'er1: Cop Code

<Customer 51*+IU-7C+ Or er51s*+6>;BD 6>;A@ 6>@6D 6>?>> 6>??>+" <Com:)'y-)me"Iu'Fry Coyote 5m:ort Store<(Com:)'y-)me" <Co't)!t-)me Co't)!tT&t0e*+S)0es Re:rese't)t&Ee+"YosK& L)t&mer<(Co't)!t-)me" <A ress N5P*+ABC9B+"

<Street"C&ty Ce'ter P0)J) D6? M)&' St.<(Street" <C&ty"E0F&'<(C&ty" <(A ress"

<(Customer"
LetBs dissect the relevant parts of the >"er . The s"& >"er : )hich generates the /rderI*s attri&"te list: 'aps the /rderI* col"'n val"es as ato'ic val"es /"sing the path data%&1. These val"es )ill then &e seriali@ed as a te.t node & adding a space &et)een si&ling ato'ic val"es: )hich are provided in the ne.t cell in the ro)set. <e then avoid generating a na'e for the ro) & "sing the @ero*length string as the 8AT9 'ode arg"'ent: so that )e get a single string /note: there is no TF87 directiveE1 as the res"lt of the +,R -ML 8AT9 e.pression. That string gets 'apped to the /rderI*s attri&"te & the containing +,R -ML e.pression. The CompanyName gets 'apped to a s"& ele'ent of the sa'e na'e. The ContactTitle prod"ces the ContactTitle attri&"te of the ContactName ele'ent: )hile the ContactName col"'n val"e is 'apped into the te.t node of the sa'e ele'ent. 6ote that in this case: the sa'e res"lt )o"ld have &een achieved & 'apping ContactName to the ContactName ele'ent directl . +inall : the properties for the -ddress ele'ent parts are p"t together.

-ddin' ()L Namespaces


-ML 6a'espaces are &eco'ing an increasingl i'portant aspect of a"thoring -ML doc"'ents for infor'ation interchange. The are "sed to disa'&ig"ate different voca&"laries: identif o)nership of

a voca&"lar : and to associate -ML Sche'a infor'ation /and potentiall other infor'ation1 )ith an -ML ele'ent or attri&"te. +,R -ML: in SQL Server 2000: p"ts the &"rden of generating and 'aintaining -ML na'espaces co'pletel on the >"er )riter. The -ML na'espace declaration attri&"tes had to &e created: li4e ever other attri&"te: )ith the na'espace =R2 &eing the col"'n val"e. =nless the generated -ML )as in attri&"te*centric for': this 'eant that the >"er had to &e )ritten "sing the 7-8L2C2T 'ode. +or e.a'ple: the follo)ing >"er p"ts the res"lting C"sto'er ele'ents and its propert ele'ents into the na'espace urn:e$ample8com6customer% Cop Code

SELECT 6 )s t)F, -ULL )s :)re't, 'ur'Oe4)m:0e.!om(!ustomer' )s +!ustOCustomer8684m0'sO!ust+, Customer51 )s +!ustOCustomer868!ustOCustomer518e0eme't+, Co't)!t-)me )s +!ustOCustomer868!ustOCo't)!t-)me8e0eme't+ FROM Customers FOR XML EXPL5C5T
The res"lt of the >"er is /onl first t)o ele'ents sho)n1% Cop Code

<!ustOCustomer 4m0'sO!ust*+ur'Oe4)m:0e.!om(!ustomer+" <!ustOCustomer51"ALF=5<(!ustOCustomer51" <!ustOCo't)!t-)me"M)r&) A' ers<(!ustOCo't)!t-)me" <(!ustOCustomer" <!ustOCustomer 4m0'sO!ust*+ur'Oe4)m:0e.!om(!ustomer+" <!ustOCustomer51"A-ATR<(!ustOCustomer51" <!ustOCo't)!t-)me"A') Tru/&00o<(!ustOCo't)!t-)me" <(!ustOCustomer"
6a'espace declarations are not reall attri&"tes in the -ML data 'odel. Th"s: the 8AT9 'ode does not allo) the' to &e specified as attri&"tes. 2n order to si'plif the "se of -ML na'espaces in +,R -ML: )e have added s"pport for the <2T9 -ML6AM7S8AC7S cla"se starting in the April CT8 version of SQL Server 200$. The <2T9 -ML6AM7S8AC7S cla"se is defined in the SQL%200G standard as an e.tension to the general <2T9 cla"se that is co''onl "sed to define co''on ta&le e.pressions. The <2T9 cla"se can &e placed on top*level SQL state'ents: s"ch as S7L7CT: 26S7RT and =85AT7 state'ents: and "sed inside a CR7AT7 H27< state'ent. The <2T9 -ML6AM7S8AC7S cla"se can &e "sed )ith the RA<: A=T, and 8AT9 'ode: &"t not )ith either the -MLSC97MA and -ML5ATA directives or the 7-8L2C2T 'ode. The previo"s 'ethod of creating na'espaces is still s"pported for the SQL Server 2000 'odes: &"t cannot &e 'i.ed )ith the <2T9 -ML6AM7S8AC7S cla"se. To disa'&ig"ate the <2T9 cla"se fro' other s ntactic "ses of <2T9: T*SQL state'ents that precede <2T9 cla"ses need to &e ter'inated )ith a se'i*colon /I1. The follo)ing >"er places the c"sto'er and order data into different na'espaces and adds a root node in a defa"lt na'espace. Cop Code

H5TI XML-AMESPACES ( 1EFAULT 'ur'Oe4)m:0e.!om( o!'

, 'ur'Oe4)m:0e.!om(!ustomer' )s +!+ , 'ur'Oe4)m:0e.!om(or er' )s +o+ ) SELECT Customer51 )s +,51+, (SELECT Or er51 )s +,Or er51+ $rom Or ers PKere Customers.Customer51*Or ers.Customer51 FOR XML PATI('oOOr er'), TYPE ) )s +!OOr ers+, Com:)'y-)me )s +!OCom:)'y-)me+, Co't)!tT&t0e )s +!OCo't)!t-)me(,Co't)!tT&t0e+, Co't)!t-)me )s +!OCo't)!t-)me(te4t()+, Post)0Co e )s +!OA A ress )s +!OA ress(,N5P+,

ress(!OStreet+,

C&ty )s +!OA FROM Customers

ress(!OC&ty+

FOR XML PATI('!OCustomer'), ROOT(' o!')

As the follo)ing partial res"lt sho)s: the -ML na'espace declarations are c"rrentl added at the top*level ele'ents of ever +,R -ML selection% Cop Code

< o! 4m0'sOo*+ur'Oe4)m:0e.!om(or er+ 4m0'sO!*+ur'Oe4)m:0e.!om(!ustomer+ 4m0's*+ur'Oe4)m:0e.!om( o!+" <!OCustomer 51*+ALF=5+" <!OOr ers" <oOOr er 4m0'sOo*+ur'Oe4)m:0e.!om(or er+ 4m0'sO!*+ur'Oe4)m:0e.!om(!ustomer+ 4m0's*+ur'Oe4)m:0e.!om( o!+ Or er51*+6>?@;+ (" <oOOr er 4m0'sOo*+ur'Oe4)m:0e.!om(or er+ 4m0'sO!*+ur'Oe4)m:0e.!om(!ustomer+ 4m0's*+ur'Oe4)m:0e.!om( o!+ Or er51*+6>?A9+ (" <oOOr er 4m0'sOo*+ur'Oe4)m:0e.!om(or er+ 4m0'sO!*+ur'Oe4)m:0e.!om(!ustomer+ 4m0's*+ur'Oe4)m:0e.!om( o!+ Or er51*+6>B>9+ (" <oOOr er 4m0'sOo*+ur'Oe4)m:0e.!om(or er+ 4m0'sO!*+ur'Oe4)m:0e.!om(!ustomer+ 4m0's*+ur'Oe4)m:0e.!om( o!+

Or er51*+6>C;D+ (" <oOOr er 4m0'sOo*+ur'Oe4)m:0e.!om(or er+ 4m0'sO!*+ur'Oe4)m:0e.!om(!ustomer+ 4m0's*+ur'Oe4)m:0e.!om( o!+ Or er51*+6>AD9+ (" <oOOr er 4m0'sOo*+ur'Oe4)m:0e.!om(or er+ 4m0'sO!*+ur'Oe4)m:0e.!om(!ustomer+ 4m0's*+ur'Oe4)m:0e.!om( o!+ Or er51*+66>66+ (" <(!OOr ers" <!OCom:)'y-)me"A0$re s FutterG&ste<(!OCom:)'y-)me" <!OCo't)!t-)me Co't)!tT&t0e*+S)0es Re:rese't)t&Ee+"M)r&) A' ers<(!OCo't)!t-)me" <!OA ress N5P*+699>A+"

<!OStreet"OLere Str. DB<(!OStreet" <!OC&ty"3er0&'<(!OC&ty" <(!OA ress"

<(!OCustomer" ...
The a&ove >"er "sed the 57+A=LT cla"se for adding a defa"lt na'espace. 6ote that if there are nested -ML doc"'ents )ith no defa"lt na'espace incl"ded in the res"lt: a slight perfor'ance penalt has to &e paid to 'a4e s"re that the preserve their a&sence of a defa"lt na'espace. +inall : the <2T9 -ML6AM7S8AC7S cla"se can also &e "sed to provide na'espace &indings for the -Q"er and -ML 5ML 'ethods on the -ML data t pe.

0ecursion and ./0 ()L


,ne of the strengths of the -ML for'at is that it can easil represent hierarchies: incl"ding str"ct"rall rec"rsive hierarchies s"ch as parts lists. 2n SQL Server 2000: o" co"ld not generate s"ch str"ct"res )itho"t 4no)ing the 'a.i'"' depth at >"er for'"lation ti'e. Since )e no) can nest +,R -ML e.pressions: )e can easil generate the rec"rsive hierarchies "sing "ser*defined f"nctions. +or e.a'ple: the follo)ing "ser*defined f"nction creates a nested -ML doc"'ent detailing the parts list for a specific part. +irst: let "s define so'e e.a'ple data% Cop Code

CREATE TA3LE PARTS(& 7O 5-SERT 5-TO PARTS

&'t, :)re't &'t, ')me 'E)r!K)r(D>>))

SELECT 6, -ULL, -'!)r' U-5OSELECT 9, 6, -'e'F&'e' U-5O-

SELECT ;, 6, -'Lo y' U-5OSELECT @, ;, -' oor' U-5OSELECT D, ;, -'$e' er' U-5OSELECT ?, @, -'P&' oP' U-5OSELECT B, 9, -':&sto''
6e.t: )e define the f"nction that for a given part n"'&er ret"rns the s"&parts in -ML for'% Cop Code

CREATE FU-CT5O- P)rtsL&st(,P)rts-o &'t) RETUR-S XML H5TI RETUR-S -ULL O- -ULL 5-PUT 3E75- RETUR(SELECT & )s +,& +, ')me )s +,')me+,

CASE HIE- :)re't*,P)rts-o TIEE-1 FROM Lo.PARTS HIERE :)re't*,P)rts-o Lo.P)rtsL&st(& )

FOR XML PATI('P)rts'), TYPE) E-1


<e "se a CAS7 state'ent to 'a4e s"re that the >"er e.ec"tion does not calc"late the f"nction rec"rsivel if the opti'i@er decides to appl the filter after e.ec"ting the selection. 7.ec"ting the follo)ing e.pression )ill ret"rn part G )ith it s"&parts. Cop Code

se0e!t &

)s +,& +, ')me )s +,')me+,

CASE HIE- & *; TIEE-1 FROM PARTS HIERE & *; FOR XML PATI('P)rts'), TYPE
Ret"rns% Cop Code

Lo.P)rtsL&st(& )

<P)rts & *+;+ ')me*+Lo y+"

<P)rts & *+@+ ')me*+ oor+" <P)rts & *+?+ ')me*+P&' oP+ (" <(P)rts" <P)rts & *+D+ ')me*+$e' er+ (" <(P)rts"
6ote that SQL Server 200$ has a 'a.i'"' li'it of G2 rec"rsivel nested f"nction invocations. 2f o"r parts hierarch e.ceeds the li'it: o" )ill need to "se the old approach of getting the -ML in flat for' and appl ing an -SLT st le sheet to create the hierarch .

)ore ./0 ()L E$tensions


3esides the ne) feat"res listed a&ove: SQL Server 200$ provides the follo)ing ne) feat"res% 0. RA< 'ode can &e co'&ined )ith the ELE)ENTS directive: and ta4e a para'eter to over)rite the ro) ele'ent na'e. +or e.a'ple: Cop Code

SELECT * FROM Customers FOR XML RAH('Customer'), ELEME-TS


ret"rns /onl sho)ing the first c"sto'er1: Cop Code

<Customer" <Customer51"ALF=5<(Customer51" <Com:)'y-)me"A0$re s FutterG&ste<(Com:)'y-)me" <Co't)!t-)me"M)r&) A' ers<(Co't)!t-)me" <Co't)!tT&t0e"S)0es Re:rese't)t&Ee<(Co't)!tT&t0e" <A ress"OLere Str. DB<(A ress"

<C&ty"3er0&'<(C&ty" <Post)0Co e"699>A<(Post)0Co e" <Cou'try"7erm)'y<(Cou'try" <PKo'e">;>M>>B@;96<(PKo'e" <F)4">;>M>>B?D@D<(F)4" <(Customer"

2.

The ELE)ENTS directive provides an -S262L option to 'ap N9LL val"es to an ele'ent )ith an attri&"te $si:nil:3true3. +or e.a'ple: Cop Code

SELECT * FROM Customers HIERE ReF&o' &s 'u00 FOR XML PATI('Customer'), ELEME-TS XS5-5L
ret"rns /onl sho)ing the first c"sto'er1: Cop Code

<Customer 4m0'sO4s&*+Ktt:O((PPP.P;.orF(9>>6(XMLS!Kem)M&'st)'!e+" <Customer51"ALF=5<(Customer51" <Com:)'y-)me"A0$re s FutterG&ste<(Com:)'y-)me" <Co't)!t-)me"M)r&) A' ers<(Co't)!t-)me" <Co't)!tT&t0e"S)0es Re:rese't)t&Ee<(Co't)!tT&t0e" <A ress"OLere Str. DB<(A ress"

<C&ty"3er0&'<(C&ty" <ReF&o' 4s&O'&0*+true+ (" <Post)0Co e"699>A<(Post)0Co e" <Cou'try"7erm)'y<(Cou'try" <PKo'e">;>M>>B@;96<(PKo'e" <F)4">;>M>>B?D@D<(F)4" <(Customer"
G. A ne) in*line sche'a inference directive ()LSC2E)- that ta4es a target na'espace 90I as an optional arg"'ent has &een added for the RA< and A=T, 'odes. +or e.a'ple: Cop Code

SELECT * FROM Customers FOR XML RAH('Customer'), XMLSCIEMA('ur'Oe4)m:0e.!om')


ret"rns /onl sho)ing parts of the sche'a and data1:

Cop Code

<4s Os!Kem) t)rFet-)mes:)!e*+ur'Oe4)m:0e.!om+ 4m0'sO4s *+Ktt:O((PPP.P;.orF(9>>6(XMLS!Kem)+ 4m0'sOsq0ty:es* +Ktt:O((s!Kem)s.m&!roso$t.!om(sq0serEer(9>>@(sq0ty:es+ e0eme'tForm1e$)u0t*+qu)0&$&e +" <4s O&m:ort ')mes:)!e* +Ktt:O((s!Kem)s.m&!roso$t.!om(sq0serEer(9>>@(sq0ty:es+ (" <4s Oe0eme't ')me*+Customer+" ... <(4s Oe0eme't" <(4s Os!Kem)" <Customer 4m0's*+ur'Oe4)m:0e.!om+ Customer51*+ALF=5+ Com:)'y-)me*+A0$re s FutterG&ste+ Co't)!t-)me*+M)r&) A' ers+ Co't)!tT&t0e*+S)0es Re:rese't)t&Ee+ A ress*+OLere Str. DB+ C&ty*+3er0&'+ Post)0Co e*+699>A+ Cou'try*+7erm)'y+ PKo'e*+>;>M>>B@;96+ F)4*+>;>M>>B?D@D+ (" ...
6ote that the 7-8L2C2T and 8AT9 'odes J "nli4e the RA< and A=T, 'odes J are nor'all "sed )hen generating the -ML according to a previo"sl provided sche'a. Therefore )e do not provide the sche'a inference directive )ith the 7-8L2C2T and 8AT9 'odes.

Conclusion
<e provided a first loo4 at the e.tended +,R -ML s"pport in SQL Server 200$. The added f"nctionalit : 'ostl ena&led & the ne) ()L data t pe: )ill 'a4e +,R -ML a ver po)erf"l and eas to "se tool to generate -ML fro' o"r relational data. The ne) 8AT9 'ode: together )ith the nesting of +,R -ML >"eries and the <2T9 -ML6AM7S8AC7S cla"se: gives eno"gh po)er to replace 'ost of the 7-8L2C2T 'ode >"eries in a si'pler: 'ore 'aintaina&le )a . The a&ilit to nest +,R -ML >"eries also provides a 'echanis' to generate rec"rsive hierarchies. There )ill still &e a fe) cases )here o" 'a )ant to "se the 7-8L2C2T 'ode /in order to generate C5ATA sections or to "se the #$mlte$t directive: for e.a'ple1: &"t the ne) f"nctionalit sho"ld 'a4e the ?>"er fro' hell? a '"ch less fre>"ent enco"nter.

Das könnte Ihnen auch gefallen