如何解决如何使用Saxon API从模式感知的xslt获取警告
我有一些c#原型代码,可以编译然后执行XSLT。
var samplesDir = new Uri(AppDomain.CurrentDomain.BaseDirectory);
// Create a Processor instance.
Processor processor = new Processor(true);
var compiler = processor.NewXsltCompiler();
try
{
// Create a transformer for the stylesheet.
Xslt30Transformer transformer = compiler.Compile(new Uri(samplesDir,"po.xsl")).Load30();
// Create a serializer,with output to the standard output stream
Serializer serializer = processor.NewSerializer();
serializer.SetOutputWriter(Console.Out);
// Transform the source XML and serialize the result document
transformer.SchemaValidationMode = SchemaValidationMode.Strict;
transformer.ApplyTemplates(File.OpenRead("po.xml"),serializer);
}
catch (Exception e)
{
Console.WriteLine(e);
Console.WriteLine("Schema compilation failed with " + compiler.ErrorList.Count + " errors");
foreach (StaticError error in compiler.ErrorList)
{
Console.WriteLine("At line " + error.LineNumber + ": " + error.Message);
}
}
和一个XSLT文件
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="text" />
<xsl:import-schema schema-location="file:///C:/Users/m_r_n/source/repos/SaxonEEExample/ValidateXslt/po1.xsd" />
<xsl:template match="schema-element(PurchaseOrder)">
<xsl:for-each select="item">
<xsl:value-of select="@id" />: <xsl:value-of select="title" />
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
<xsl:template match="*">
<xsl:message terminate="yes">Source document is not a purchase order
</xsl:message>
</xsl:template>
</xsl:stylesheet>
如果我将匹配项更改为
<xsl:template match="schema-element(PurchaseOrder1)">
这将正确报告错误
At line 8: There is no declaration for element <PurchaseOrder1> in an imported schema
很棒。
如果我改回原处并将for-each行修改为
<xsl:for-each select="item1">
然后氧气编辑器(使用Saxon EE)正确报告此行的警告
The complex type of element PurchaseOrder does not allow a child element named item1
但是我的C#代码将愉快地编译并执行它(输出一个空字符串)。
如何使Saxon编译器报告警告? 我的代码没有做的氧气在做什么?
解决方法
Saxon可能会将警告输出到控制台,而您看不到控制台上的内容。
通过设置ErrorList
的{{1}}或ErrorReporter
属性(取决于您使用的发行版)来尝试从编译器捕获错误。
这主要是与迈克尔·凯对话的结果的总结。
问题似乎是默认情况下JIT设置为true,并且JIT意味着懒惰地对警告进行了评估,该警告可以在执行时执行。
您可以通过使用神奇的“ -j”选项在处理器上设置属性来覆盖此设置,然后我假设所有创建的编译器都不会懒惰地评估警告,或者,实际上,对我而言,最好进入基础编译器信息,将其设置为关闭(这需要向您的应用添加更多引用,但是编译器会告诉您您需要做什么)。
var samplesDir = new Uri(AppDomain.CurrentDomain.BaseDirectory);
// Create a Processor instance.
Processor processor = new Processor(true);
var compiler = processor.NewXsltCompiler();
// option 1 - set the JIT flag to false in the underlying compiler info
compiler.GetUnderlyingCompilerInfo().setJustInTimeCompilation(false);
// option 2 - set JIT optimization off
//processor.SetProperty("http://saxon.sf.net/feature/optimizationLevel","-j");
try
{
Xslt30Transformer transformer = compiler.Compile(new Uri(samplesDir,"po.xsl")).Load30();
// Create a serializer,with output to the standard output stream
Serializer serializer = processor.NewSerializer();
serializer.SetOutputWriter(Console.Out);
// Transform the source XML and serialize the result document
transformer.SchemaValidationMode = SchemaValidationMode.Strict;
transformer.ApplyTemplates(File.OpenRead("po.xml"),serializer);
}
catch (Exception e)
{
Console.WriteLine(e);
Console.WriteLine("Schema compilation failed with " + compiler.ErrorList.Count + " errors");
foreach (StaticError error in compiler.ErrorList)
{
Console.WriteLine("At line " + error.LineNumber + ": " + error.Message);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。