I currently have the following code creating XML under parent nodes named PARTY. Inside the PARTY nodes is another node called ROLE. My question is how can I replace the current ROLE node which just says ROLE with my [SequenceNr] variable inside the xquery.
I had attempted doing a replace in the @OutputXml but that didn't work, and I think there should be an easy way in the xquery that I do not know about.
Current code:
WITH [PartyNodes] AS
(
SELECT (
SELECT [dbo].[XmlA](@Id),
[dbo].[XmlB](@Id)
FOR XML PATH(''),TYPE
) AS [AllTogether]
)
,[NumberedSequences] AS
(
SELECT 'PARTY' + CONVERT(NVARCHAR, ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) + 10) + '_ROLE1' AS [PartySequenceNr], -- party label
ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) + 10 AS [SequenceNr], -- party sequence
[T].[Party].[query]('.') AS [TheNode]
FROM [PartyNodes]
CROSS APPLY [AllTogether].[nodes]('/PARTY') AS T(Party)
)
SELECT @OutputXml = ( SELECT [TheNode].[query]('let $p:=/PARTY[1]
let $lbl:=sql:column("PartySequenceNr")
let $nr:=sql:column("SequenceNr")
return
<PARTY SequenceNumber="{$nr}" xlink_label="{$lbl}" >
{$p/*}
</PARTY>'
)
FROM [NumberedSequences]
FOR XML PATH(''),ROOT('PARTIES')
);
Current output:
<PARTY SequenceNumber="1" xlink:label="PARTY1_ROLE1">
<INDIVIDUAL>
<NAME>
<FullName>Test</FullName>
</NAME>
</INDIVIDUAL>
<ROLES>
<ROLE>
<A>Test</A>
</ROLE>
</ROLES>
</PARTY>
Desired output:
<PARTY SequenceNumber="1" xlink:label="PARTY1_ROLE1">
<INDIVIDUAL>
<NAME>
<FullName>Test</FullName>
</NAME>
</INDIVIDUAL>
<ROLES>
<ROLE SequenceNumber="1" xlink:label="PARTY1_ROLE1">>
<A>Test</A>
</ROLE>
</ROLES>
</PARTY>