2015-02-10 23 views
0

我有一個ASP.NET Web窗體應用程序。 我使用XSLT來形成HTML頁面。來自XSLT的數據未加載

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="html"/> 
    <xsl:template match="/"> 
    <html> 
     <head> 
     <title>Footballer's XSLT Demo</title> 
     </head> 
     <body> 
     <h2>Footballer's Information</h2> 
     </body> 
    </html> 
    </xsl:template> 
</xsl:stylesheet> 

我寫負載頁面上此代碼:

namespace XSLT 
{ 
    public partial class _Default : Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      string MyXsltPath = Request.PhysicalApplicationPath + "1/footballers.xslt"; 
      XslCompiledTransform XSLTransform = new XslCompiledTransform(); 
      XSLTransform.Load(MyXsltPath); 
     } 
    } 
} 

但不顯示任何信息。 我該怎麼辦?

+1

您提供一個轉換,但如果是源數據?轉換用於讀取XML並根據xslt輸出它,但您似乎缺少xml源。 – dbugger 2015-02-10 18:30:33

+0

上一個評論者是正確的,儘管我會使用不同的措辭:XSLT通常會轉換XML輸入文檔_。另外,在我看來,你並沒有真正引用這個轉換。 – 2015-02-10 20:11:33

回答

0

這裏是加載XSLT文件的代碼,

  XmlDocument xmlDoc = new XmlDocument(); 
      System.IO.Stream xmlStream; 
      System.Xml.Xsl.XslCompiledTransform xsl = new System.Xml.Xsl.XslCompiledTransform(); 
      ASCIIEncoding enc = new ASCIIEncoding(); 
      System.IO.StringWriter writer = new System.IO.StringWriter(); 

     String TransactionXML = "<xml><node></node></xml>"; 
     // Get Xsl 
     xsl.Load(HttpContext.Current.Server.MapPath("footballers.xslt")); 
     // Remove the utf encoding as it causes problems with the XPathDocument 
     TransactionXML = TransactionXML.Replace("utf-32", ""); 
     TransactionXML = TransactionXML.Replace("utf-16", ""); 
     TransactionXML = TransactionXML.Replace("utf-8", ""); 
     xmlDoc.LoadXml(TransactionXML); 

     // Get the bytes 
     xmlStream = new System.IO.MemoryStream(enc.GetBytes(xmlDoc.OuterXml), true); 

     // Load Xpath document 
     System.Xml.XPath.XPathDocument xp = new System.Xml.XPath.XPathDocument(xmlStream); 

     // Perform Transform 
     xsl.Transform(xp, null, writer); 
     Response.Write(writer.ToString()); 
+0

它運作良好。謝謝! – Marina 2015-02-11 08:33:25