在SaxonCS 12.5中编译含<xsd:import/>的XML Schema时触发NullReferenceException
在.NET 8控制台应用中使用SaxonCS 12.5 EE版(持有站点许可证),调用Saxon.Api.Processor.SchemaManager.Compile()编译包含外部Schema导入的XML Schema时,触发NullReferenceException。该Schema在.NET原生XmlSchemaSet、其他校验工具及旧版SaxonEE 9.7(IKVM实现)中均可正常编译通过。
可重现示例文件
Schema.xsd
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:import schemaLocation="Schema2.xsd" namespace="http://www.w3.org/2005/xpath-functions" /> <xsd:element name="elem" type="xsd:string" /> </xsd:schema>
Schema2.xsd
<xsd:schema targetNamespace="http://www.w3.org/2005/xpath-functions" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="elem2" type="xsd:string" /> </xsd:schema>
saxon-config.xml
<configuration xmlns="http://saxon.sf.net/ns/configuration" edition="EE" licenseFileLocation="saxon-license.lic" />
触发异常的C#代码
string schemaPath = System.IO.Path.Combine(System.AppContext.BaseDirectory, "Schema.xsd"); string configPath = System.IO.Path.Combine(System.AppContext.BaseDirectory, "saxon-config.xml"); using System.IO.FileStream configStream = System.IO.File.OpenRead(configPath); Saxon.Api.Processor proc = new(configStream, new System.Uri(configPath)); proc.SchemaManager.Compile(new System.Uri(schemaPath));
抛出的异常信息
System.NullReferenceException: Object reference not set to an instance of an object. at Saxon.Eej.ee.schema.sdoc.XSDImport.prepareAttributes() at Saxon.Eej.ee.schema.sdoc.SchemaElement.processAllAttributes() at Saxon.Eej.ee.schema.sdoc.SchemaElement.processAllAttributes() at Saxon.Eej.ee.schema.sdoc.SchemaReader.read(Source source, SchemaCompiler compiler, PipelineConfiguration pipe, SchemaElement referrer) at Saxon.Eej.config.EnterpriseConfiguration.addSchemaSource(Source schemaSource, ErrorReporter errorReporter, SchemaCompiler compiler) at Saxon.Eej.config.EnterpriseConfiguration.addSchemaSource(Source schemaSource, ErrorReporter errorReporter) at Saxon.Eej.ee.s9api.SchemaManagerImpl.load(Source source) at Saxon.Api.SchemaManager.Compile(Uri uri) at Program.Main(String[] args) in C:\Projects\Test\Program.cs:line 11
可正常编译的.NET原生代码对比
string schemaPath = System.IO.Path.Combine(System.AppContext.BaseDirectory, "Schema.xsd"); System.Xml.Schema.XmlSchemaSet xss = new(); xss.Add(null, schemaPath); xss.Compile();
解决思路
避免标准命名空间冲突:
导入的命名空间http://www.w3.org/2005/xpath-functions是Saxon默认内置的XPath函数命名空间,手动导入自定义的同名Schema可能引发内部逻辑冲突。尝试将Schema2.xsd的targetNamespace修改为自定义非标准命名空间,再重新编译测试。修改Schema加载方式:
替换直接调用Compile(Uri)的方式,先显式创建SchemaFileSource对象并加载,再执行编译操作:string schemaPath = Path.Combine(AppContext.BaseDirectory, "Schema.xsd"); string configPath = Path.Combine(AppContext.BaseDirectory, "saxon-config.xml"); using var configStream = File.OpenRead(configPath); var proc = new Processor(configStream, new Uri(configPath)); var schemaSource = new SchemaFileSource(new Uri(schemaPath)); proc.SchemaManager.Load(schemaSource); proc.SchemaManager.Compile();代码方式初始化Processor:
跳过配置文件,直接通过代码配置许可证和版本,确保配置加载完整:string schemaPath = Path.Combine(AppContext.BaseDirectory, "Schema.xsd"); var proc = new Processor(true); proc.SetLicenseFile(new Uri(Path.Combine(AppContext.BaseDirectory, "saxon-license.lic"))); proc.SetEdition(Processor.EnterpriseEdition); proc.SchemaManager.Compile(new Uri(schemaPath));升级SaxonCS版本:
该NullReferenceException可能是SaxonCS 12.5的已知bug,尝试升级至最新稳定版(如12.6及以上),查看官方修复记录是否包含该问题。确认Schema路径解析正确性:
确保Schema2.xsd的路径能被Saxon正确解析,可尝试使用绝对路径作为schemaLocation的值,或者在初始化Processor时明确设置基础URI,保证导入的Schema文件可被找到。
内容的提问来源于stack exchange,提问作者JTennessen

