Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL-FOR XML-将几乎相同的元素彼此相邻放置_Sql_Sql Server_Xml_For Xml - Fatal编程技术网

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

SQL-FOR XML-将几乎相同的元素彼此相邻放置,sql,sql-server,xml,for-xml,Sql,Sql Server,Xml,For Xml,我有一个存储过程来根据UBL-TR-2.1标准生成XML文档。 我刚刚学习了如何向查询中的元素添加属性(即schemeID=“VKN”)。 现在我有一个不同的问题: UBL-TR-2.1标准定义使用cbc:ID放置3次cac:partyiIdentification,但不同的schemeID属性,如您在此处所见: ... <cac:PartyIdentification> <cbc:ID schemeID="VKN">1190538652</cbc:ID>

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

UBL-TR-2.1
标准定义使用
cbc:ID
放置3次
cac:partyiIdentification
,但不同的
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>
...
但这会抛出一个错误(这里是德语的原始信息):

Msg 6852,第16级,状态1,程序sp_RTIR_TR_Export_to_XML,第行 85[批次起始行7]模具属性 cac:AccountingSupplierParty/cac:Party/cac:PartyIdentification/cbc:ID/@schemeID-Spalte XML路径无效的XML层次结构中的darf 属性元素folgen

在英语中是这样的:

<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>
Msg 6852,第16级,状态1,程序sp_RTIR_TR_Export_to_XML,第行 85[批处理起始行7]受信任的属性 cac:AccountingSupplierParty/cac:Party/cac:PartyIdentification/cbc:ID/@schemeID FOR XML路径中的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>

622171
0119053865200011
不应重复元素
。结构应如下所示:

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

...
1190538652
622171
0119053865200011
...

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

        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以关联主表上的子查询。谢谢。我还需要替换
中的名称空间,但这不是问题。@dns\u nx只是为了补充这一点:或者为了一些解释+从我这边。。。