XmlDataSource.TransformFile 屬性

定義

指定可延伸樣式表語言 (XSL) 檔案 (.xsl) 的檔名,該檔案會定義要在 XmlDataSource 控制項所管理的 XML 資料上執行的 XSLT 轉換。

public:
 virtual property System::String ^ TransformFile { System::String ^ get(); void set(System::String ^ value); };
public virtual string TransformFile { get; set; }
member this.TransformFile : string with get, set
Public Overridable Property TransformFile As String

屬性值

XSL 樣式表檔案的絕對實體路徑或相對路徑,這個檔案定義 DataDataFile 屬性所含資料所要執行的 XML 轉換。 預設值是 Empty

例外狀況

此文件正在載入。

範例

下列程式碼範例示範如何使用 XmlDataSource 控制項和 TreeView 控制項,在 Web 表單上顯示已轉換的 XML 資料。 轉換是使用 屬性所 TransformFile 指示的樣式表單來執行。 您可以選擇性地使用 XsltArgumentList 來提供樣式表單的轉換引數。 如需詳細資訊,請參閱 TransformArgumentList 屬性 (Property)。

<%@ Page Language="C#" %>
<!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 runat="server">
    <title>ASP.NET Example</title>
</head>
<body>

    <form id="form1" runat="server">
      <asp:XmlDataSource
        id="XmlDataSource1"
        runat="server"
        datafile="bookstore.xml"
        transformfile="bookstore.xsl"/>

      <!- TreeView uses hierachical data, so the
          XmlDataSource uses an XmlHierarchicalDataSourceView
          when a TreeView is bound to it. -->

      <asp:treeview
        id="TreeView1"
        runat="server"
        datasourceid="XmlDataSource1">
        <databindings>
          <asp:treenodebinding depth="1" datamember="genre"
             textfield="name" valuefield="name"/>
          <asp:treenodebinding depth="2" datamember="book"
            textfield="title" valuefield="ISBN"/>
          <asp:treenodebinding depth="3" datamember="chapter"
            textfield="name" valuefield="num"/>
        </databindings>
      </asp:treeview>

    </form>
    
  </body>
</html>
<%@ Page Language="VB" %>
<!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 runat="server">
    <title>ASP.NET Example</title>
</head>
<body>

    <form id="form1" runat="server">
      <asp:xmldatasource
        id="XmlDataSource1"
        runat="server"
        datafile="bookstore.xml"
        transformfile="bookstore.xsl"/>

      <!- TreeView uses hierachical data, so the
          XmlDataSource uses an XmlHierarchicalDataSourceView
          when a TreeView is bound to it. -->

      <asp:treeview
        id="TreeView1"
        runat="server"
        datasourceid="XmlDataSource1">
        <databindings>
          <asp:treenodebinding depth="1" datamember="genre"
             textfield="name" valuefield="name"/>
          <asp:treenodebinding depth="2" datamember="book"
            textfield="title" valuefield="ISBN"/>
          <asp:treenodebinding depth="3" datamember="chapter"
            textfield="name" valuefield="num"/>
        </databindings>
      </asp:treeview>

    </form>
  </body>
</html>

程式碼範例中的 XML 檔案具有下列資料:

<bookstore>  
   <genre name="fiction">  
     <book ISBN="0000000000">  
       <title>Secrets of Silicon Valley</title>  
       <price>12.95</price>  
       <chapters>  
         <chapter num="1" name="Introduction" />             
         <chapter num="2" name="Body" />            
         <chapter num="3" name="Conclusion" />  
       </chapters>  
     </book>  
   </genre>  
   <genre name="novel">  
     <book genre="novel" ISBN="1111111111">  
       <title>Straight Talk About Computers</title>  
       <price>24.95</price>  
       <chapters>  
         <chapter num="1" name="Introduction" />   
         <chapter num="2" name="Body" />  
         <chapter num="3" name="Conclusion" />  
       </chapters>  
     </book>  
   </genre>  
</bookstore>  

執行 XML 轉換的 XSL 樣式表單具有下列結構:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  
 <xsl:template match="bookstore">  
   <bookstore>  
     <xsl:apply-templates select="genre"/>  
   </bookstore>  
 </xsl:template>  
 <xsl:template match="genre">  
   <genre>  
     <xsl:attribute name="name">  
       <xsl:value-of select="@name"/>  
     </xsl:attribute>  
     <xsl:apply-templates select="book"/>  
   </genre>  
 </xsl:template>  
 <xsl:template match="book">  
   <book>  
     <xsl:attribute name="ISBN">  
       <xsl:value-of select="@ISBN"/>  
     </xsl:attribute>  
     <xsl:attribute name="title">  
       <xsl:value-of select="title"/>  
     </xsl:attribute>  
     <xsl:attribute name="price">  
       <xsl:value-of select="price"/>  
     </xsl:attribute>  
     <xsl:apply-templates select="chapters/chapter" />  
   </book>  
 </xsl:template>  
 <xsl:template match="chapter">  
   <chapter>  
     <xsl:attribute name="num">  
       <xsl:value-of select="@num"/>  
     </xsl:attribute>  
     <xsl:attribute name="name">  
       <xsl:value-of select="@name"/>  
     </xsl:attribute>  
     <xsl:apply-templates/>  
   </chapter>  
 </xsl:template>  
</xsl:stylesheet>  

備註

如果同時 TransformFile 設定 和 Transform 屬性,則會優先使用 屬性, TransformFile 並使用 XSL 樣式表單檔案 (.xsl) 中的資料,而不是 屬性中指定的 Transform 樣式表單專案。 如果使用 屬性設定 XPath XPath 運算式,則會在轉換 XML 資料之後套用它。

如果您變更 屬性的值 Transform ,就會 DataSourceChanged 引發 事件。 如果已啟用快取,而且您變更 的值 Transform ,快取就會失效。

注意

類別 XmlDataSource 會使用已被取代的 XslTransform 類別來執行 XSL 轉換。 如果您想要使用類別已被取代之後 XslTransform 引進的樣式表單功能,請使用 類別手動套用 XslCompiledTransform 轉換。

適用於

另請參閱