【发布时间】:2021-08-01 07:12:02
【问题描述】:
我有一个使用 XML 进行通信的旧版 c++ 应用程序,其中一个元素具有以下 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);
【问题讨论】: