How can I create a scheme that requires certain elements, allows others, and is an agnostic of order?

I want to create an XML schema containing the following:

<xs:complexType name="Record">
        <!--required elements-->
        <xs:element name="RecordTag" type="xs:string" minOccurs="1" maxOccurs="1" />
        <xs:element name="RecordSize" type="xs:string" minOccurs="1" maxOccurs="1" />
        <xs:element name="RecordSection" type="xs:string" minOccurs="1" maxOccurs="1" />

        <!--optional elements-->
        <xs:element name="RecordName" type="xs:string" minOccurs="0" maxOccurs="1" />
        <xs:element name="RecordType" type="xs:string" minOccurs="0" maxOccurs="1" />
        <xs:element name="RecordValue" type="xs:string" minOccurs="0" maxOccurs="1" />
        <xs:element name="RecordDefault" type="xs:string" minOccurs="0" maxOccurs="1" />
        <xs:element name="RecordComment" type="xs:string" minOccurs="0" maxOccurs="1" />
        <xs:element name="RecordURL" type="xs:string" minOccurs="0" maxOccurs="1" />
        <xs:element name="Condition" type="xs:string" minOccurs="0" maxOccurs="1" />
        <xs:element name="Master" type="xs:string" minOccurs="0" maxOccurs="1" />
        <xs:element name="RecordCurrent" type="xs:string" minOccurs="0" maxOccurs="1" />
        <xs:element name="RecordId" type="xs:string" minOccurs="0" maxOccurs="1" />
</xs:complexType>

As you can tell from the comments, I want the top three elements to be required, and the rest are optional. The outline should allow the elements to appear in any order.

Now, if I use the indicator <xs:sequence>, the order is executed, which I do not want. If I use the indicator <xs:all>, then the diagram requires that all elements are displayed, even if the value is minOccursset to 0.

Is there any other indicator that I can use to accomplish my task?

Thank!

+3
source share
1

, XSD ; , , ( xs: all compositor), XML Record.

<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="Record" type="Record"/>
    <xs:complexType name="Record">
        <xs:all>
            <!--required elements-->
            <xs:element name="RecordTag" type="xs:string" minOccurs="1" maxOccurs="1"/>
            <xs:element name="RecordSize" type="xs:string" minOccurs="1" maxOccurs="1"/>
            <xs:element name="RecordSection" type="xs:string" minOccurs="1" maxOccurs="1"/>

            <!--optional elements-->
            <xs:element name="RecordName" type="xs:string" minOccurs="0" maxOccurs="1"/>
            <xs:element name="RecordType" type="xs:string" minOccurs="0" maxOccurs="1"/>
            <xs:element name="RecordValue" type="xs:string" minOccurs="0" maxOccurs="1"/>
            <xs:element name="RecordDefault" type="xs:string" minOccurs="0" maxOccurs="1"/>
            <xs:element name="RecordComment" type="xs:string" minOccurs="0" maxOccurs="1"/>
            <xs:element name="RecordURL" type="xs:string" minOccurs="0" maxOccurs="1"/>
            <xs:element name="Condition" type="xs:string" minOccurs="0" maxOccurs="1"/>
            <xs:element name="Master" type="xs:string" minOccurs="0" maxOccurs="1"/>
            <xs:element name="RecordCurrent" type="xs:string" minOccurs="0" maxOccurs="1"/>
            <xs:element name="RecordId" type="xs:string" minOccurs="0" maxOccurs="1"/>          
        </xs:all>
    </xs:complexType>
</xs:schema>

XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<Record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <RecordId>RecordId1</RecordId>
    <RecordCurrent>RecordCurrent1</RecordCurrent>
    <Master>Master1</Master>
    <Condition>Condition1</Condition>
    <RecordURL>RecordURL1</RecordURL>
    <RecordSection>RecordSection1</RecordSection>
    <RecordSize>RecordSize1</RecordSize>
    <!--
    <RecordTag>RecordTag1</RecordTag>
    -->
</Record>

( .NET):

'Record' . : "RecordTag".

Xerces :

cvc-complex-type.2.4.b: "" . "{RecordTag}".

, , , .

< xs: all > , , minOccurs 0.

XSD 1.0 all. , ; , , , ​​XML- XSD.

+1

Source: https://habr.com/ru/post/1755620/


All Articles