- 1 - Créer le modèle XSL suivant dans le répertoire de votre appli (qui doit être une application Console... C'est juste pour l'exemple) :
-
- <?xml version="1.0" encoding="UTF-8" ?>
- - <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
- <xsl:output method="html" />
- - <xsl:template match="*">
- <xsl:apply-templates />
- </xsl:template>
- - <xsl:template match="Employees">
- - <TR>
- - <TD>
- <xsl:value-of select="@FirstName" />
- </TD>
- - <TD>
- - <B>
- <xsl:value-of select="@LastName" />
- </B>
- </TD>
- </TR>
- </xsl:template>
- - <xsl:template match="/">
- - <HTML>
- - <HEAD>
- <STYLE>th { background-color: #CCCCCC }</STYLE>
- </HEAD>
- - <BODY>
- - <TABLE border="1" style="width:300;">
- - <TR>
- <TH colspan="2">Employees</TH>
- </TR>
- - <TR>
- <TH>First name</TH>
- <TH>Last name</TH>
- </TR>
- <xsl:apply-templates select="root" />
- </TABLE>
- </BODY>
- </HTML>
- </xsl:template>
- </xsl:stylesheet>
-
- 2 - Voici le code C#, qui va générer un fichier HTML. J'ai copié ici toute ma classe :
-
- class Class1
- {
- static string NorthwindConnString = "Provider=SQLOLEDB;Server=localhost;database=Northwind;Integrated Security=SSPI";
- public static int testXSL()
- {
- //Stream strm;
- SqlXmlCommand cmd = new SqlXmlCommand(NorthwindConnString);
- cmd.CommandText = "select FirstName, LastName from Employees For XML Auto";
- cmd.RootTag = "root";
- using (Stream strm = cmd.ExecuteStream())
- {
- XmlTextReader reader = new XmlTextReader(strm);
- XPathDocument xd = new XPathDocument(reader, XmlSpace.Preserve);
- XslTransform xslt = new XslTransform();
- xslt.Load("MyXSL.xsl", null);
- XmlTextWriter writer = new XmlTextWriter("xslt_output.html", System.Text.Encoding.UTF8);
-
- xslt.Transform(xd, null, writer);
- }
- return 0;
- }
- public static int Main(String[] args)
- {
- testXSL();
- return 0;
- }
-
- }
-
- 3 - générer l'application, et la, si tout se passe bien, vous avez un joli fichier HTML dans le répertoire de l'appli
-
- Vous remarquerez dans la requête SQL le : "XML for Auto" à la fin. C'est cette commande qui dit à SQL Server de retourner un résultat au format XML.
1 - Créer le modèle XSL suivant dans le répertoire de votre appli (qui doit être une application Console... C'est juste pour l'exemple) :
<?xml version="1.0" encoding="UTF-8" ?>
- <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" />
- <xsl:template match="*">
<xsl:apply-templates />
</xsl:template>
- <xsl:template match="Employees">
- <TR>
- <TD>
<xsl:value-of select="@FirstName" />
</TD>
- <TD>
- <B>
<xsl:value-of select="@LastName" />
</B>
</TD>
</TR>
</xsl:template>
- <xsl:template match="/">
- <HTML>
- <HEAD>
<STYLE>th { background-color: #CCCCCC }</STYLE>
</HEAD>
- <BODY>
- <TABLE border="1" style="width:300;">
- <TR>
<TH colspan="2">Employees</TH>
</TR>
- <TR>
<TH>First name</TH>
<TH>Last name</TH>
</TR>
<xsl:apply-templates select="root" />
</TABLE>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>
2 - Voici le code C#, qui va générer un fichier HTML. J'ai copié ici toute ma classe :
class Class1
{
static string NorthwindConnString = "Provider=SQLOLEDB;Server=localhost;database=Northwind;Integrated Security=SSPI";
public static int testXSL()
{
//Stream strm;
SqlXmlCommand cmd = new SqlXmlCommand(NorthwindConnString);
cmd.CommandText = "select FirstName, LastName from Employees For XML Auto";
cmd.RootTag = "root";
using (Stream strm = cmd.ExecuteStream())
{
XmlTextReader reader = new XmlTextReader(strm);
XPathDocument xd = new XPathDocument(reader, XmlSpace.Preserve);
XslTransform xslt = new XslTransform();
xslt.Load("MyXSL.xsl", null);
XmlTextWriter writer = new XmlTextWriter("xslt_output.html", System.Text.Encoding.UTF8);
xslt.Transform(xd, null, writer);
}
return 0;
}
public static int Main(String[] args)
{
testXSL();
return 0;
}
}
3 - générer l'application, et la, si tout se passe bien, vous avez un joli fichier HTML dans le répertoire de l'appli
Vous remarquerez dans la requête SQL le : "XML for Auto" à la fin. C'est cette commande qui dit à SQL Server de retourner un résultat au format XML.