Question : Adding an attribute to the xml root using FOR XML PATH syntax

I would like to include an additional attribute at the root level of the xml that is being produced by the statement below.

So that:

ation="http://gtvpmapp02/GoScript/Schema/FrameworkBatch.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

would then become:

ation="http://gtvpmapp02/GoScript/Schema/FrameworkBatch.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" GoScriptName="test">
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
with xmlnamespaces(
	'http://www.w3.org/2001/XMLSchema-instance' as "xsi"
,	'http://gtvpmapp02/GoScript/Schema/FrameworkBatch.xsd' as "noNamespaceSchemaLocation"
)
select isnull(command,'') 'Command', 
isnull(DataType,'') 'DataType', 
isnull(RemoteBaseDirectory,'') 'RemoteBaseDirectory', 
isnull(RemoteFile, '') 'RemoteFile', 
isnull(LocalFile,'') 'LocalFile', 
isnull(LastRemoteFile,'') 'LastRemoteFile',
isnull(LastLocalFile,'') 'LastLocalFile' ,
isnull(LastAttempt,'') 'LastAttempt' ,
isnull(LastSuccess,'') 'LastSuccess' ,
isnull(LastStatus,'') 'LastStatus' 
from gtdev02.industryexposure.dbo.FTPBatch 
where batchfilename = 'VinFTPBatch1.xml'
for xml path('FTP'), Root('FrameworkBatch'), ELEMENTS XSINIL

Answer : Adding an attribute to the xml root using FOR XML PATH syntax

The XSINIL is telling the XML output to create a namespace section / header at the top.

Do not think you can intercept the way you are asking. Have done some like this, but not having both the XMLNAMESPACES and XSINIL at the same time as wanting to get an element...

Have not given up, always enjoy a good challenge, but so far, the automated way it simply not cutting it...

for example, to get the header you are looking for (nearly), I would use :

with xmlnamespaces(
      'http://www.w3.org/2001/XMLSchema-instance' as "xsi"
,      'http://gtvpmapp02/GoScript/Schema/FrameworkBatch.xsd' as "noNamespaceSchemaLocation"
)
select
(
select 'TEST' as 'GoScriptName' for xml raw('FrameworkBatch'),type
)

Now to get the rest to be part of the same path of 'FrameworkBatch' is proving the challenge - normally just add another select, but with the "with xmlnamespaces" it is trying to ecapsulate an entire new node.

Might have to go to a manually produced result (as it code for it - still within SQL).

Will get back - just thought I need to share the fact that I have spent a bit of time on this problem, and haven't given up yet...
Random Solutions  
 
programming4us programming4us