I know how to do it in the simplest scenario, e.g.
DECLARE @commaSeparatedValues NVARCHAR(MAX)
DECLARE @xml XML = N'<id>1</id>
<name>test</name>
<istest>1</istest>'
;WITH nodes AS
(
SELECT Tbl.Col.value('.', 'nvarchar(max)') as Value
FROM @xml.nodes('*/text()') Tbl(Col)
),
prepareStrings
AS
(
SELECT IIF(ISNUMERIC(n.value) = 1, n.Value, '''' + n.Value + '''') AS Value
FROM nodes n
)
SELECT @commaSeparatedValues = CASE WHEN @commaSeparatedValues IS NULL THEN s.Value ELSE @commaSeparatedValues + ',' + s.value END
FROM prepareStrings s
SELECT @commaSeparatedValues as csv
This works perfectly. Problem arises when I want to parse this way the following xml data. I have problems with writing the proper query.
DECLARE @xml XML = N'
<e>
<id>1</id>
<name>test</name>
<istest>1</istest>
</e>
<e>
<id>2</id>
<name>test2</name>
<istest>0</istest>
</e>
'
I can get the elements row by row by using
select Tbl.col.query('.') as [xml]
from @xml.nodes('e') Tbl(col)
What I don't know is how to move forward with this. Don't know how to use this query and now querying the [xml] column.