【问题标题】:Schema Validator "nillable" For Child Nodes Doesn't Work?子节点的模式验证器“nillable”不起作用?
【发布时间】:2011-01-14 01:02:09
【问题描述】:

由于某种原因,我无法完全让 nillable 与 .Net 架构验证器一起正常工作。我正在尝试找到一种方法使父节点成为可选,但同时防止空节点通过验证器。

这是当前元素验证器:

    <xs:element name="Dates" minOccurs="0" maxOccurs="1">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="From" type="datetime" minOccurs="0" maxOccurs="1" />
          <xs:element name="To" type="datetime" minOccurs="0" maxOccurs="1" />
        </xs:sequence>
      </xs:complexType>
    </xs:element>

我已尝试将 Dates 元素更改为 nillable="false" 但这不起作用 - 一个空节点仍然可以通过验证器。

我还尝试将所有三个元素节点都更改为 nillable="false" - 这对于检测空父节点非常有效,但会导致两个子节点都成为必需节点,而不是保持可选节点。

那么我在这里遗漏了什么吗?是的,我总是可以向它抛出一些代码并使其工作......但我敢打赌,架构声明中的变化将给我我需要的东西。

【问题讨论】:

    标签: .net validation xsd


    【解决方案1】:

    您的情况的解决方案是“多项选择”:

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xs:element name="root">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="Dates" minOccurs="0" maxOccurs="1">
                        <xs:complexType>
                            <xs:choice minOccurs="1" maxOccurs="2">
                                <xs:choice maxOccurs="1">
                                    <xs:element name="From" type="xs:dateTime" />
                                </xs:choice>
                                <xs:choice maxOccurs="1">
                                    <xs:element name="To" type="xs:dateTime" />
                                </xs:choice>
                            </xs:choice>
                        </xs:complexType>
                    </xs:element>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:schema>
    

    有效文件

    <?xml version="1.0"?>
    <root>
        <Dates>
            <From>2010-01-20T12:34:45</From>
            <To>2011-01-20T12:34:45</To>
        </Dates>
    </root>
    
    
    <?xml version="1.0"?>
    <root>
        <Dates>
            <From>2010-01-20T12:34:45</From>
        </Dates>
    </root>
    
    
    <?xml version="1.0"?>
    <root>
        <Dates>
            <To>2011-01-20T12:34:45</To>
        </Dates>
    </root>
    

    无效文档

    <?xml version="1.0"?>
    <root>
        <Dates/>
    </root>
    

    简单的方法

    <xs:element name="Dates" minOccurs="0" maxOccurs="1">
        <xs:complexType>
            <xs:sequence>
                <xs:any minOccurs="1" maxOccurs="2" processContents="lax" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    

    执行&lt;From /&gt;&lt;To /&gt; 的唯一方法是使用特殊的命名空间。

    【讨论】:

    • 我认为非常接近,但您的第一个示例(带有 From/To 节点)验证失败(请参阅xmlforasp.net/SchemaValidator.aspx)。这看起来像是朝着正确的方向前进,但是当我在架构验证器站点上摆弄您的架构时,我只会让事情变得更糟。 :-)
    • @jerhewet 你确实是对的。我发布的版本缺少外部choicemaxOccurs="2"
    • 达曼!或者也许是大女人——但无论哪种情况/性别,这都是我想要的!非常开心的露营者!
    猜你喜欢
    • 2018-09-08
    • 1970-01-01
    • 1970-01-01
    • 2018-10-21
    • 2018-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-30
    相关资源
    最近更新 更多