XmlDataSource.TransformFile 屬性

定義

指定可延伸樣式表語言 (XSL) 檔案 (.xsl) 的檔名,該檔案會定義要在 XmlDataSource 控制項所管理的 XML 資料上執行的 XSLT 轉換。Specifies the file name of an Extensible Stylesheet Language (XSL) file (.xsl) that defines an XSLT transformation to be performed on the XML data managed by the XmlDataSource control.

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

屬性值

String

XSL 樣式表檔案的絕對實體路徑或相對路徑,這個檔案定義 DataDataFile 屬性所含資料所要執行的 XML 轉換。The absolute physical path or relative path of the XSL style sheet file that defines an XML transformation to be performed on the data contained in the Data or DataFile properties. 預設值是 EmptyThe default value is Empty.

例外狀況

此文件正在載入。The document is loading.

範例

下列程式碼範例示範如何使用 XmlDataSource 控制項和 TreeView 控制項,在 Web 表單上顯示已轉換的 XML 資料。The following code example demonstrates how to use an XmlDataSource control and a TreeView control to display transformed XML data on a Web form. 轉換是使用屬性所指示的樣式表單來執行 TransformFileThe transformation is performed using the style sheet indicated by the TransformFile property. 您可以使用,選擇性地為樣式表單提供轉換引數 XsltArgumentListYou can optionally supply transformation arguments for the style sheet using an XsltArgumentList. 如需詳細資訊,請參閱 TransformArgumentList 屬性 (Property)。For more information, see the 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 檔案包含下列資料:The XML file in the code example has the following data:

<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 樣式表單具有下列結構:The XSL style sheet that performs the XML transformation has the following structure:

<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) ,而不是在屬性中指定的樣式表單專案 TransformIf both the TransformFile and Transform properties are set, the TransformFile property takes precedence and the data in the XSL style sheet file (.xsl) is used instead of the style sheet elements specified in the Transform property. 如果使用屬性設定 XPath 運算式,則 XPath 會在轉換 XML 資料之後套用它。If an XPath expression is set using the XPath property, it is applied after the XML data is transformed.

如果您變更屬性的值 TransformDataSourceChanged 就會引發事件。If you change the value of the Transform property, the DataSourceChanged event is raised. 如果快取已啟用,而您變更的值 Transform ,則快取會失效。If caching is enabled and you change the value of Transform, the cache is invalidated.

注意

類別會使用已被 XmlDataSource 取代的 XslTransform 類別來執行 XSL 轉換。The XmlDataSource class uses the deprecated XslTransform class to perform XSL transformations. 如果您想要使用在類別淘汰之後引進的樣式表單功能 XslTransform ,請使用類別手動套用轉換 XslCompiledTransformIf you want to use style sheet features that were introduced after the XslTransform class was deprecated, apply the transforms manually by using the XslCompiledTransform class.

適用於

另請參閱