Home > other >  How to force a comlpexType element to have at least 3 characters in a string?
How to force a comlpexType element to have at least 3 characters in a string?

Time:09-16

Working in XML 1.1. I am tryping to restrict a XML-schema for an element of complexType that will include a string to have at least 3 characters. How do I do that?

My aim is to have an element that is a complexType, so it includes some further elements as well as attributes to be used. The aim is now that the content of this element itself must not be empty. Ideally I would like to have something like this:

<text type='question'><condition extended='playerMoved' keyword='game'/>Did you already <keyword>move</keyword>?</text>

I want it so that an element without any text inside is invalid so something like this should be forbidden:

<text type='question'><condition extended='playerMoved' keyword='game'/></text>

My problem now is that all solutions I have tried only work with simpleTypes but I cannot bring them to use on a complexType. I have already tried the following ideas but they all only throw error messages and don't compile:

1.) Restriction in element itself:

<xs:element name="text">
    <xs:complexType mixed="true">
        <xs:simpleContent>
            <xs:restriction base='xs:string'>
                <xs:minLength value="3"/>
            </xs:restriction>
        </xs:simpleContent>
        <xs:sequence>
            <xs:choice minOccurs="0" maxOccurs="unbounded">
                <xs:element name="condition" type="condition" minOccurs="0" maxOccurs="unbounded"/>
                ...
            </xs:choice>
        </xs:sequence>
        <xs:attribute name="type" use="required">
            <xs:simpleType>
                ...
            </xs:simpleType>
        </xs:attribute>
    </xs:complexType>
</xs:element>

2.) Restriction by a complexType of simpleContent:

 <xs:element name="text">
    <xs:complexType mixed="true">
        <xs:simpleContent>
            <xs:restriction base='textString'>
                <xs:minLength value="3"/>
            </xs:restriction>
        </xs:simpleContent>
        <xs:sequence>
            ...
        </xs:sequence>
        <xs:attribute name="type" use="required">
            ...
        </xs:attribute>
    </xs:complexType>
</xs:element>

...

<xs:complexType name="textString">
    <xs:simpleContent>
        <xs:extension base="xs:string"/>
    </xs:simpleContent>
</xs:complexType>

None of these work and since I was not able to find a way how to do it by an assert I am seriously stuck and don't know how to continue. How can I get my schema to have a string of at least 3 characters mandatory?

CodePudding user response:

Put an assertion like <xs:assert id="test-length" test="string-length() ge 3"></xs:assert> into the xs:complexType element.

  • Related