为什么xmllint突然无法通过验证?
我正在使用 XMLSchema.xsd 来验证一个带有 xmllint 的XML架构。
$ xmllint --version
xmllint: using libxml version 20913
$ xmllint --schema test/XMLSchema.xsd oec.xsd --load-trace --noout
Loaded URL="test/XMLSchema.xsd" ID="(null)"
Loaded URL="http://www.w3.org/2001/xml.xsd" ID="(null)"
Loaded URL="oec.xsd" ID="(null)"
oec.xsd validates
但是在升级 xmllint 版本时:
$ xmllint --version
xmllint: using libxml version 21502
compiled with: Threads Tree Output Push Reader Patterns Writer SAXv1 DTDValid HTML C14N Catalog XPath XPointer XInclude Iconv ISO8859X Regexps Automata RelaxNG Schemas Schematron Modules Debug Zlib
$ xmllint --schema test/XMLSchema.xsd oec.xsd --load-trace --noout
Loaded URL="test/XMLSchema.xsd" ID="(null)"
I/O warning : failed to load "http://www.w3.org/2001/xml.xsd": No such file or directory
test/XMLSchema.xsd:87: element import: Schemas parser warning : Element '{http://www.w3.org/2001/XMLSchema}import': Failed to locate a schema at location 'http://www.w3.org/2001/xml.xsd'. Skipping the import.
test/XMLSchema.xsd:264: element attribute: Schemas parser error : attribute use (unknown), attribute 'ref': The QName value '{http://www.w3.org/XML/1998/namespace}lang' does not resolve to a(n) attribute declaration.
test/XMLSchema.xsd:1426: element attribute: Schemas parser error : attribute use (unknown), attribute 'ref': The QName value '{http://www.w3.org/XML/1998/namespace}lang' does not resolve to a(n) attribute declaration.
WXS schema test/XMLSchema.xsd failed to compile
我不知道该怎么理解。我的变通方法是把 xml.xsd 下载到本地,并把 XMLSchema.xsd 中的 schemaLocation 属性改为本地引用,但我想避免编辑该文件,并更好地控制 schemaLocation 的逻辑。
解决方案
这通常归结于libxml2在获取外部模式导入时变得更加严格。在较新版本中,xmllint如果不能通过正确的目录解析或网络访问被允许/可用,就无法可靠地解析 http://www.w3.org/2001/xml.xsd。较旧的版本则更宽松,默默地处理了它。
所以发生的情况是:导入失败 → xml.xsd未被加载 → xml:lang属性的定义永远无法解析 → 模式编译失败。
最佳修复是使用XML目录(catalog)来替代改写schemaLocation,或者确保网络访问并启用目录支持。这样可以保持你的模式不变,并使解析具有确定性。
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。