【问题标题】:SQL - FOR XML - Placing nearly same elements next to each otherSQL - FOR XML - 将几乎相同的元素放在一起
【发布时间】: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>

元素 &lt;cac:AccountingSupplierParty&gt; 和 &lt;cac:Party&gt; 不应重复。结构应该是这样的:

<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


    【解决方案1】:

    您需要将其与空列名分开。来确定它的无限行。

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

    输出:

    【讨论】:

    • 对不起,实际上恐怕这不是正确的答案。请检查更新部分中的原始帖子。不过还是非常感谢!
    • 我们需要通过子查询重建您的嵌套 xml,替换 WHERE v3.pk = v2.pk 以关联您在主表上的子查询。
    • 谢谢。我还需要替换&lt;cac:AccountingSupplierParty&gt; 中的命名空间,但这没问题。
    • @dns_nx 只是补充一下:You might read this 或this 以获得一些解释。从我身边 +1...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-11
    • 1970-01-01
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多