微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

元素为零且必须为空

如何解决元素为零且必须为空

我有一个旧的 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模式验证失败,报错“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 缺陷。但是,有两点让我对这个结论持谨慎态度:

  1. 使用带有空标签的 xsi:nil 是标准用法。如果这样的缺陷在像 xerces-c 一样古老的 XML 处理器中仍然存在,我会感到非常惊讶。
  2. 您说 <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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。