SQL - FOR XML - 将几乎相同的元素彼此相邻放置

SQL - FOR XML - Placing nearly same elements next to each other

我有一个存储过程可以根据 UBL-TR-2.1 标准生成 XML 文档。 我刚刚学习了如何向查询中的元素添加属性(即 schemeID="VKN")。 现在我有一个不同的问题:

UBL-TR-2.1 标准定义用 cbc:ID 放置 3 次 cac:PartyIdentification,但不同的 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 查询中尝试了这个(我 post 只有查询的相关部分):

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

但这会引发错误(此处为德语原始消息):

Msg 6852, Level 16, State 1, Procedure sp_RTIR_TR_Export_to_XML, Line 85 [Batch Start Line 7] Die attributzentrierte 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.

英文是这样的:

Msg 6852, Level 16, State 1, Procedure sp_RTIR_TR_Export_to_XML, Line 85 [Batch Start Line 7] The attribute-trusted cac:AccountingSupplierParty/cac:Party/cac:PartyIdentification/cbc:ID/@schemeID column in the XML hierarchy in FOR XML PATH must not follow a non-attribute-trusted parental element.

所以问题似乎是,元素几乎相同。将它们并排放置时遇到了 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>

您需要用空列名分隔它。确定其无限行。

        SELECT      
            (SELECT            
                'VKN'               as 'cac:PartyIdentification/cbc:ID/@schemeID',
                 v3.TAXNRM        as 'cac:PartyIdentification/cbc:ID',
                null,
                'TICARETSICILNO'    as 'cac:PartyIdentification/cbc:ID/@schemeID',
                '622171'            as 'cac:PartyIdentification/cbc:ID',
                null,
                'MERSISNO'          as 'cac:PartyIdentification/cbc:ID/@schemeID',
                '0119053865200011'  as 'cac:PartyIdentification/cbc:ID'

             FROM 
             vorgang2(nolock) v3 
             WHERE v3.pk = v2.pk FOR XML path('cac:Party')
                    ,root('cac:AccountingSupplierParty')
                    ,type)

        FROM 
            vorgang2(nolock) v2

        FOR XML PATH('') , ROOT('Invoice') 

输出: