Xml 如何使用simpleContent验证complexType的非空值

Xml 如何使用simpleContent验证complexType的非空值,xml,xsd,Xml,Xsd,我有一个包含以下内容的XML文件: <farm> <propertybag name="name1">Value1</propertybag> <propertybag name="name2">Value2</propertybag> <propertybag name="name3"></propertybag> </farm> 我试着用平常的方式: <xs:res

我有一个包含以下内容的XML文件:

<farm>
    <propertybag name="name1">Value1</propertybag>
    <propertybag name="name2">Value2</propertybag>
    <propertybag name="name3"></propertybag>
</farm>
我试着用平常的方式:

<xs:restriction base="xs:string">
    <xs:minLength value="1"/>
    <xs:whiteSpace value="collapse"/>
</xs:restriction>

但它不起作用,我尝试了几种解决方案,但没有成功。
欢迎任何帮助。

因此我最终找到了一个解决方案,我创建了一个simpleType来验证属性和元素是否为空或空白:

<xs:element name="farm" minOccurs="0" maxOccurs="1">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="propertybag" type="propertyBag" minOccurs="1" maxOccurs="unbounded">
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>



<xs:simpleType name="propertyValue">
    <xs:restriction base="xs:string">
        <xs:minLength value="1"/>
        <xs:whiteSpace value="collapse"/>
    </xs:restriction>
</xs:simpleType>

如果您有
则需要将基类型声明为say
base=“my:non-empty string”
,其中
non-empty string
定义为使用
minLength=“1”
限制
xs:string
的类型

<xs:element name="farm" minOccurs="0" maxOccurs="1">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="propertybag" type="propertyBag" minOccurs="1" maxOccurs="unbounded">
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>



<xs:simpleType name="propertyValue">
    <xs:restriction base="xs:string">
        <xs:minLength value="1"/>
        <xs:whiteSpace value="collapse"/>
    </xs:restriction>
</xs:simpleType>