ok, .. Comme un test simple (dans ASP.NET, SQL), je l'ai utilisé un dataset pour stocker des données qui viennent de DB, puis remplissez XmlDataDocument. Ensuite, XslCompiledTransform et XmlTextWriter sont utilisés pour charger xsl défini. Enfin, la page xml sera envoyée à la page en utilisant Response.OutputStream. Après l'exécution de la page, il semble xml page stylé en utilisant xsl mais mon problème principal reste encore: Simple Database est une base de données SQL avec une table qui contient 4 colonnes: ID, Nom, Âge, Téléphone C'EST Ma Default.aspx .cs (code) - (je ne change pas la conception prédéfinie de default.aspx)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Xml;
using System.Xml.Xsl;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection Conn = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=TestDB;Integrated Security=SSPI;");
Conn.Open();
DataSet DS = new DataSet("TestDataSet");
SqlDataAdapter DA = new SqlDataAdapter("SELECT TOP 1 * FROM TableTest", Conn);
DA.Fill(DS, "Persons");
Conn.Close();
DS.EnforceConstraints = false;
XmlDataDocument xmlDoc = new XmlDataDocument(DS);
// Create a procesing instruction.
XmlProcessingInstruction newPI;
String PItext = "<?xml version='1.0' encoding='utf-8'?>'";
newPI = xmlDoc.CreateProcessingInstruction("xml-stylesheet", PItext);
// Add the processing instruction node to the document.
xmlDoc.AppendChild(newPI);
XslCompiledTransform xslTran = new XslCompiledTransform();
xslTran.Load(MapPath("TestTransform.xsl"));
// Response.ContentType = "text/xml;charset=UTF-8";
XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, System.Text.Encoding.UTF8);
xslTran.Transform(xmlDoc, null, writer);
writer.Close();
}
}
Et est simple fichier XSL (TestTransform.xsl)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:str="http://xsltsl.org/string" version="1.0">
<xsl:output method="html"/>
<xsl:template match="Persons">
<html>
<head>
<title>
Simple Test : Information for : <xsl:value-of select="Name/text()"/>
</title>
</head>
<style type="text/css">
.label { font-weight: bold; vertical-align: text-top; text-align: right;}
.xsllocation { font-size: 18px; color: white; font-weight: bold; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; }
</style>
<body bgcolor="#ddffff" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" background="/images/background1.gif">
<table width="700" border="0" cellspacing="0">
<td width="525" valign="top">
<table cellspacing="2" cellpadding="0" border="0">
<tr valign="top">
<td>       </td>
<td width="100%" align="center">
<a name="contents" id="contents"></a>
<table style="margin-left: 10px;" align="left">
<xsl:variable name="name">
<xsl:value-of select="Name/text()"/>
</xsl:variable>
<xsl:if test="$name != 'NA' and $name != ''">
<tr>
<td class="label">Name is:</td>
<td>
<xsl:copy-of select="$name"/>
</td>
</tr>
</xsl:if>
<xsl:variable name="age">
<xsl:value-of select="Age/text()"/>
</xsl:variable>
<xsl:if test="$age != 'NA' and $age != ''">
<tr>
<td class="label">Age:</td>
<td>
<xsl:copy-of select="$age"/> years old
</td>
</tr>
</xsl:if>
<xsl:variable name="phone">
<xsl:value-of select="Phone/text()"/>
</xsl:variable>
<xsl:if test="$phone != 'NA' and $phone != ''">
<tr>
<td class="label">Phone Number:</td>
<td>
<xsl:copy-of select="$phone"/>
</td>
</tr>
</xsl:if>
</table>
</td>
</tr>
</table>
</td>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
1.below est Résultat de ce code simple p âge Lorsque je l'exécute et afficher dans page source (en utilisant le navigateur) Il montre les balises html (comme le corps, td, ...):
<html xmlns:str="http://xsltsl.org/string"><head><title>
Simple Test : Information for : Mr Abc </title></head><style type="text/css">
.label { font-weight: bold; vertical-align: text-top; text-align: right;}
.xsllocation { font-size: 18px; color: white; font-weight: bold; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; }
</style><body bgcolor="#ddffff" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" background="/images/background1.gif"><table width="700" border="0" cellspacing="0"><td width="525" valign="top"><table cellspacing="2" cellpadding="0" border="0"><tr valign="top"><td> </td><td width="100%" align="center"><a name="contents" id="contents" /><table style="margin-left: 10px;" align="left"><tr><td class="label">Name is:</td><td>Mr Abc </td></tr><tr><td class="label">Age:</td><td>32 years old
</td></tr><tr><td class="label">Phone Number:</td><td>345353232 </td></tr></table></td></tr></table></td></table></body></html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title></head>
<body>
<form name="form1" method="post" action="Default.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZKwzWK/GivBcwTW8PWk8wUf8dacg" />
</div>
<div>
</div>
</form>
</body>
</html>
mais comme je l'ai dit avant que je dois faire cela ressemble à cette page météo mentionné (s'il vous plaît voir la source de la page dans le navigateur) .. Quelque chose comme ceci: * quand * je clique sur la page Voir la source sur le navigateur ...
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="TestTransform.xsl" type="text/xsl"?>
<Persons version="1.0"
<Name>Mr Abc</Name>
<Age>32</Age>
<Phone>345353232</Phone>
</Persons>
Quel est le problème? Comment puis-je faire (résoudre) cela dans asp.net?
@ csharper2020: Si vous connaissez bien ASP.Net, alors vous connaissez les bases du modèle de population. Un exemple XSLT ici dans http://stackoverflow.com/questions/3529852/sitemesh-like-functionality-with-xslt/3533226 –
@Alejandro Merci – csharper2020
@ drachenstern- Non, pas encore, .. J'ai un problème de génération dynamique de xml .. (je pense que c'est lié à l'en-tête de la page xml qui revient de xmldatadocument? Je ne sais pas pourquoi la source de la page dans le navigateur n'est pas structurée xml) ... – csharper2020