Wrapping of text in HTML(XML)

  • Hi,

    I am using FOR XML to generate the HTML and following is my query. I am using td { white-space:nowrap;} for the columns to used no wrap. But i want two columns which need to be wrapped. i have tried lots of options. can somebody help.

    SET @tableHTML =

    N'<H3><font size="4"> Board - Items </font></H3>' +

    N'<table border="1" ><font size="1"> <head><style TYPE="text/css"> td { white-space:nowrap;} </style> ' +

    N'<tr bgcolor=#4b6c9e><font size="2" color=white><th> ID</th>

    <th> A</th>

    <th> B </th>

    <th> C </th>

    <th> D</th>

    <th> E </th>

    <th> F</th>

    <th>G </th>

    <th>H</th>

    <th>I</th>

    <th>J</th></tr>' +

    CAST ( ( SELECT td =A, '',

    td = B, '',

    td = C, '',

    td = D, '',

    td = E, '',

    td = F, '',

    td = G, '',

    td = H, '',

    td = I, '',

    td = J, '',

    td = K

    FROM #blocked

    FOR XML PATH('tr'), TYPE

    ) AS NVARCHAR(MAX) ) +

    N'</font> </font></table>' ;

    From the above code i want G and H columns to be wrapped.

    Thanks.

  • sandhyak16 (9/3/2015)


    Hi,

    I am using FOR XML to generate the HTML and following is my query. I am using td { white-space:nowrap;} for the columns to used no wrap. But i want two columns which need to be wrapped. i have tried lots of options. can somebody help.

    SET @tableHTML =

    N'<H3><font size="4"> Board - Items </font></H3>' +

    N'<table border="1" ><font size="1"> <head><style TYPE="text/css"> td { white-space:nowrap;} </style> ' +

    N'<tr bgcolor=#4b6c9e><font size="2" color=white><th> ID</th>

    <th> A</th>

    <th> B </th>

    <th> C </th>

    <th> D</th>

    <th> E </th>

    <th> F</th>

    <th>G </th>

    <th>H</th>

    <th>I</th>

    <th>J</th></tr>' +

    CAST ( ( SELECT td =A, '',

    td = B, '',

    td = C, '',

    td = D, '',

    td = E, '',

    td = F, '',

    td = G, '',

    td = H, '',

    td = I, '',

    td = J, '',

    td = K

    FROM #blocked

    FOR XML PATH('tr'), TYPE

    ) AS NVARCHAR(MAX) ) +

    N'</font> </font></table>' ;

    From the above code i want G and H columns to be wrapped.

    Thanks.

    Quick question, can you post the DDL for #blocked and sample data as an insert statement?

    😎

  • Some issues with your HTML template:

    i. The head tag is inside the table tag, try moving it (and the style tag it contains) to before the H3 tag

    ii. The head tag isn't closed

    iii. Instead of using font tags try doing all your styling inside your css

    iv. To have correct HTML you should have a <head> and a <body> element in your document, the body should contain the table

    Also this article might help

  • i just built this example from my notes about adding align in xml.

    this quick example seems to generate valid xml/html snippets, using inline styles.

    i would do as suggested and use a css class, but that's just a matter of detail and better practice.

    --just some data from sys.objects so anyone can run it.

    --white-space: normal|nowrap|pre|pre-line|pre-wrap|initial|inherit;

    --white-space: normal|nowrap|pre|pre-line|pre-wrap|initial|inherit;

    SELECT td = RW,'',

    'white-space: nowrap' AS 'td/@style',td= REPLICATE(name + ' ',50),'' ,

    'white-space: nowrap' AS 'td/@style',td= REPLICATE(type_desc + ' ',50) ,'',

    'white-space: normal' AS 'td/@style',td= REPLICATE(name + ' ',50),'',

    'white-space: normal' AS 'td/@style',td= REPLICATE(type_desc + ' ',50),''

    FROM (

    select

    row_number() over(partition by type_desc order by name) As RW,

    name,

    type_desc

    from sys.objects

    ) MyAlias

    WHERE RW = 1

    FOR XML PATH('tr'), TYPE

    SELECT td = RW,'',

    'nowrap' AS 'td/@class',td= REPLICATE(name + ' ',50),'' ,

    'nowrap' AS 'td/@class',td= REPLICATE(type_desc + ' ',50) ,'',

    'normal' AS 'td/@class',td= REPLICATE(name + ' ',50),'',

    'normal' AS 'td/@class',td= REPLICATE(type_desc + ' ',50),''

    FROM (

    select

    row_number() over(partition by type_desc order by name) As RW,

    name,

    type_desc

    from sys.objects

    ) MyAlias

    WHERE RW = 1

    FOR XML PATH('tr'), TYPE

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply