1

Can I use xml/xslt for dynamic html generation in my code behind with my data in dataset.

I have a requirement where i will have to show top 5 news in my home page. SO what i have thot is:

  • After the page is loaded, I will call a web service which will send me html version of my top 5 news. (Ajax way)
  • Using javascript I will insert the returned formatted html string inside a div tag.

Ok, But I was looking for some good way of generating the formatted html. My actual data is inside a DataTable. I was wondering wether this can be done by xslt & xml or not.

I had earlier used StringBuilder way to make the dynamic html after inemurating the DataTable, but I dont feel that its not the preferred way because if later i have to add some extra formatting then i have to again modify my code & publish the dll.

Just wanted to know what techniques should be used for such kind of scenarios. Need your valuable suggestions and if possible sample links.

Sample DataTable:

Id- Heading - Description  - link  
1 - hello   - how are you  - yahoo.com  
2 - bye     - I am leaving - google.com 

Sample output Html:

<table>
<tr id='1'>
<td >hello</td><td ><a href='yahoo.com'>how are you</a></td>
</tr>
<tr id='1'>
<td >bye</td><td ><a href='google.com'>I am leaving</a></td>
</tr>
</table>

Thanks & Kind regards.
M

4

2 回答 2

1

Yes - you can use xml/xslt for generating html. Only issue with this approach is to get xml serialization of data - data-table supports it but its schema is convoluted as compared to say a hand-crafted xml - so writing xsl becomes a bit difficult. Then there is obvious performance cost - first datatore to datatable, then datatable to xml and then xml to xslt. You can avoid some by directly fetching xml from the database (that would also allow you to control the schema). Personally, I would go to xml/xslt route only if I have many such conversions to do.

You also have some alternatives:

  1. User server side templating - for example, user aspx page or usercontrol and data-binding syntax to generate the html (use HttpServerUtility.Execute to get page html in service code)

  2. My favorite is to return JSON object from service and do html generation in java-script. You can use client side templates for the purpose - see how to do it with microsoft way or using pure js templating engines such as jsTemplate or jTemplates

于 2011-04-19T11:33:36.237 回答
0

You can use native XslTransform class, or you can choose one of many opensource implementations.

于 2011-04-19T11:15:16.493 回答