gpt4 book ai didi

xml - 如何给同级的xml节点分配ID

转载 作者:数据小太阳 更新时间:2023-10-29 01:58:09 27 4
gpt4 key购买 nike

我正在尝试修改第三方 xml,以便所有元素都具有来自 T-SQL 的 ID。

这是原始的 xml(部分):

<Tables>
<Table Type="LineItem">
<TableRow>
<Field Name="LI_NominalCode" Type="wd_lit_nominalcode">244234</Field>
<Field Name="LI_NominalDesc" Type="lit_nominaldesc">RENT RECEIVABLE - INTERNAL</Field>
<Field Name="LI_Account" Type="lit_wd_account" />
<Field Name="LI_AccountDesc" Type="lit_wd_accountdesc" />
<Field Name="LI_SecondAccount" Type="lit_wd_2ndaccount" />
<Field Name="LI_SecondAccountDesc" Type="lit_wd_2ndaccountdesc" />
<Field Name="LI_NetValue" Type="lit_vatexcludedamount">4522.89</Field>
<Field Name="LI_EnergyUsage" Type="wd_energyusage">56666</Field>
<Field Name="LI_EnergyType" Type="wd_energytype">ELECTRICITY</Field>
</TableRow>
<TableRow>
<Field Name="LI_NominalCode" Type="wd_lit_nominalcode">150021</Field>
<Field Name="LI_NominalDesc" Type="lit_nominaldesc">Rent Building 1</Field>
<Field Name="LI_Account" Type="lit_wd_account" />
<Field Name="LI_AccountDesc" Type="lit_wd_accountdesc" />
<Field Name="LI_SecondAccount" Type="lit_wd_2ndaccount" />
<Field Name="LI_SecondAccountDesc" Type="lit_wd_2ndaccountdesc" />
<Field Name="LI_NetValue" Type="lit_vatexcludedamount">456.37</Field>
<Field Name="LI_EnergyUsage" Type="wd_energyusage">2805.00</Field>
<Field Name="LI_EnergyType" Type="wd_energytype">ELECTRICITY</Field>
</TableRow>
<TableRow>
<Field Name="LI_NominalCode" Type="wd_lit_nominalcode">2342341</Field>
<Field Name="LI_NominalDesc" Type="lit_nominaldesc">Rent Building 2</Field>
<Field Name="LI_Account" Type="lit_wd_account" />
<Field Name="LI_AccountDesc" Type="lit_wd_accountdesc" />
<Field Name="LI_SecondAccount" Type="lit_wd_2ndaccount" />
<Field Name="LI_SecondAccountDesc" Type="lit_wd_2ndaccountdesc" />
<Field Name="LI_NetValue" Type="lit_vatexcludedamount">355</Field>
<Field Name="LI_EnergyUsage" Type="wd_energyusage">6900</Field>
<Field Name="LI_EnergyType" Type="wd_energytype">ELECTRICITY</Field>
</TableRow>
</Table>
<Table Type="BankAccountTable" />
<Table Type="VATTable" />
</Tables>

如您所见,<Table> 元素没有 ID,因此在流程的后期很难识别它们。

我想创建一个循环来遍历所有 <Table> 元素并执行如下代码片段:

set @xml.modify('
insert attribute ID {sql:variable("@idString")}
into (/Documents/Document/Invoice/Tables/Table[@Type="LineItem"]/TableRow)[sql:variable("@id")]')

问题出在最后一个sql变量上,错误是:

XQuery [modify()]: Only 'http://www.w3.org/2001/XMLSchema#decimal?', 'http://www.w3.org/2001/XMLSchema#boolean?' or 'node()*' expressions allowed as predicates, found 'xs:string ?'

这很好用,但我不想总是更改同一行(第 1 行)。

set @xml.modify('
insert attribute ID {sql:variable("@idString")}
into (/Documents/Document/Invoice/Tables/Table[@Type="LineItem"]/TableRow)[1]')

顺便说一下,如果我使用 int 变量而不是这样的字符串:

set @xml.modify('insert attribute ID {sql:variable("@idString")} 
into (/Documents/Document/Invoice/Tables/Table[@Type="LineItem"]/TableRow)[sql:variab‌​le("@idInt")]')`

我收到另一个错误:

XQuery [modify()]: The target of 'insert' must be a single node, found 'element(TableRow,xdt:untyped) *

最佳答案

您可以将变量 @id 与函数 position() 进行比较。

declare @id int
declare @RowCount int
select @RowCount = @xml.value('count(/Tables/Table[@Type="LineItem"]/TableRow)', 'int')

set @id = 1

while @id <= @RowCount
begin
set @xml.modify('
insert attribute ID {sql:variable("@id")}
into (/Tables/Table[@Type="LineItem"]/TableRow[position()=sql:variable("@id")])[1]')

set @id = @id + 1
end

关于xml - 如何给同级的xml节点分配ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13274572/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com