これは、xml と xslt を受け入れ、変換された文字列を返す XSLT ファイルの Saxon Transformation 用のコードです。この関数で xsl 1.0 または 2.0 を処理できます。
DocumentBuilder
BaseURI
ファイル形式がなくても、が必要です。このディレクトリとは何の関係もないにもかかわらず、私は"c:\\"
として提供しました。BaseURI
このことを達成したり、この関数を書いたりするより良い方法はありますか?
public static string SaxonTransform(string xmlContent, string xsltContent)
{
// Create a Processor instance.
Processor processor = new Processor();
// Load the source document into a DocumentBuilder
DocumentBuilder builder = processor.NewDocumentBuilder();
Uri sUri = new Uri("c:\\");
// Now set the baseUri for the builder we created.
builder.BaseUri = sUri;
// Instantiating the Build method of the DocumentBuilder class will then
// provide the proper XdmNode type for processing.
XdmNode input = builder.Build(new StringReader(xmlContent));
// Create a transformer for the stylesheet.
XsltTransformer transformer = processor.NewXsltCompiler().Compile(new StringReader(xsltContent)).Load();
// Set the root node of the source document to be the initial context node.
transformer.InitialContextNode = input;
StringWriter results = new StringWriter();
// Create a serializer.
Serializer serializer = new Serializer();
serializer.SetOutputWriter(results);
transformer.Run(serializer);
return results.ToString();
}