XSD 属性 NILLABLE 不起作用
我正在努力获取一个 xml 文件来根据 XSD 架构进行验证,但我在验证时遇到了问题。每次验证时,我都会收到错误消息
“架构有效性错误:元素'{http://services.website.com/ProgramResponse}Population':''不是原子类型'xs:double'的有效值。”
我相信发生此错误是因为该字段中有一个空字符,显示如下:
< HarvPop>< /HarvPop>
因此,为了解决这个问题,我尝试对元素使用 nillable="true" 属性,这样它们就可以为空,但仍然显示为空。这似乎是唯一的解决方案,但根本不起作用。我仍然收到错误。
我目前正在使用 XMLMate 进行验证,并且我还对几个在线验证器进行了双重检查。错误仍然存在。任何建议都会很棒。
<?xml version="1.0" encoding="UTF-8"?>
<xsd:element name="Reports" type="tns:ReportsType"/>
<xsd:complexType name="ReportsType">
<xsd:sequence>
<xsd:element name="Report" type="tns:ReportType" maxOccurs="unbounded" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="ReportType">
<xsd:sequence>
<xsd:element name="Id" nillable="true"/>
<xsd:element name="Brand" type="xsd:string"/>
<xsd:element name="Address" type="xsd:string"/>
<xsd:element name="City" type="xsd:string"/>
<xsd:element name="State" type="xsd:string"/>
<xsd:element name="ZipCode" type="xsd:string"/>
<xsd:element name="Entry" type="tns:EntryType" maxOccurs="unbounded" minOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="EntryType">
<xsd:sequence>
<xsd:element name="RM" nillable="true" type="xsd:double"/>
<xsd:element name="Pop" nillable="true" type="xsd:double"/>
<xsd:element name="Wt" nillable="true" type="xsd:double"/>
<xsd:element name="EntryId" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
I am working on getting an xml file to validate against an XSD schema and I'm having trouble with the validations. Every time I validate I get errors saying
"Schemas validity error: Element '{http://services.website.com/ProgramResponse}Population': '' is not a valid value of the atomic type 'xs:double'."
I believe this error happens because I have a null character in that field, displayed like this:
< HarvPop>< /HarvPop>
So, to solve this I tried using the nillable="true" attribute for the elements so they will be able to be null, but still show up as empty. This seems to be the only solution, but it is not working at all. I still get the errors.
I am currently using XMLMate for my validations and I have double checked it agains several online verifiers as well. The error still persists. Any suggestions would be great.
<?xml version="1.0" encoding="UTF-8"?>
<xsd:element name="Reports" type="tns:ReportsType"/>
<xsd:complexType name="ReportsType">
<xsd:sequence>
<xsd:element name="Report" type="tns:ReportType" maxOccurs="unbounded" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="ReportType">
<xsd:sequence>
<xsd:element name="Id" nillable="true"/>
<xsd:element name="Brand" type="xsd:string"/>
<xsd:element name="Address" type="xsd:string"/>
<xsd:element name="City" type="xsd:string"/>
<xsd:element name="State" type="xsd:string"/>
<xsd:element name="ZipCode" type="xsd:string"/>
<xsd:element name="Entry" type="tns:EntryType" maxOccurs="unbounded" minOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="EntryType">
<xsd:sequence>
<xsd:element name="RM" nillable="true" type="xsd:double"/>
<xsd:element name="Pop" nillable="true" type="xsd:double"/>
<xsd:element name="Wt" nillable="true" type="xsd:double"/>
<xsd:element name="EntryId" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像
< 这样的节点HarvPop>< /HarvPop>
表示该值存在并且它的值是一个空字符串。基于此 w3.org 页面上的信息:http://www.w3.org /TR/xmlschema-0/#Nils
nillable 属性的使用方式如下:
定义:
用法:
即您必须明确声明该值为空。
另一种方法是声明 minoccurrs = 0,以允许该值缺失。
A node like
< HarvPop>< /HarvPop>
is stating that the value is there and that it's value is an empty string.Based on the information on this w3.org page: http://www.w3.org/TR/xmlschema-0/#Nils
The nillable attribute is used like this:
Definition:
<xsd:element name="shipDate" type="xsd:date" nillable="true"/>
Usage:
<shipDate xsi:nil="true"></shipDate>
ie You have to specifically state that the value is null.
The other way to do it is to state minoccurs = 0, to allow the value to be missing.
我发现也可以用最少的代码解决问题的一种方法是将 default="0" 属性添加到 XSD。这允许您验证双精度值,而无需通过将 nil 默认为数字来处理 nil。
使用强类型 XSD 反序列化 XML 文档时出错
One way that I found that also fixes the problem with minimal code was to add the default="0" attribute to the XSD. This allows you to validate as a double without having to deal with nil by making nil default to a number.
Error deserialising XML document with strongly typed XSD