这是我的代码:
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) 在Saxon.Program.TransformData(String data,String xmlCopy,String xslt)在C:\ Users \Davíð\ source \ Saxon \ Saxon \ Program.cs:line 33中 在C:\ Users \Davíð\ source \ Saxon \ Saxon \ Program.cs:line 22中的Saxon.Program.Main(String [] args)处
基于下面的注释,固定的工作代码:
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
}
答案 0 :(得分:0)
您遇到了一些问题:
processor.NewXsltCompiler()
都会返回一个新的XsltCompiler
。您称它为两次:
processor.NewXsltCompiler().BaseUri = new Uri("C:/Users/Davíð/source/Saxon/Saxon");
XsltExecutable templates = processor.NewXsltCompiler().Compile(new XmlTextReader(new StringReader(xslt)));
代码首先设置BaseUri
,然后创建另一个代码进行编译。因此,永远不会在实际用于工作的BaseUri
上设置XsltCompiler
。
这是导致异常的直接原因。未处理的异常:System.ArgumentNullException:值不能为null。参数名称:Saxon.Api.XsltCompiler.Compile(XmlReader reader)处的BaseUri 。
您应该只创建一个XsltCompiler
。
您正在使用XmlTextReader
,但是XmlTextReader
has been deprecated since .Net 2.0已有10多年的历史了。而是使用XmlReader.Create(TextReader)
。另外,请确保事后将其丢弃。
您将文件data.xml
的内容的两个副本加载到内存xml
和xmlCopy
中。但是.Net中的字符串是不可变的,因此不需要这样做。
因此您的代码应类似于:
public 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 xml1, string xml2, string xslt)
{
// Create a Processor instance.
var processor = new Processor();
// Create a compiled stylesheet
var compiler = processor.NewXsltCompiler();
compiler.BaseUri = new Uri("C:/Users/Davíð/source/Saxon/Saxon");
XsltExecutable templates;
using (var reader = XmlReader.Create(new StringReader(xslt)))
templates = compiler.Compile(reader);
// 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 " + xml1 + " -----");
TransformData(processor, templates, xml1, Console.Out);
// Do the second transformation
Console.WriteLine("\n\n----- transform of " + xml2 + " -----");
TransformData(processor, templates, xml2, Console.Out);
}
private static void TransformData(Processor processor, XsltExecutable templates, string xml, TextWriter output)
{
var transformer = templates.Load30();
using (var reader = XmlReader.Create(new StringReader(xml)))
{
var input = processor.NewDocumentBuilder().Build(reader);
transformer.ApplyTemplates(input, processor.NewSerializer(output));
}
}