Compartilhar via


XmlDataSource.TransformFile Propriedade

Definição

Especifica o nome do arquivo de um arquivo XSL (linguagem XSL) (.xsl) que define uma transformação XSLT a ser executada nos dados XML gerenciados pelo controle XmlDataSource.

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

Valor da propriedade

O caminho físico absoluto ou o caminho relativo do arquivo de folha de estilos XSL que define uma transformação XML a ser executada nos dados contidos nas Data propriedades ou DataFile . O valor padrão é Empty.

Exceções

O documento está carregando.

Exemplos

O exemplo de código a seguir demonstra como usar um XmlDataSource controle e um TreeView controle para exibir dados XML transformados em um formulário da Web. A transformação é executada usando a folha de estilos indicada pela TransformFile propriedade . Opcionalmente, você pode fornecer argumentos de transformação para a folha de estilos usando um XsltArgumentList. Para obter mais informações, consulte a propriedade TransformArgumentList.

<%@ 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>

O arquivo XML no exemplo de código tem os seguintes dados:

<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>

A folha de estilos XSL que executa a transformação XML tem a seguinte estrutura:

<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>

Comentários

Se as TransformFile propriedades e Transform forem definidas, a TransformFile propriedade terá precedência e os dados no arquivo de folha de estilos XSL (.xsl) serão usados em vez dos elementos de folha de estilos especificados na Transform propriedade . Se uma expressão XPath for definida usando a XPath propriedade , ela será aplicada depois que os dados XML forem transformados.

Se você alterar o valor da Transform propriedade , o DataSourceChanged evento será gerado. Se o cache estiver habilitado e você alterar o valor de Transform, o cache será invalidado.

Observação

A XmlDataSource classe usa a classe preterida XslTransform para executar transformações XSL. Se você quiser usar recursos de folha de estilos que foram introduzidos depois que a XslTransform classe foi preterida, aplique as transformações manualmente usando a XslCompiledTransform classe .

Aplica-se a

Confira também