0

I have a table which contains HTML tag . I just want to create a HTML FORM using this table is HTML FOrmat for eg.

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

Result should be

ID HTML                                 
-- ------------------------------------------------------------------------------------
1  <Html> <Head> <Title></Title> <Meta></Meta> </Head><Body> <Font></Font></Body</Html>
5
  • What have you tried? Also, I really struggle to imagine a situation where there isn't a better way of achieving what you want. Why exactly do you need to get a form this way? Commented Feb 7, 2012 at 12:12
  • 1
    Where is a form involved? of those tags only FONT is valid within a form. Special case logic would be needed as some of those tags nest within others and there is no indication of that in the table data, are those 6 elements always present with the same ID? Your going to need to expand your question for a better answer Commented Feb 7, 2012 at 12:15
  • 1
    I work on a system that generates html from the database. Frankly the entire system is insane. Honestly - don't do it. How are you going to style the output for instance. Attributes?? Inner Text. My head hurts. If you must do this then generate the content as XML (like the answer below) then transform it with XSTL. I'm not even convinced that this is a good idea though. Commented Feb 7, 2012 at 12:29
  • It was a puzzle on a sql puzzle site.. I was curious about it so just posted it Commented Feb 7, 2012 at 12:40
  • Without additional information for closing tags the results will be ambiguous, e.g. if you have two DIV tags, does the first contain the second or is it implicitly closed by the second? Commented Feb 7, 2012 at 18:23

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.