元素为零且必须为空
element is nil and must be empty
我有一个遗留的 c++ 应用程序,它使用 XML 与元素的以下 xml 架构进行通信。
<xs:simpleType name="CountryType">
<xs:restriction base="xs:string">
<xs:minLength value="2"/>
<xs:maxLength value="3"/>
<xs:whiteSpace value="collapse"/>
<xs:enumeration value="ABW"/>
<xs:enumeration value="ALB"/>
<xs:enumeration value="ALG"/>
<xs:enumeration value="AND"/>
...
</xs:restriction>
</xs:simpleType>
<xs:element name="Country" nillable="true">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="CountryType">
<xs:attribute ref="searchCriteria" use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
即使nillable设置为true,但是当
<Country xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
传入,xerces-c schema验证失败,报错“element 'Country' is nil and must be empty”。
为什么会失败? CountryType 的 minLength 为 2,所以我认为应该限制它为空,但是当输入为空并且接受以下内容时,模式验证不会抱怨。
<Country></Country>
有什么办法让它接受 nil 吗?
<Country xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
使用的xerces-c库是3.0版本。架构解析器设置为:
XercesDOMParser *schemaParser = null;
schemaParser = new XercesDOMParser();
schemaParser->setDoNamespaces(true);
schemaParser->setDoSchema(true);
schemaParser->setValidationScheme(XercesDOMParser::Val_Always);
schemaParser->setExternalNoNamespaceSchemaLocation(getSchemaFile());
schemaParser->setIncludeIgnorableWhitespace(false);
schemaParser->cacheGrammarFromParse(true);
XML 文档根据该架构有效。我已经使用在线 XSD 验证器进行了检查。所以乍一看,这看起来像一个 xerces-c 缺陷。但是,有两件事让我对这个结论持谨慎态度:
- 使用带空标签的 xsi:nil 是标准用法。如果这样的缺陷在像 xerces-c 这样古老的 XML 处理器中幸存至今,我会感到非常惊讶。
- 你说
<Country></Country>
被接受了。根据 XML 规范,<Country/>
和 <Country></Country>
之间没有区别。 (参见 https://www.w3.org/TR/xml/#sec-starttags,规则 [43])。同样,xerces-c 可能在这方面有缺陷,但这会有点令人惊讶(无论如何对我来说)。
您可能使用的是非常旧的 xerces-c 版本,存在一些未修复的缺陷 - 可能值得更新到最新版本。如果那不能解决问题,那么我会联系 xerces-c 的维护者:https://xerces.apache.org/xerces-c/feedback.html
我有一个遗留的 c++ 应用程序,它使用 XML 与元素的以下 xml 架构进行通信。
<xs:simpleType name="CountryType">
<xs:restriction base="xs:string">
<xs:minLength value="2"/>
<xs:maxLength value="3"/>
<xs:whiteSpace value="collapse"/>
<xs:enumeration value="ABW"/>
<xs:enumeration value="ALB"/>
<xs:enumeration value="ALG"/>
<xs:enumeration value="AND"/>
...
</xs:restriction>
</xs:simpleType>
<xs:element name="Country" nillable="true">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="CountryType">
<xs:attribute ref="searchCriteria" use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
即使nillable设置为true,但是当
<Country xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
传入,xerces-c schema验证失败,报错“element 'Country' is nil and must be empty”。
为什么会失败? CountryType 的 minLength 为 2,所以我认为应该限制它为空,但是当输入为空并且接受以下内容时,模式验证不会抱怨。
<Country></Country>
有什么办法让它接受 nil 吗?
<Country xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
使用的xerces-c库是3.0版本。架构解析器设置为:
XercesDOMParser *schemaParser = null;
schemaParser = new XercesDOMParser();
schemaParser->setDoNamespaces(true);
schemaParser->setDoSchema(true);
schemaParser->setValidationScheme(XercesDOMParser::Val_Always);
schemaParser->setExternalNoNamespaceSchemaLocation(getSchemaFile());
schemaParser->setIncludeIgnorableWhitespace(false);
schemaParser->cacheGrammarFromParse(true);
XML 文档根据该架构有效。我已经使用在线 XSD 验证器进行了检查。所以乍一看,这看起来像一个 xerces-c 缺陷。但是,有两件事让我对这个结论持谨慎态度:
- 使用带空标签的 xsi:nil 是标准用法。如果这样的缺陷在像 xerces-c 这样古老的 XML 处理器中幸存至今,我会感到非常惊讶。
- 你说
<Country></Country>
被接受了。根据 XML 规范,<Country/>
和<Country></Country>
之间没有区别。 (参见 https://www.w3.org/TR/xml/#sec-starttags,规则 [43])。同样,xerces-c 可能在这方面有缺陷,但这会有点令人惊讶(无论如何对我来说)。
您可能使用的是非常旧的 xerces-c 版本,存在一些未修复的缺陷 - 可能值得更新到最新版本。如果那不能解决问题,那么我会联系 xerces-c 的维护者:https://xerces.apache.org/xerces-c/feedback.html