Blank xml namespace in child nodes in SQL Server -
hi need following in sql:
i need create xml file in format
<document xmlns="urn:iso:std:iso:20022:tech:xsd:001.002.001.04"> <fitofipmtstsrpt> <grphdr> <msgid></msgid> </grphdr> <orgnlgrpinfandsts> <orgnlmsgid /> </orgnlgrpinfandsts> </fitofipmtstsrpt> </document>
at moment have variable holds main info , build between info (take grphdr can inserted multiple times main xml, different info)
declare @xml xml='<document xmlns="urn:iso:std:iso:20022:tech:xsd:001.002.001.04"> <fitofipmtstsrpt> </fitofipmtstsrpt> </document> ' declare @xmlgrp xml='<grphdr> <msgid></msgid> </grphdr>' --here code fill msgid
then when add grphdr main xml
set @xml.modify ('declare namespace a= "urn:iso:std:iso:20022:tech:xsd:001.002.001.04"; insert sql:variable("@xmlgrp") (a:document/a:fitofipmtstsrpt)[1]') select @xml
i need out top file format happens following given
<document xmlns="urn:iso:std:iso:20022:tech:xsd:001.002.001.04"> <fitofipmtstsrpt> <grphdr xmlns="">-- need xmlns tag out <msgid /> </grphdr> </fitofipmtstsrpt> </document>
somehow need empty xmlns tag out of xml. can't convert varchar(max) remove our db has limited variable 8000 characters , xml can grow more 8000. there can multiple grphdr or orgnlgrpinfandsts in 1 file
table: lim_live_inbound lim_msg_id | lim_request_transaction_id | client_name ------------------------------------------------------ 021/00210006/20160225/000002 | 00012016-02-25000000023 | mr piet 021/00210006/20160225/000002 | 00012016-02-25000000022 | mrs name
must generate this
<document xmlns="urn:iso:std:iso:20022:tech:xsd:001.002.001.04"> <fitofipmtstsrpt> <grphdr> <msgid>021/00210006/20160225/000002</msgid> </grphdr> <orgnlgrpinfandsts> <orgnlmsgid>00012016-02-25000000023</orgnlmsgid> <name>mr piet</name> </orgnlgrpinfandsts> <orgnlgrpinfandsts> <orgnlmsgid>00012016-02-25000000022</orgnlmsgid> <name>mrs name</name> </orgnlgrpinfandsts> </fitofipmtstsrpt> </document>
this why i'm trying insert xml way. if 1 can me better way appreciated.
edit 2: found way avoid repeated namespaces. first create nested xml without namespace, join it:
declare @lim_live_inbound table(lim_msg_id varchar(100),lim_request_transaction_id varchar(100),client_name varchar(100)); insert @lim_live_inbound values ('021/00210006/20160225/000002','00012016-02-25000000023','mr piet') ,('021/00210006/20160225/000002','00012016-02-25000000022','mrs name'); declare @nestedxmls table(msgid varchar(100),nestedxml xml); grpmsg ( select distinct lim_msg_id msgid @lim_live_inbound ) insert @nestedxmls select msgid ,( select innertbl.lim_request_transaction_id orgnlmsgid ,innertbl.client_name name @lim_live_inbound innertbl innertbl.lim_msg_id=grpmsg.msgid xml path('orgnlgrpinfandsts'),type ) grpmsg; xmlnamespaces(default 'urn:iso:std:iso:20022:tech:xsd:001.002.001.04') ,grpmsg ( select distinct lim_msg_id msgid @lim_live_inbound ) select grpmsg.msgid [grphdr/msgid] ,n.nestedxml [node()] grpmsg inner join @nestedxmls n on grpmsg.msgid=n.msgid xml path('fitofipmtstsrp'),root('document')
the result
<document xmlns="urn:iso:std:iso:20022:tech:xsd:001.002.001.04"> <fitofipmtstsrp> <grphdr> <msgid>021/00210006/20160225/000002</msgid> </grphdr> <orgnlgrpinfandsts xmlns=""> <orgnlmsgid>00012016-02-25000000023</orgnlmsgid> <name>mr piet</name> </orgnlgrpinfandsts> <orgnlgrpinfandsts xmlns=""> <orgnlmsgid>00012016-02-25000000022</orgnlmsgid> <name>mrs name</name> </orgnlgrpinfandsts> </fitofipmtstsrp> </document>
edit: new approach fit sample data
this repeating namespace - syntactically correct, yet annoying (read here: https://connect.microsoft.com/sqlserver/feedback/details/265956/suppress-namespace-attributes-in-nested-select-for-xml-statements)
declare @lim_live_inbound table(lim_msg_id varchar(100),lim_request_transaction_id varchar(100),client_name varchar(100)); insert @lim_live_inbound values ('021/00210006/20160225/000002','00012016-02-25000000023','mr piet') ,('021/00210006/20160225/000002','00012016-02-25000000022','mrs name'); xmlnamespaces(default 'urn:iso:std:iso:20022:tech:xsd:001.002.001.04') ,grpmsg ( select distinct lim_msg_id msgid @lim_live_inbound ) select msgid [grphdr/msgid] ,( select innertbl.lim_request_transaction_id orgnlmsgid ,innertbl.client_name name @lim_live_inbound innertbl innertbl.lim_msg_id=grpmsg.msgid xml path('orgnlgrpinfandsts'),type ) grpmsg xml path('fitofipmtstsrp'),root('document')
the result
<document xmlns="urn:iso:std:iso:20022:tech:xsd:001.002.001.04"> <fitofipmtstsrp> <grphdr> <msgid>021/00210006/20160225/000002</msgid> </grphdr> <orgnlgrpinfandsts xmlns="urn:iso:std:iso:20022:tech:xsd:001.002.001.04"> <orgnlmsgid>00012016-02-25000000023</orgnlmsgid> <name>mr piet</name> </orgnlgrpinfandsts> <orgnlgrpinfandsts xmlns="urn:iso:std:iso:20022:tech:xsd:001.002.001.04"> <orgnlmsgid>00012016-02-25000000022</orgnlmsgid> <name>mrs name</name> </orgnlgrpinfandsts> </fitofipmtstsrp> </document>
this first approach
i don't know data comes from, - absolutely hard coded - approach:
with xmlnamespaces(default 'urn:iso:std:iso:20022:tech:xsd:001.002.001.04') select 0 [grphdr/msgid] ,0 [orgnlgrpinfandsts/orgnlmsgid] xml path('fitofipmtstsrp'),root('document')
the result
<document xmlns="urn:iso:std:iso:20022:tech:xsd:001.002.001.04"> <fitofipmtstsrp> <grphdr> <msgid>0</msgid> </grphdr> <orgnlgrpinfandsts> <orgnlmsgid>0</orgnlmsgid> </orgnlgrpinfandsts> </fitofipmtstsrp> </document>
Comments
Post a Comment