好的,..
作为一个简单的测试(在 ASP.NET、SQL 中),我使用了一个数据集来存储来自 DB 的数据,然后填写 XmlDataDocument。然后 XslCompiledTransform 和 XmlTextWriter 用于加载定义的 xsl.. 最后样式化的 xml 页面将使用 Response.OutputStream 发送到页面。运行页面后,似乎是使用 xsl 设置样式的 xml 页面,但 我的主要问题仍然存在:
Simple Database 是一个 SQL 数据库,其表包含 4 列:ID、姓名、年龄、电话
这是我的 Default.aspx.cs(代码)-(我没有更改 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();
}
}
这是一个简单的 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 是 当我运行它时该简单代码页的结果 并在 页面源代码 中查看(使用浏览器)它显示 html 标签(如 body、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>
但正如我之前所说,我需要让它看起来像上面提到的天气页面(请在浏览器中查看页面源代码).. 类似这样的: *何时*我在浏览器上点击页面查看源...
<?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>
有什么问题??
我如何在 asp.net 中做(解决)这个问题?