Saxon中的BaseUri在尝试编译xslt内容时引起问题

问题描述 投票:0回答:1

这是我的代码:

static void Main(string[] args)
{
    string xml = File.ReadAllText("C:/Users/Davíð/source/Saxon/Saxon/data.xml");
    string xslt = File.ReadAllText("C:/Users/Davíð/source/Saxon/Saxon/dataXslt.xsl");
    string xmlCopy = File.ReadAllText("C:/Users/Davíð/source/Saxon/Saxon/data.xml");

    TransformData(xml, xmlCopy, xslt);
}

static public void TransformData(string data, string xmlCopy, string xslt)
{
    // Create a Processor instance.
    Processor processor = new Processor();

    // Create a compiled stylesheet
    processor.NewXsltCompiler().BaseUri = new Uri("C:/Users/Davíð/source/Saxon/Saxon");
    XsltExecutable templates = processor.NewXsltCompiler().Compile(new XmlTextReader(new StringReader(xslt)));

    // Note: we could actually use the same Xslt30Transformer in this case.
    // But in principle, the two transformations could be done in parallel in separate threads.



    // Do the first transformation
    Console.WriteLine("\n\n----- transform of " + data + " -----");
    Xslt30Transformer transformer1 = templates.Load30();
    XdmNode input1 = processor.NewDocumentBuilder().Build(new XmlTextReader(new StringReader(data)));
    transformer1.ApplyTemplates(input1, processor.NewSerializer(Console.Out));     // default destination is Console.Out

    // Do the second transformation
    Console.WriteLine("\n\n----- transform of " + xmlCopy + " -----");
    Xslt30Transformer transformer2 = templates.Load30();
    XdmNode input2 = processor.NewDocumentBuilder().Build(new XmlTextReader(new StringReader(xmlCopy)));
    transformer2.ApplyTemplates(input2, processor.NewSerializer(Console.Out));     // default destination is Console.Out
}

无论是否删除BaseUri,这都是我不断收到的错误。

未处理的异常:System.ArgumentNullException:值不能为null。参数名称:BaseUri在Saxon.Api.XsltCompiler.Compile(XmlReader reader)在C:\ Users \Davíð\ source \ Saxon \ Saxon \ Program.cs:line 33中的Saxon.Program.TransformData(String data,String xmlCopy,String xslt)在Saxon.Program.Main(String [] args)在C:\ Users \Davíð\ source \ Saxon \ Saxon \ Program.cs:line 22

下面的工作代码:

static void Main(string[] args)

{
    string xml = File.ReadAllText("C:/Users/Davíð/source/Saxon/Saxon/data.xml");
    string xslt = File.ReadAllText("C:/Users/Davíð/source/Saxon/Saxon/dataXslt.xsl");

    TransformData(xml, xml, xslt);
}

static public void TransformData(string data, string xmlCopy, string xslt)
{
    // Create a Processor instance.
    Processor processor = new Processor();

    // Create a compiled stylesheet
    var compiler = processor.NewXsltCompiler();
    compiler.BaseUri = new Uri("C:/Users/Davíð/source/Saxon/Saxon");
    XsltExecutable templates = compiler.Compile(XmlReader.Create(new StringReader(xslt)));

    // Note: we could actually use the same Xslt30Transformer in this case.
    // But in principle, the two transformations could be done in parallel in separate threads.



    // Do the first transformation
    Console.WriteLine("\n\n----- transform of " + data + " -----");
    Xslt30Transformer transformer1 = templates.Load30();
    XdmNode input1 = processor.NewDocumentBuilder().Build(XmlReader.Create(new StringReader(data)));
    transformer1.ApplyTemplates(input1, processor.NewSerializer(Console.Out));     // default destination is Console.Out

    // Do the second transformation
    Console.WriteLine("\n\n----- transform of " + xmlCopy + " -----");
    Xslt30Transformer transformer2 = templates.Load30();
    XdmNode input2 = processor.NewDocumentBuilder().Build(new XmlTextReader(new StringReader(xmlCopy)));
    transformer2.ApplyTemplates(input2, processor.NewSerializer(Console.Out));     // default destination is Console.Out
}
c# xml xslt uri saxon
1个回答
0
投票

您有几个问题:

    每次调用
  • processor.NewXsltCompiler()都会返回一个新的processor.NewXsltCompiler()。您称它为[[twice:

    XsltCompiler
    代码首先设置为processor.NewXsltCompiler().BaseUri = new Uri("C:/Users/Davíð/source/Saxon/Saxon");
    XsltExecutable templates = processor.NewXsltCompiler().Compile(new XmlTextReader(new StringReader(xslt)));
    ,然后创建另一个进行编译。因此,永远不会在实际用于工作的BaseUri上设置BaseUri

    这是异常的直接原因

    未处理的异常:System.ArgumentNullException:值不能为null。参数名称:Saxon.Api.XsltCompiler.Compile(XmlReader reader)处的BaseUri。

    您应该只创建一个BaseUri
  • 您正在使用XsltCompiler,但是XsltCompiler已使用10多年了。而是使用XmlTextReader。另外,请确保事后将其丢弃。
  • 您将文件XmlTextReader has been deprecated since .Net 2.0的内容的两个副本加载到内存中,分别存储在字符串XmlTextReaderXmlReader.Create(TextReader)中。但是.Net中的字符串是不可变的,因此无需执行此操作。

因此您的代码应类似于:

XmlReader.Create(TextReader)

© www.soinside.com 2019 - 2024. All rights reserved.