加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

SQL Server中子节点中的空缺xml定名空间

发布时间:2021-03-31 07:47:35 所属栏目:编程 来源:网络整理
导读:嗨我必要sql中的以下辅佐: 我必要以这种名目建设一个xml文件 Document xmlns="urn:iso:std:iso:20022:tech:xsd:001.002.001.04" FIToFIPmtStsRpt GrpHdr MsgId/MsgId /GrpHdr OrgnlGrpInfAndSts OrgnlMsgId / /OrgnlGrpInfAndSts /FIToFIPmtStsRpt/document
副问题[/!--empirenews.page--]

嗨我必要sql中的以下辅佐:

我必要以这种名目建设一个xml文件

<Document xmlns="urn:iso:std:iso:20022:tech:xsd:001.002.001.04">
 <FIToFIPmtStsRpt>
  <GrpHdr>
    <MsgId></MsgId>
  </GrpHdr>
  <OrgnlGrpInfAndSts>
    <OrgnlMsgId />
  </OrgnlGrpInfAndSts>
 </FIToFIPmtStsRpt>
</document>

今朝我有一个生涯首要信息的变量,我成立了信息之间(以为grphdr可以多次插入主xml,具有差异的信息)

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 i do some code to fill msgid

然后当我将grphdr添加回主xml时

SET @xml.modify
('declare namespace a= "urn:iso:std:iso:20022:tech:xsd:001.002.001.04";
insert sql:variable("@xmlgrp") 
into (a:Document/a:FIToFIPmtStsRpt)[1]')

select @xml

我必要找出顶级文件名目,但此刻产生的是以下内容

<Document xmlns="urn:iso:std:iso:20022:tech:xsd:001.002.001.04">
 <FIToFIPmtStsRpt>
   <GrpHdr xmlns="">-- i need this xmlns tag out
     <MsgId />
   </GrpHdr>
 </FIToFIPmtStsRpt>
 </Document>

不知怎的,我必要xml中的空xmlns标签.我无法转换为varchar(max)来删除,由于我们的数据库已将变量限定为8000个字符
我的xml可以增添到8000以上.
1个文件中可以有多个grphdr或OrgnlGrpIn??fAndSts

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

必需天生这样的

<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>

这就是我实行插入xml方法的缘故起因.假如任何人能以更好的方法辅佐我,我将不胜谢谢.

办理要领

编辑2:我终于找到了停止一再定名空间的要领.起首建设没有定名空间的嵌套XML,然后插手它:

更新(2017年12月)

现实上,这种办理要领如故没有现实辅佐.定名空间xmlns =“”被视为*内部的全部内容都不在定名空间内…您可以将功效转换为NVARCHAR(MAX)并行使REPLACE来删除xmlns =“”.然后,您可以将字符串从头转换为XML.对微软的羞耻,10(!!)岁的题目(见下面的链接)如故没有办理.请去哪里投票!

DECLARE @lim_Live_Inbound TABLE(lim_msg_id VARCHAR(100),lim_request_transaction_id VARCHAR(100),client_name VARCHAR(100));
INSERT INTO @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);

WITH GrpMsg AS
(
    SELECT DISTINCT lim_msg_id AS MsgId
    FROM @lim_Live_Inbound
)
INSERT INTO @nestedXMLs 
SELECT MsgId,(
        SELECT innerTbl.lim_request_transaction_id AS OrgnlMsgId,innerTbl.client_name AS name
        FROM @lim_Live_Inbound AS innerTbl
        WHERE innerTbl.lim_msg_id=GrpMsg.MsgId
        FOR XML PATH('OrgnlGrpInfAndSts'),TYPE
      ) 
FROM GrpMsg;

WITH XMLNAMESPACES(DEFAULT 'urn:iso:std:iso:20022:tech:xsd:001.002.001.04'),GrpMsg AS
(
    SELECT DISTINCT lim_msg_id AS MsgId
    FROM @lim_Live_Inbound
)
SELECT GrpMsg.MsgId AS [GrpHdr/MsgId],n.nestedXML AS [node()]
FROM GrpMsg
INNER JOIN @nestedXMLs AS n ON GrpMsg.MsgId=n.MsgId
FOR XML PATH('FIToFIPmtStsRp'),ROOT('Document')

功效

<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>

您将行使CAST(REPLACE(CAST(TheXMLHere AS NVARCHAR(MAX)),’xmlns =“”’,”)AS XML)来挣脱错误的空名称空间….

编辑:得当您的样本数据的新要领

这是一再定名空间 – 但这在语法上是正确的,但很烦人(在这里阅读: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),'Mrs Name');

WITH XMLNAMESPACES(DEFAULT 'urn:iso:std:iso:20022:tech:xsd:001.002.001.04'),GrpMsg AS
(
    SELECT DISTINCT lim_msg_id AS MsgId
    FROM @lim_Live_Inbound
)
SELECT MsgId AS [GrpHdr/MsgId],TYPE
      ) 
FROM GrpMsg
FOR XML PATH('FIToFIPmtStsRp'),ROOT('Document')

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读