使用指定的 args 转换 IXPathNavigable 中的 XML 数据,并将结果输出到 XmlWriter。Transforms the XML data in the IXPathNavigable using the specified args and outputs the result to an XmlWriter.
public:
void Transform(System::Xml::XPath::IXPathNavigable ^ input, System::Xml::Xsl::XsltArgumentList ^ args, System::Xml::XmlWriter ^ output, System::Xml::XmlResolver ^ resolver);
public void Transform (System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList? args, System.Xml.XmlWriter output, System.Xml.XmlResolver? resolver);
public void Transform (System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList args, System.Xml.XmlWriter output, System.Xml.XmlResolver resolver);
member this.Transform : System.Xml.XPath.IXPathNavigable * System.Xml.Xsl.XsltArgumentList * System.Xml.XmlWriter * System.Xml.XmlResolver -> unit
Public Sub Transform (input As IXPathNavigable, args As XsltArgumentList, output As XmlWriter, resolver As XmlResolver)
参数
An object implementing the IXPathNavigable interface. 在 .NET Framework 中,这可以是 XmlNode(一般为 XmlDocument),或者是包含要转换的数据的 XPathDocument。In the .NET Framework, this can be either an XmlNode (typically an XmlDocument), or an XPathDocument containing the data to be transformed.
XsltArgumentList,包含用作转换输入的命名空间限定的参数。An XsltArgumentList containing the namespace-qualified arguments used as input to the transformation.
The XmlWriter to which you want to output.
用于解析 XSLT document() 函数的 XmlResolver。The XmlResolver used to resolve the XSLT document() function. 如果为 null,则不解析 document() 函数。If this is null, the document() function is not resolved.
例外
处理 XSLT 转换时出错。There was an error processing the XSLT transformation.
注意:这是对早期版本行为的更改。Note: This is a change in behavior from earlier versions. 如果使用的是 Microsoft .NET Framework 1.1 版或更早版本,则会引发 XsltException。An XsltException is thrown if you are using Microsoft .NET Framework version 1.1 or earlier.
示例
下面的示例将 XML 文档转换为 HTML 文档。The following example transforms an XML document into an HTML document. 它显示表中每本书的 ISBN、标题和价格。It displays the ISBN, title, and price for each book in a table.
#using
#using
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 = "books.xml";
String^ stylesheet = "output.xsl";
//Load the stylesheet.
XslTransform^ xslt = gcnew XslTransform;
xslt->Load( stylesheet );
//Load the file to transform.
XPathDocument^ doc = gcnew XPathDocument( filename );
//Create an XmlTextWriter which outputs to the console.
XmlTextWriter^ writer = gcnew XmlTextWriter( Console::Out );
//Transform the file and send the output to the console.
xslt->Transform(doc,nullptr,writer,nullptr);
writer->Close();
}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();
}
}Option Strict
Option Explicit
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
End Class
示例使用下列两个输入文件。The sample uses the following two input files.
books.xml
The Autobiography of Benjamin FranklinBenjamin
Franklin
8.99
The Confidence ManHerman
Melville
11.99
The GorgiasPlato
9.99
output.xsl
ISBN | Title | Price |
注解
XslTransform 支持 XSLT 1.0 语法。XslTransform supports the XSLT 1.0 syntax. XSLT 样式表必须包括命名空间声明 xmlns:xsl= http://www.w3.org/1999/XSL/Transform 。The XSLT style sheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.
args与 xsl:param 样式表中定义的元素匹配。The args are matched with the xsl:param elements defined in the style sheet. xsl:output当) 忽略输出到 (时,不支持该元素 XmlWriter xsl:output 。The xsl:output element is not supported when outputting to an XmlWriter (xsl:output is ignored).
转换将应用于整个文档。Transformations apply to the document as a whole. 换句话说,如果你传入文档根节点以外的一个节点,并不能防止转换进程访问已加载文档的所有节点。In other words, if you pass in a node other than the document root node, this does not prevent the transformation process from accessing all nodes in the loaded document. 若要转换节点片段,必须创建一个 XmlDocument 仅包含节点片段的,并将 XmlDocument 其传递给 Transform 方法。To transform a node fragment, you must create an XmlDocument containing just the node fragment and pass that XmlDocument to the Transform method.
下面的示例对节点片段执行转换。The following example performs a transformation on a node fragment.
XslTransform xslt = new XslTransform();
xslt.Load("print_root.xsl");
XmlDocument doc = new XmlDocument();
doc.Load("library.xml");
// Create a new document containing just the node fragment.
XmlNode testNode = doc.DocumentElement.FirstChild;
XmlDocument tmpDoc = new XmlDocument();
tmpDoc.LoadXml(testNode.OuterXml);
// Pass the document containing the node fragment
// to the Transform method.
Console.WriteLine("Passing " + tmpDoc.OuterXml + " to print_root.xsl");
xslt.Transform(tmpDoc, null, Console.Out, null);
该示例使用 library.xml 和 print_root.xsl 文件作为输入,并将以下结果输出到控制台。The example uses the library.xml and print_root.xsl files as input and outputs the following to the console.
Passing Pride And Prejudice to print_root.xsl
Root node is book.
library.xml
Pride And PrejudiceHookprint_root.xsl
Root node is
另请参阅
适用于