我使用这个网站很长一段时间,很多次它帮助我解决了各种问题。 这次我被困住了。我尝试将一个复杂的xml导入到ms-sql表中。
<?xml version='1.0' encoding='UTF-8'?>
<S2SCTScf:SCTScfBlkCredTrf xmlns="urn:S2SCTScf:xsd:$SCTScfBlkCredTrf" xmlns:S2SCTScf="urn:S2SCTScf:xsd:$SCTScfBlkCredTrf" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:S2SCTScf:xsd:$SCTScfBlkCredTrf SCTScfBlkCredTrf.xsd">
<S2SCTScf:SndgInst>XXXXXXXX</S2SCTScf:SndgInst>
<S2SCTScf:RcvgInst>YYYYYYYY</S2SCTScf:RcvgInst>
<S2SCTScf:SrvcId>SCT</S2SCTScf:SrvcId>
<S2SCTScf:TstCode>P</S2SCTScf:TstCode>
<S2SCTScf:FType>SCF</S2SCTScf:FType>
<S2SCTScf:FileRef>AAAAAAAAAAAAAAAAAAA</S2SCTScf:FileRef>
<S2SCTScf:RoutingInd>IND</S2SCTScf:RoutingInd>
<S2SCTScf:FileBusDt>2016-11-01</S2SCTScf:FileBusDt>
<S2SCTScf:FileCycleNo>01</S2SCTScf:FileCycleNo>
<S2SCTScf:FIToFICstmrCdtTrf xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02">
<GrpHdr>
<MsgId>111111111111111111</MsgId>
<CreDtTm>2016-11-01T15:45:11.0Z</CreDtTm>
<NbOfTxs>11</NbOfTxs>
<TtlIntrBkSttlmAmt Ccy="EUR">111111</TtlIntrBkSttlmAmt>
<IntrBkSttlmDt>2016-11-01</IntrBkSttlmDt>
<SttlmInf>
<SttlmMtd>CLRG</SttlmMtd>
<ClrSys>
<Prtry>ST2</Prtry>
</ClrSys>
</SttlmInf>
<InstgAgt>
<FinInstnId>
<BIC>XXXXXXXX</BIC>
</FinInstnId>
</InstgAgt>
<InstdAgt>
<FinInstnId>
<BIC>XXXXXXXX</BIC>
</FinInstnId>
</InstdAgt>
</GrpHdr>
<CdtTrfTxInf>
<PmtId>
<EndToEndId>NOTPROVIDED</EndToEndId>
<TxId>XXXXXXXXXXXXXXXXXXXXXXXXXX</TxId>
</PmtId>
<PmtTpInf>
<SvcLvl>
<Cd>SEPA</Cd>
</SvcLvl>
</PmtTpInf>
<IntrBkSttlmAmt Ccy="XXX">1.00</IntrBkSttlmAmt>
<ChrgBr>SLEV</ChrgBr>
<Dbtr>
<Nm>MXXXXXX XXXXXXX</Nm>
<PstlAdr>
<Ctry>XX</Ctry>
<AdrLine>XXXXXXXXXXXXXXXXXXXXXXXXXXX</AdrLine>
</PstlAdr>
</Dbtr>
<DbtrAcct>
<Id>
<IBAN>XXXXXXXXXXXXXXXXXXXX</IBAN>
</Id>
</DbtrAcct>
<DbtrAgt>
<FinInstnId>
<BIC>XXXXXXXXXXX</BIC>
</FinInstnId>
</DbtrAgt>
<CdtrAgt>
<FinInstnId>
<BIC>XXXXXXXXXX</BIC>
</FinInstnId>
</CdtrAgt>
<Cdtr>
<Nm>XXXXXXXXXXXXXXXXXXXXXXX</Nm>
</Cdtr>
<CdtrAcct>
<Id>
<IBAN>XXXXXXXXXXXXXXXXXXXXXXX</IBAN>
</Id>
</CdtrAcct>
<RmtInf>
<Ustrd>XXXXXXXXXXXXXXXXXXXXXXX</Ustrd>
</RmtInf>
</CdtTrfTxInf>
<CdtTrfTxInf>
<PmtId>
<EndToEndId>NOTPROVIDED</EndToEndId>
<TxId>XXXXXXXXXXXXXXXXXXXXXXXXXXXX</TxId>
</PmtId>
<PmtTpInf>
<SvcLvl>
<Cd>SEPA</Cd>
</SvcLvl>
</PmtTpInf>
<IntrBkSttlmAmt Ccy="XXX">1.00</IntrBkSttlmAmt>
<ChrgBr>SLEV</ChrgBr>
<Dbtr>
<Nm>XXXXXXXXXXXXXXXXX</Nm>
<PstlAdr>
<Ctry>XX</Ctry>
<AdrLine>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</AdrLine>
</PstlAdr>
</Dbtr>
<DbtrAcct>
<Id>
<IBAN>XXXXXXXXXXXXXXXXXXXXXXXXX</IBAN>
</Id>
</DbtrAcct>
<DbtrAgt>
<FinInstnId>
<BIC>XXXXXXXXXXXXXXXXXXXXXXXX</BIC>
</FinInstnId>
</DbtrAgt>
<CdtrAgt>
<FinInstnId>
<BIC>XXXXXXXXXXXXXXXXXXXXXXXX</BIC>
</FinInstnId>
</CdtrAgt>
<Cdtr>
<Nm>XXXXXXXXXXXXXXXXXXXXXXXX</Nm>
</Cdtr>
<CdtrAcct>
<Id>
<IBAN>XXXXXXXXXXXXXXXXXXXXXXXXXX</IBAN>
</Id>
</CdtrAcct>
<RmtInf>
<Ustrd>XXXXXXXXXXXXXXXXXXXXXXXXXXXXX</Ustrd>
</RmtInf>
</CdtTrfTxInf>
</S2SCTScf:FIToFICstmrCdtTrf>
</S2SCTScf:SCTScfBlkCredTrf>
&#13;
我尝试OPENXML和XQuery函数,但我在声明和使用命名空间(或命名空间)方面遇到了一些问题。我不熟悉这种复杂的xml和namespaceuri。我需要一个ideea来获取数据到表格。我已成功使用更简单的xml,即使有一个命名空间。我已经手动删除了前11行并且选择的波纹管非常精细......
DECLARE @XML AS XML, @hDoc AS INT, @SQL NVARCHAR (MAX)
SELECT @XML = XMLData FROM XMLwithOpenXML where id=6 --this is the xml without first 11 lines
EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML
SELECT * FROM OPENXML(@hDoc, 'FIToFICstmrCdtTrf/CdtTrfTxInf')
WITH
(
CCY [varchar](100) 'IntrBkSttlmAmt/@Ccy',
IntrBkSttlmAmt [varchar](100) 'IntrBkSttlmAmt',
TxId [varchar](100) 'PmtId/TxId',
EndToEndId [varchar](100) 'PmtId/EndToEndId',
ChrgBr [varchar](100) 'ChrgBr'
--etc
)
EXEC sp_xml_removedocument @hDoc
GO
&#13;
答案 0 :(得分:1)
首先发表一些评论
FROM OPENXML
已过时,不应再使用(罕见例外退出)<?xml version=''1.0'' encoding=''UTF-8''?>
包含显式编码。这会迫使你走VARCHAR
- 路径,这与不是普通的拉丁字符有关。最好用utf-16
替换它,然后转到NVARCHAR
- 路径。在这种情况下,您必须在XML文字前面设置“N”。*:
每个元素。S2SCTScf:FIToFICstmrCdtTrf
定义了一个新的默认命名空间。我将其与innerDeflt
混淆了。这是变量声明
DECLARE @xml XML=
'<?xml version=''1.0'' encoding=''UTF-8''?>
<S2SCTScf:SCTScfBlkCredTrf xmlns="urn:S2SCTScf:xsd:$SCTScfBlkCredTrf" xmlns:S2SCTScf="urn:S2SCTScf:xsd:$SCTScfBlkCredTrf" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:S2SCTScf:xsd:$SCTScfBlkCredTrf SCTScfBlkCredTrf.xsd">
<S2SCTScf:SndgInst>XXXXXXXX</S2SCTScf:SndgInst>
<S2SCTScf:RcvgInst>YYYYYYYY</S2SCTScf:RcvgInst>
<S2SCTScf:SrvcId>SCT</S2SCTScf:SrvcId>
<S2SCTScf:TstCode>P</S2SCTScf:TstCode>
<S2SCTScf:FType>SCF</S2SCTScf:FType>
<S2SCTScf:FileRef>AAAAAAAAAAAAAAAAAAA</S2SCTScf:FileRef>
<S2SCTScf:RoutingInd>IND</S2SCTScf:RoutingInd>
<S2SCTScf:FileBusDt>2016-11-01</S2SCTScf:FileBusDt>
<S2SCTScf:FileCycleNo>01</S2SCTScf:FileCycleNo>
<S2SCTScf:FIToFICstmrCdtTrf xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02">
<GrpHdr>
<MsgId>111111111111111111</MsgId>
<CreDtTm>2016-11-01T15:45:11.0Z</CreDtTm>
<NbOfTxs>11</NbOfTxs>
<TtlIntrBkSttlmAmt Ccy="EUR">111111</TtlIntrBkSttlmAmt>
<IntrBkSttlmDt>2016-11-01</IntrBkSttlmDt>
<SttlmInf>
<SttlmMtd>CLRG</SttlmMtd>
<ClrSys>
<Prtry>ST2</Prtry>
</ClrSys>
</SttlmInf>
<InstgAgt>
<FinInstnId>
<BIC>XXXXXXXX</BIC>
</FinInstnId>
</InstgAgt>
<InstdAgt>
<FinInstnId>
<BIC>XXXXXXXX</BIC>
</FinInstnId>
</InstdAgt>
</GrpHdr>
<CdtTrfTxInf>
<PmtId>
<EndToEndId>NOTPROVIDED</EndToEndId>
<TxId>XXXXXXXXXXXXXXXXXXXXXXXXXX</TxId>
</PmtId>
<PmtTpInf>
<SvcLvl>
<Cd>SEPA</Cd>
</SvcLvl>
</PmtTpInf>
<IntrBkSttlmAmt Ccy="XXX">1.00</IntrBkSttlmAmt>
<ChrgBr>SLEV</ChrgBr>
<Dbtr>
<Nm>MXXXXXX XXXXXXX</Nm>
<PstlAdr>
<Ctry>XX</Ctry>
<AdrLine>XXXXXXXXXXXXXXXXXXXXXXXXXXX</AdrLine>
</PstlAdr>
</Dbtr>
<DbtrAcct>
<Id>
<IBAN>XXXXXXXXXXXXXXXXXXXX</IBAN>
</Id>
</DbtrAcct>
<DbtrAgt>
<FinInstnId>
<BIC>XXXXXXXXXXX</BIC>
</FinInstnId>
</DbtrAgt>
<CdtrAgt>
<FinInstnId>
<BIC>XXXXXXXXXX</BIC>
</FinInstnId>
</CdtrAgt>
<Cdtr>
<Nm>XXXXXXXXXXXXXXXXXXXXXXX</Nm>
</Cdtr>
<CdtrAcct>
<Id>
<IBAN>XXXXXXXXXXXXXXXXXXXXXXX</IBAN>
</Id>
</CdtrAcct>
<RmtInf>
<Ustrd>XXXXXXXXXXXXXXXXXXXXXXX</Ustrd>
</RmtInf>
</CdtTrfTxInf>
<CdtTrfTxInf>
<PmtId>
<EndToEndId>NOTPROVIDED</EndToEndId>
<TxId>XXXXXXXXXXXXXXXXXXXXXXXXXXXX</TxId>
</PmtId>
<PmtTpInf>
<SvcLvl>
<Cd>SEPA</Cd>
</SvcLvl>
</PmtTpInf>
<IntrBkSttlmAmt Ccy="XXX">1.00</IntrBkSttlmAmt>
<ChrgBr>SLEV</ChrgBr>
<Dbtr>
<Nm>XXXXXXXXXXXXXXXXX</Nm>
<PstlAdr>
<Ctry>XX</Ctry>
<AdrLine>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</AdrLine>
</PstlAdr>
</Dbtr>
<DbtrAcct>
<Id>
<IBAN>XXXXXXXXXXXXXXXXXXXXXXXXX</IBAN>
</Id>
</DbtrAcct>
<DbtrAgt>
<FinInstnId>
<BIC>XXXXXXXXXXXXXXXXXXXXXXXX</BIC>
</FinInstnId>
</DbtrAgt>
<CdtrAgt>
<FinInstnId>
<BIC>XXXXXXXXXXXXXXXXXXXXXXXX</BIC>
</FinInstnId>
</CdtrAgt>
<Cdtr>
<Nm>XXXXXXXXXXXXXXXXXXXXXXXX</Nm>
</Cdtr>
<CdtrAcct>
<Id>
<IBAN>XXXXXXXXXXXXXXXXXXXXXXXXXX</IBAN>
</Id>
</CdtrAcct>
<RmtInf>
<Ustrd>XXXXXXXXXXXXXXXXXXXXXXXXXXXXX</Ustrd>
</RmtInf>
</CdtTrfTxInf>
</S2SCTScf:FIToFICstmrCdtTrf>
</S2SCTScf:SCTScfBlkCredTrf>';
这是查询:首先声明命名空间。您的节点是1:1的纯结构,因此只需在元素名称后面添加元素名称XPath
即可读取它们。只有<CdtTrfTxInf>
出现两次需要1:n-approach
APPLY
和.nodes()
。
在我的示例中,您可以获得XML中隐藏的任何类型数据的模板。其余的由你决定。
WITH XMLNAMESPACES(DEFAULT 'urn:S2SCTScf:xsd:$SCTScfBlkCredTrf'
,'urn:S2SCTScf:xsd:$SCTScfBlkCredTrf' as S2SCTScf
,'http://www.w3.org/2001/XMLSchema-instance' AS xsi
,'urn:S2SCTScf:xsd:$SCTScfBlkCredTrf SCTScfBlkCredTrf.xsd' AS schemaLocation
,'urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02' AS innerDeflt)
SELECT rt.value(N'(S2SCTScf:SndgInst)[1]','nvarchar(max)') AS SndgInst
,rt.value(N'(S2SCTScf:RcvgInst)[1]','nvarchar(max)') AS RcvgInst
--more like this
,rt.value(N'(S2SCTScf:FIToFICstmrCdtTrf/innerDeflt:GrpHdr/innerDeflt:MsgId)[1]','nvarchar(max)') AS MsgId
,rt.value(N'(S2SCTScf:FIToFICstmrCdtTrf/innerDeflt:GrpHdr/innerDeflt:CreDtTm)[1]','datetime') AS CreDtTm
--more like this
,rt.value(N'(S2SCTScf:FIToFICstmrCdtTrf/innerDeflt:GrpHdr/innerDeflt:TtlIntrBkSttlmAmt/@Ccy)[1]','nvarchar(max)') AS TtlIntrBkSttlmAmt_Ccy
,rt.value(N'(S2SCTScf:FIToFICstmrCdtTrf/innerDeflt:GrpHdr/innerDeflt:TtlIntrBkSttlmAmt)[1]','int') AS TtlIntrBkSttlmAmt
--all nodes are 1:1, just "more of the same"
--But CdtTrfTxInf is there twice, therefore the call to OUTER APPLY rt.nodes()
,cti.value(N'(innerDeflt:PmtId/innerDeflt:EndToEndId)[1]','nvarchar(max)') AS EndToEndId
--all the rest is following the same schema...
FROM @xml.nodes(N'S2SCTScf:SCTScfBlkCredTrf') AS A(rt) --root
OUTER APPLY rt.nodes(N'S2SCTScf:FIToFICstmrCdtTrf/innerDeflt:CdtTrfTxInf') AS B(cti) --CdtTrfTxInf
部分结果(字幕移位......)
SndgInst RcvgInst MsgId CreDtTm TtlIntrBkSttlmAmt_Ccy TtlIntrBkSttlmAmt EndToEndId
XXXXXXXX YYYYYYYY 111111111111111111 2016-11-01 15:45:11.000 EUR 111111 NOTPROVIDED
XXXXXXXX YYYYYYYY 111111111111111111 2016-11-01 15:45:11.000 EUR 111111 NOTPROVIDED