I have a table of data that I need to create a .xml file from in a specific format.
CREATE TABLE [dbo].[XML_TABLE](
[ProductID] [nvarchar](25) NULL,
[Name] [nvarchar](25) NULL,
[ParentID] [nvarchar](51) NULL,
[AttributeType] [nvarchar](10) NULL,
[AttributeID] [nvarchar](255) NULL,
[AttributeValue] [nvarchar](4000) NULL
) ON [PRIMARY]
GO
Here are some values to insert:
INSERT INTO WORK.DBO.XML_TABLE([ProductID], [Name], [ParentID],
[AttributeType], [AttributeID], [AttributeValue])
VALUES('1557505','DVSCTV-
HT','39122206_5183','Value','IDW_EnvironmentalConditions','Indoor use
only'),
('1557505','DVSCTV-
HT','39122206_5183','Value','IDW_Enclosure','Plastic'),
('1557505','DVSCTV-
HT','39122206_5183','MultiValue','IDW_Color','Hot')
Here is the current query I have but is not correct (close though):
SELECT (
SELECT *
FROM [WORK].[dbo].[XML_TABLE]
FOR XML PATH('Product'), TYPE, ROOT('Products')
).query('<XmlFormat version="1.0">
<Values>
{
for $x in /Products/Product[AttributeType="Value"]
return <Value AttributeID="{data($x/AttributeID)}">{data($x/AttributeValue)}</Value>
}
<MultiValue>
{
for $x in /Products/Product[AttributeType="MultiValue"]
return <MultiValue AttributeID="{data($x/AttributeID)}">{data($x/AttributeValue)}</MultiValue>
}
</MultiValue>
</Values>
</XmlFormat>');
This gives me this output:
<XmlFormat version="1.0">
<Values>
<Value AttributeID="IDW_EnvironmentalConditions">Indoor use only</Value>
<Value AttributeID="IDW_Enclosure">Plastic</Value>
<MultiValue>
<MultiValue AttributeID="IDW_Color">Hot</MultiValue>
</MultiValue>
</Values>
</XmlFormat>
But I need this output:
<Products>
<Product ID="1557505" UserTypeID="CatalogNumber" ParentID="12345678_0123">
<Name>DVSCTV-HT</Name>
<Values>
<Value AttributeID="IDW_EnvironmentalConditions">Indoor use only</Value>
<Value AttributeID="IDW_Enclosure">Plastic</Value>
<MutliValue AttributeID="IDW_Color">
<Value>Hot</Value>
</MultiValue>
</Values>
</Product>
</Products>
How I get there from my current query I don't have a clue. I am trying to get this imported into a 3rd party application and the format is what they gave me to format the file to. Any help would be appreciated. This is the first time I have worked with this. I spent 2 days figuring out the physical file creation with SSIS and C# and finally got that working but the formatting of the file...I've been banging my head against the wall for several days now.