Saxon XSLT .Net转换:当xml和xsl都作为字符串传递时,在BaseURI中给出什么

时间:2013-10-12 02:01:55

标签: xslt xslt-1.0 xslt-2.0 saxon

这是我对Saxon Transformation of XSLT文件的代码,它接受xml和xslt并返回一个转换后的字符串。我可以通过此函数处理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();
}

1 个答案:

答案 0 :(得分:1)

如果您认为永远不会使用基URI(因为您从不做任何依赖于基本URI的事情),那么最好的策略是设置一个基本URI,如果您的假设结果是错误的话,该URI将立即被识别,例如“file:/// dummy / base / uri”。

选择合法URI(C:\不是)。