Apply XSLT conversion to Dataset

Source: Internet
Author: User
Tags xsl xslt

Dataset writexml allows you to write dataset content in XML format. A common task that follows is to convert the XML into another format using XSL Transformation (XSLT. However, by synchronizing dataset with xmldatadocument, you can apply an XSLT style table to the content of dataset, instead of using writexml to write dataset content in XML format.
In the following example, a dataset is filled with a table and a link to synchronize the dataset with xmldatadocument, and then a part of the dataset is written as an HTML file using the XSLT style table. The following is the content of the XSLT style sheet.
<XSL: stylesheet xmlns: XSL = "http://www.w3.org/1999/XSL/Transform" version = "1.0">

<XSL: template match = "customerorders">
<HTML>
<Style>
Body {font-family: verdana; font-size: 9pt}
TD {font-size: 8pt}
</Style>
<Body>
<Table border = "1">
<XSL: Apply-templates select = "customers"/>
</Table>
</Body>
</Html>
</XSL: Template>

<XSL: template match = "customers">
<Tr> <TD>
<XSL: value-of select = "contactname"/>, <XSL: value-of select = "phone"/> <br/>
</TD> </tr>
<XSL: Apply-templates select = "orders"/>
</XSL: Template>

<XSL: template match = "orders">
<Table border = "1">
<Tr> <TD valign = "TOP"> <B> order: </B> </TD> <TD valign = "TOP"> <XSL: value-of select = "orderid"/> </TD> </tr>
<Tr> <TD valign = "TOP"> <B> date: </B> </TD> <TD valign = "TOP"> <XSL: value-of select = "orderdate"/> </TD> </tr>
<Tr> <TD valign = "TOP"> <B> ship to: </B> </TD>
<TD valign = "TOP"> <XSL: value-of select = "shipname"/> <br/>
<XSL: value-of select = "shipaddress"/> <br/>
<XSL: value-of select = "shipcity"/>, <XSL: value-of select = "shipregion"/> <XSL: value-of select = "shippostalcode"/> <br/>
<XSL: value-of select = "shipcountry"/> </TD> </tr>
</Table>
</XSL: Template>

</XSL: stylesheet>
BelowCodeIs the code for filling dataset and applying XSLT style sheets.
Note: When the dataset inclusion relationship of the XSLT style table is applied, if the nested attribute of datarelation is set to true for each nested relationship, the optimal performance is obtained. This setting allows you to use an XSLT style sheet to perform normal top-down processing to traverse hierarchies and convert data, rather than using performance-intensive XPath positioning axes (for example, the style sheet node tests the front and back peers in the expression) to traverse the data hierarchy. For more information about nested relationships, see nested datarelation.
[C #]
Using system;
Using system. Data;
Using system. Data. sqlclient;
Using system. xml;
Using system. xml. XSL;

Public class sample
{
Public static void main ()
{
Sqlconnection nwindconn = new sqlconnection ("Data Source = localhost; initial catalog = northwind; Integrated Security = sspi ;");
Nwindconn. open ();

Dataset custds = new dataset ("customerdataset ");

Sqldataadapter custda = new sqldataadapter ("select * from MERs", nwindconn );
Custda. Fill (custds, "MERs ");

Sqldataadapter ordersda = new sqldataadapter ("select * from orders", nwindconn );
Ordersda. Fill (custds, "orders ");

Nwindconn. Close ();

Custds. relations. Add ("custorders ",
Custds. Tables ["MERs"]. Columns ["customerid"],
Custds. Tables ["orders"]. Columns ["customerid"]). nested = true;

Xmldatadocument xmldoc = new xmldatadocument (custds );

Transform transform into TRAN = new transform ();
Using Tran. Load ("transform. XSL ");

Xmltextwriter writer = new xmltextwriter ("effect_output.html", system. Text. encoding. utf8 );

Using Tran. Transform (xmldoc, null, writer );
Writer. Close ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.