次の方法で共有


XslTransform.Transform メソッド (IXPathNavigable, XsltArgumentList, XmlWriter, XmlResolver)

メモ : この名前空間、クラス、およびメンバは、.NET Framework Version 1.1 だけでサポートされています。

指定した args を使用して IXPathNavigable の XML データを変換し、その結果を XmlWriter に出力します。

Overloads Public Sub Transform( _
   ByVal input As IXPathNavigable, _   ByVal args As XsltArgumentList, _   ByVal output As XmlWriter, _   ByVal resolver As XmlResolver _)
[C#]
public void Transform(IXPathNavigableinput,XsltArgumentListargs,XmlWriteroutput,XmlResolverresolver);
[C++]
public: void Transform(IXPathNavigable* input,XsltArgumentList* args,XmlWriter* output,XmlResolver* resolver);
[JScript]
public function Transform(
   input : IXPathNavigable,args : XsltArgumentList,output : XmlWriter,resolver : XmlResolver);

パラメータ

  • input
    IXPathNavigable インターフェイスを実装するオブジェクト。.NET Framework では、これは XmlNode (通常は XmlDocument)、または変換されるデータを含む XPathDocument になります。

  • args
    変換に対する入力として使用された名前空間限定引数を含む XsltArgumentList

  • output
    出力先の XmlWriter

  • resolver
    XSLT の document() 関数を解決するために使用する XmlResolver 。これが null 参照 (Visual Basic では Nothing) の場合、document() 関数は解決されません。

    Transform メソッドが完了した後、 XmlResolver がキャッシュされていません。

例外

例外の種類 条件
XsltException XSLT 変換の処理中にエラーが発生しました。

解説

XslTransform は、XSLT 1.0 構文をサポートしています。XSLT スタイル シートには、名前空間宣言 xmlns:xsl= http://www.w3.org/1999/XSL/Transform を含める必要があります。

args は、スタイル シートに定義されている xsl:param 要素と一致します。 XmlWriter に出力するとき、 xsl:output 要素はサポートされません。 xsl:output は無視されます。詳細については、「 XslTransform からの出力 」を参照してください。

変換はドキュメント全体に適用されます。つまり、ドキュメント ルート ノード以外のノードに渡す場合、これによって、読み込まれたドキュメント内のすべてのノードに変換処理が行われることを防ぐことはできません。ノード フラグメントを変換するには、ノード フラグメントだけを含む XmlDocument を作成し、その XmlDocument を Transform メソッドに渡す必要があります。詳細については、「 XslTransform クラスによる XSLT プロセッサの実装 」を参照してください。

使用例

[Visual Basic, C#, C++] XML ドキュメントを HTML ドキュメントに変換する例を次に示します。ここでは、表内の各書籍の ISBN、タイトル、および価格を表示します。

 
Option Strict
Option Explicit

Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Xsl
Imports System.Xml.XPath

Public Class Sample
    Private Shared filename1 As String = "books.xml"
    Private Shared stylesheet1 As String = "output.xsl"
    
    
    Public Shared Sub Main()
        'Load the stylesheet.
        Dim xslt As New XslTransform()
        xslt.Load(stylesheet1)
        
        'Load the file to transform.
        Dim doc As New XPathDocument(filename1)
        
        'Create an XmlTextWriter which outputs to the console.
        Dim writer As New XmlTextWriter(Console.Out)
        
        'Transform the file and send the output to the console.
        xslt.Transform(doc, Nothing, writer, Nothing)
        writer.Close()
    End Sub 'Main 
End Class 'Sample

[C#] 
using System;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;

public class Sample
{
  private const String filename = "books.xml";
  private const String stylesheet = "output.xsl";

  public static void Main()
  {
    //Load the stylesheet.
    XslTransform xslt = new XslTransform();
    xslt.Load(stylesheet);

    //Load the file to transform.
    XPathDocument doc = new XPathDocument(filename);
             
    //Create an XmlTextWriter which outputs to the console.
    XmlTextWriter writer = new XmlTextWriter(Console.Out);

    //Transform the file and send the output to the console.
    xslt.Transform(doc, null, writer, null);
    writer.Close();

  }
}

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Xsl;
using namespace System::Xml::XPath;

int main()
{
    String* filename = S"books.xml";
    String* stylesheet = S"output.xsl";

    //Load the stylesheet.
    XslTransform* xslt = new XslTransform();
    xslt->Load(stylesheet);

    //Load the file to transform.
    XPathDocument* doc = new XPathDocument(filename);
             
    //Create an XmlTextWriter which outputs to the console.
    XmlTextWriter* writer = new XmlTextWriter(Console::Out);

    //Transform the file and send the output to the console.
    xslt->Transform(doc, 0, writer, 0);
    writer->Close();
}

[Visual Basic, C#, C++] このサンプルでは、次の 2 つの入力ファイルを使用します。

[Visual Basic, C#, C++] books.xml

<?xml version='1.0'?>
<!-- This file represents a fragment of a book store inventory database -->
<bookstore>
  <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
    <title>The Autobiography of Benjamin Franklin</title>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
  </book>
  <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
    <title>The Confidence Man</title>
    <author>
      <first-name>Herman</first-name>
      <last-name>Melville</last-name>
    </author>
    <price>11.99</price>
  </book>
  <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
    <title>The Gorgias</title>
    <author>
      <name>Plato</name>
    </author>
    <price>9.99</price>
  </book>
</bookstore>

[Visual Basic, C#, C++] output.xsl

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="bookstore">
  <HTML>
    <BODY>
      <TABLE BORDER="2">
        <TR>
          <TD>ISBN</TD>
          <TD>Title</TD>
          <TD>Price</TD>
        </TR>
        <xsl:apply-templates select="book"/>
      </TABLE>

    </BODY>
  </HTML>
</xsl:template>
<xsl:template match="book">
  <TR>
    <TD><xsl:value-of select="@ISBN"/></TD>
    <TD><xsl:value-of select="title"/></TD>
    <TD><xsl:value-of select="price"/></TD>
  </TR>
</xsl:template>
</xsl:stylesheet>

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

XslTransform クラス | XslTransform メンバ | System.Xml.Xsl 名前空間 | XslTransform.Transform オーバーロードの一覧 | XslTransform クラスの随意動作の実装 | XslTransform からの出力 | XmlSecureResolver | XmlResolver.Credentials