iXô
iXô

Reputation: 1182

XSD for elements string with attribute id

I would like to write a XSD element that permit something like that :

<CustomFields>
    <CustomField id="1">some text data</CustomField>
    <CustomField id="2">some text data</CustomField>
</CustomFields>

But I have some restrictions : I have to restrict the text (maxLenght = 36). And I would like to be impossible to have 2 CustomField with the same id.

So far I wrote this, but it not what I want :

<xs:element name="CustomFields" minOccurs="0">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="CustomField" minOccurs="0" maxOccurs="20">
                <xs:complexType>
                    <xs:simpleContent>
                        <xs:extension base="xs:string">
                            <xs:attribute name="id" type="xs:integer" use="required"></xs:attribute>
                        </xs:extension>
                    </xs:simpleContent>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

Thanks for any help.

Regards.

Upvotes: 0

Views: 70

Answers (1)

Jirka Š.
Jirka Š.

Reputation: 3428

You could use following attempt

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">

    <xs:simpleType name="restrictedString">
        <!-- Make a new type to be a "descendant" of string-->
        <xs:restriction base="xs:string">
            <xs:maxLength value="36"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:element name="CustomFields">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="CustomField" minOccurs="0" maxOccurs="20">
                    <xs:complexType>
                        <xs:simpleContent>
                            <!-- reference new type you declared above -->
                            <xs:extension base="restrictedString">
                                <xs:attribute name="id" type="xs:integer" use="required"/>
                            </xs:extension>
                        </xs:simpleContent>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
        <!-- Restrict @id to be unique-->
        <xs:unique name="id_uq">
            <xs:selector xpath="CustomField"/>
            <xs:field xpath="@id"/>
        </xs:unique>
    </xs:element>
</xs:schema>

Upvotes: 2

Related Questions