Xml 如何仅对某些元素进行验证

Xml 如何仅对某些元素进行验证,xml,xsd,xsd-validation,Xml,Xsd,Xsd Validation,我有一个xsd,其中定义了一些元素,下面给出了一个示例 <xs:complexType name="carType"> <xs:complexContent> <xs:extension base="vehicleType"> <xs:all> <xs:element name="mode">

我有一个xsd,其中定义了一些元素,下面给出了一个示例

<xs:complexType name="carType">
        <xs:complexContent>
            <xs:extension base="vehicleType">
                <xs:all>
                    <xs:element name="mode">
                        <xs:complexType>
                            <xs:choice>
                                <xs:element name="off"/>
                                <xs:element name="driving"/>
                            </xs:choice>
                        </xs:complexType>
                    </xs:element>
                    <xs:element ref="speed" minOccurs="0"/>
                    <xs:element ref="properties"/>
                </xs:all>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
    <xs:element name="CarA" type="carType" substitutionGroup="vehicleType"/>
<xs:element name="CarB" substitutionGroup="vehicleType"/>

如果有任何xml包含除CarA和CarB之外的元素,我想跳过验证

<CarC>
    <myCarType/>
</CarC>


目前我得到的是“cvc elt.1:找不到元素“CarC”的声明。”

这取决于您用来调用模式验证器的API。寻找“lax验证”选项。如果使用Saxon s9api接口,可以通过设置
SchemaValidator.setLax(true)
来调用lax验证。当然,您选择的模式处理器很可能不提供此选项

Lax验证意味着“如果模式包含相关元素的声明,则根据该声明进行验证;否则将内容视为有效。”