开发者

Schema Validator "nillable" For Child Nodes Doesn't Work?

开发者 https://www.devze.com 2023-02-04 11:57 出处:网络
For some reason I can\'t quite get nillable to work correctly with the .Net schema validator.I\'m trying to find a way to make a parent node optional but at the same time prevent an empty node from pa

For some reason I can't quite get nillable to work correctly with the .Net schema validator. I'm trying to find a way to make a parent node optional but at the same time prevent an empty node from passing through the validator.

Here's the current element validator:

    <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>

I've tried changing the Dates element to nillable="false" but that doesn't work -- an empty node still makes it past the validator.

I've also tried changing all three element nodes to nillable="false" -- which works fine for detecting an empty parent node but results in both children becoming required nodes instead of remaining optional.

So am I missing something here? Yes, I can always just t开发者_如何学Pythonhrow some code at it and make it work... but I'm betting that there's a variation here in the schema declarations that will give me what I need.


The solution in your case is "multiple choice":

<?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>

valid documents

<?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>

invalid document

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

The simple way

<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>

Where your only way of enforcing <From /> and <To /> would be using a special namespace.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号