【发布时间】:2020-02-14 00:43:31
【问题描述】:
我有一个基于UBL-TR-2.1 标准生成XML 文档的存储过程。
我刚刚学会了如何向查询中的元素添加属性(即schemeID="VKN")。
现在我有一个不同的问题:
UBL-TR-2.1 标准定义将 3 次 cac:PartyIdentification 与 cbc:ID,但不同的 schemeID 属性如您在此处看到的:
...
<cac:PartyIdentification>
<cbc:ID schemeID="VKN">1190538652</cbc:ID>
</cac:PartyIdentification>
<cac:PartyIdentification>
<cbc:ID schemeID="TICARETSICILNO">622171</cbc:ID>
</cac:PartyIdentification>
<cac:PartyIdentification>
<cbc:ID schemeID="MERSISNO">0119053865200011</cbc:ID>
</cac:PartyIdentification>
...
所以我在sql 查询中尝试了这个(我只发布了查询的相关部分):
SELECT
@XMLData = xmldat.xmldataCol
FROM
(
SELECT
(
SELECT
....
'VKN' as 'cac:AccountingSupplierParty/cac:Party/cac:PartyIdentification/cbc:ID/@schemeID',
v2.TAXNRM as 'cac:AccountingSupplierParty/cac:Party/cac:PartyIdentification/cbc:ID',
'TICARETSICILNO' as 'cac:AccountingSupplierParty/cac:Party/cac:PartyIdentification/cbc:ID/@schemeID',
'622171' as 'cac:AccountingSupplierParty/cac:Party/cac:PartyIdentification/cbc:ID',
'MERSISNO' as 'cac:AccountingSupplierParty/cac:Party/cac:PartyIdentification/cbc:ID/@schemeID',
'0119053865200011' as 'cac:AccountingSupplierParty/cac:Party/cac:PartyIdentification/cbc:ID',
...
FROM
vorgang2 (nolock) v2
FOR XML PATH('') , ROOT('Invoice')
) as xmldataCol
) as xmldat
但这会引发错误(这里是德语的原始消息):
消息 6852,级别 16,状态 1,过程 sp_RTIR_TR_Export_to_XML,行 85 [Batch Start Line 7] 模具属性 cac:AccountingSupplierParty/cac:Party/cac:PartyIdentification/cbc:ID/@schemeID-Spalte darf in der XML-Hierarchie in FOR XML PATH nicht auf ein nicht attributzentriertes gleichgeordnetes Element folgen.
英文是这样的:
消息 6852,级别 16,状态 1,过程 sp_RTIR_TR_Export_to_XML,行 85 [Batch Start Line 7] 属性信任 cac:AccountingSupplierParty/cac:Party/cac:PartyIdentification/cbc:ID/@schemeID FOR XML PATH 中 XML 层次结构中的列不得跟随 非属性信任的父元素。
所以问题似乎是,元素几乎相同。这里遇到了sql 将它们放在一起的麻烦。
这个问题有什么解决办法吗?
非常感谢!
更新
在两者之间添加null 的解决方案无法正常工作,因为我在xml 中得到了这个结果:
<cac:AccountingSupplierParty>
<cac:Party>
<cac:PartyIdentification>
<cbc:ID schemeID="VKN"/>
</cac:PartyIdentification>
</cac:Party>
</cac:AccountingSupplierParty>
<cac:AccountingSupplierParty>
<cac:Party>
<cac:PartyIdentification>
<cbc:ID schemeID="TICARETSICILNO">622171</cbc:ID>
</cac:PartyIdentification>
</cac:Party>
</cac:AccountingSupplierParty>
<cac:AccountingSupplierParty>
<cac:Party>
<cac:PartyIdentification>
<cbc:ID schemeID="MERSISNO">0119053865200011</cbc:ID>
</cac:PartyIdentification>
</cac:Party>
</cac:AccountingSupplierParty>
元素 <cac:AccountingSupplierParty> 和 <cac:Party> 不应重复。结构应该是这样的:
<cac:AccountingSupplierParty>
<cac:Party>
...
<cac:PartyIdentification>
<cbc:ID schemeID="VKN">1190538652</cbc:ID>
</cac:PartyIdentification>
<cac:PartyIdentification>
<cbc:ID schemeID="TICARETSICILNO">622171</cbc:ID>
</cac:PartyIdentification>
<cac:PartyIdentification>
<cbc:ID schemeID="MERSISNO">0119053865200011</cbc:ID>
</cac:PartyIdentification>
...
</cac:Party>
</cac:AccountingSupplierParty>
【问题讨论】:
标签: sql sql-server xml for-xml