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

How to validate not empty value of complexType with simpleContent

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

<farm>
    <propertybag name="name1">Value1</propertybag>
    <propertybag name="name2">Value2</propertybag>
    <propertybag name="name3"></propertybag>
</farm>

我只想将我的 XSD 文件设置为不验证第三个元素 name3 因为没有值。

这是我的 XSD 文件:

<xs:element name="farm" minOccurs="0" maxOccurs="1">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="propertybag" minOccurs="1" maxOccurs="unbounded">
                <xs:complexType>
                    <xs:simpleContent>
                        <xs:extension base="xs:string">
                            <xs:attribute name="name" type="xs:string" use="required" />
                        </xs:extension>
                    </xs:simpleContent>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

我试过平时:

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

在你有 <xs:extension base="xs:string"> 的地方,你需要将基类型声明为 base="my:non-empty-string",其中 non-empty-string 被定义为使用 [= 限制 xs:string 的类型14=].