使用SQL从HTML标记构建有效的HTML

时间:2012-02-07 12:06:44

标签: sql sql-server tsql

我有一个包含HTML标记的表。 我只想创建一个使用此表的HTML FORM是HTML FOrmat 例如。

ID Tags
-- ----
1  Html
2  Head
3  Title
4  Meta
5  Body
6  Font

结果应为

ID HTML                                 
-- ------------------------------------------------------------------------------------
1  <Html> <Head> <Title></Title> <Meta></Meta> </Head><Body> <Font></Font></Body</Html>

1 个答案:

答案 0 :(得分:1)

declare @t table(id int, tags varchar(50))
insert into @t values 
        (1, 'Html'),
        (2, 'Head'),
        (3, 'Title'),
        (4, 'Meta'),
        (5, 'Body'),
        (6, 'Font')

;with Tags1 as
(
    select xml1 = (select '<' + tags + '>' from @t for xml path (''))
)
,Tags2 as
(
    select xml2 = (select '</' + tags + '>' from @t order by id desc for xml path (''))
)
select replace(replace(Tags1.xml1 + Tags2.xml2,'&lt;','<'),'&gt;','>')
from Tags1, Tags2