Reputation: 103
Could you please help me to understand how can I generate XML/HTML with more than one attribute
I have this SQL code
select
[td/@align] = 'center', td = format(GETDATE(),'dd.MM.yyyy'), null
for xml path('tr')
This code returns as its result:
<tr>
<td align="center">16.09.2020</td>
</tr>
and I need
<tr>
<td align="center" style="background-color: red;">16.09.2020</td>
</tr>
Can't find out how to do this...
If I try to use something like this [td/@align/@style]
- SQL is causing an error
Column name 'td/@align/@style' contains an invalid XML identifier as required by FOR XML; '@'(0x0040) is the first character at fault
Upvotes: 1
Views: 171
Reputation: 43646
Are you looking for this:
select 'center' AS [td/@align]
,'background-color: red;' AS [td/@style]
,format(GETDATE(),'dd.MM.yyyy') AS [td]
for xml path('tr')
it yields this:
<tr>
<td align="center" style="background-color: red;">16.09.2020</td>
</tr>
You can think of one row columns as xml tag value and attributes, which are grouped using the alias AS
. So, for more attributes, just add new value with the corresponding alias - td/@...
.
Upvotes: 5