【问题标题】:XSD attribute NILLABLE not workingXSD 属性 NILLABLE 不起作用
【发布时间】:2011-10-13 11:18:11
【问题描述】:

我正在获取一个 xml 文件以针对 XSD 架构进行验证,但我在验证时遇到了问题。每次我验证我都会收到错误提示

“架构有效性错误:元素 '{http://services.website.com/ProgramResponse}Population': '' 不是原子类型 'xs:double' 的有效值。”

我认为发生此错误是因为我在该字段中有一个空字符,显示如下:

所以,为了解决这个问题,我尝试对元素使用 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>

【问题讨论】:

    标签: xml xsd xsd-validation


    【解决方案1】:

    像&lt; HarvPop&gt;&lt; /HarvPop&gt; 这样的节点表明该值存在并且它的值是一个空字符串。

    基于此 w3.org 页面上的信息:http://www.w3.org/TR/xmlschema-0/#Nils

    nillable 属性是这样使用的:

    定义: &lt;xsd:element name="shipDate" type="xsd:date" nillable="true"/&gt;

    用法: &lt;shipDate xsi:nil="true"&gt;&lt;/shipDate&gt;

    ie 你必须明确声明该值为空。

    另一种方法是声明 minoccurs = 0,以允许缺少值。

    【讨论】:

    • 或者,将类型从 xs:double 更改为 itemType 为 xs:double 的列表类型,其中 minLength=0 和 maxLength=1。这样你就可以省略没有多余 xsi:nil 属性的值。
    • 在使用 minOccurs 时是否必须在 xml 中包含 xsi:nil="true"?
    • 没有。 nil 表示您可以让节点存在且为空,minOccurs=0 表示整个节点可能会丢失。
    【解决方案2】:

    我发现也可以用最少的代码解决问题的一种方法是将 default="0" 属性添加到 XSD。这允许您通过将 nil 默认为数字来验证作为双精度数,而无需处理 nil。

    Error deserialising XML document with strongly typed XSD

    【讨论】:

    • 解决了我的问题。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-06
    • 1970-01-01
    • 2011-03-31
    • 2018-11-19
    • 2012-07-23
    • 2016-07-16
    • 2020-01-24
    相关资源
    最近更新 更多