如何通过XSD防止XML中的空元素

如何通过XSD防止XML中的空元素,xsd,Xsd,对于xml模式,我是个新手。无论如何,我的问题是: 我有以下元素 <property name="propA">some-value</property> 一些值 我希望我的XSD能够防止出现空元素,例如: <property name="propB" /> <property name="propC"></property> 我如何使用当前的XSD实现这一点,如下所示: <xs:complexType name="pr

对于xml模式,我是个新手。无论如何,我的问题是:

我有以下元素

<property name="propA">some-value</property>
一些值
我希望我的XSD能够防止出现空元素,例如:

<property name="propB" />
<property name="propC"></property>

我如何使用当前的XSD实现这一点,如下所示:

<xs:complexType name="property">
    <xs:simpleContent>
        <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" use="required" />
        </xs:extension>
    </xs:simpleContent>
</xs:complexType>

您可以为此构建自己的simpleType。例如:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> 
    <xs:element name="property" type="property"/>
    <xs:complexType name="property">
        <xs:simpleContent>
            <xs:extension base="nonEmptyString">
                <xs:attribute name="name" type="xs:string" use="required" />
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
    <xs:simpleType name="nonEmptyString">
        <xs:restriction base="xs:string">
            <xs:minLength value="1"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

您可以为此构建自己的simpleType。例如:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> 
    <xs:element name="property" type="property"/>
    <xs:complexType name="property">
        <xs:simpleContent>
            <xs:extension base="nonEmptyString">
                <xs:attribute name="name" type="xs:string" use="required" />
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
    <xs:simpleType name="nonEmptyString">
        <xs:restriction base="xs:string">
            <xs:minLength value="1"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>


当您说“阻止空元素”时,是指阻止渲染空元素,还是指元素必须具有值?如果您是指第二个选项,“required”选项可能会起作用。当您说“阻止空元素”时,您是指阻止渲染空元素,还是指元素必须具有值?如果您是指第二个,则“必需”选项可能会起作用。根据像
(带空格内容)这样的元素是否可接受,您可能还需要指定
。根据像
(带空格内容)这样的元素是否可接受,您可能还需要指定