25

I think I have searched a lot about this but still no go.

Will appreciate any help.

I am trying to restrict an attribute for an element with empty content. color should have a restriction to only hold 3 digit or minLength=3 and maxLength=3. It should not have any content.

Input file: items.xml

<?xml version="1.0" encoding="utf-8"?>
<items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="">
    <product id="" name="">
        <article id="1001">
            <umbrella color="100"/>
            <umbrella color="101"/>
        </article>
        <article id="1002">
            <umbrella color="110"/>
        </article>
    </product>
</items>

EDIT: I know how to do a XSD Restriction on a simpleType. But I don't how to combine it to one entity with a ComplexType.

If you could provide a more detailed (or full) solution I would be happy.

Btw, color is not limited to xs:integer. It is actually an xs:string.

StackzOfZtuff
  • 2,534
  • 1
  • 28
  • 25
ZiggyStardust
  • 415
  • 1
  • 7
  • 18

2 Answers2

34

You can define your attribute similar to the following. This example uses a pattern to restrict the value, but you could also use min and max if that's more appropriate.

<xs:attribute name="color">
    <xs:simpleType>
        <xs:restriction base="xs:integer">
            <xs:pattern value="[0-9][0-9][0-9]"/>
        </xs:restriction>
    </xs:simpleType>
</xs:attribute>

Then in your element definition, you just use a ref to reference the defined attribute:

<xs:attribute ref="color"/>

UPDATE (in response to comment from OP):

Here's what the entire schema might look like:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    
    <xs:attribute name="color">
        <xs:simpleType>
            <xs:restriction base="xs:integer">
                <xs:pattern value="[0-9][0-9][0-9]"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:attribute>
    
    <xs:attribute name="id">
        <xs:simpleType>
            <xs:restriction base="xs:integer">
                <xs:pattern value="[0-9][0-9][0-9][0-9]"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:attribute>
    
    <xs:attribute name="name" type="xs:string"/>

    <xs:complexType name="article_type">
        <xs:attribute ref="color" use="required"/>
    </xs:complexType>
    
    <xs:element name="article">
        <xs:complexType>
            <xs:choice maxOccurs="unbounded" minOccurs="0">
                <xs:element name="umbrella" type="article_type"/>
            </xs:choice>
            <xs:attribute ref="id" use="required"/>
        </xs:complexType>
    </xs:element>
    
    <xs:element name="product">
        <xs:complexType>
            <xs:choice maxOccurs="unbounded" minOccurs="0">
                <xs:element ref="article"/>
            </xs:choice>
            <xs:attribute ref="id" use="required"/>
            <xs:attribute ref="name"/>
        </xs:complexType>
    </xs:element>
    
    <xs:element name="items">
        <xs:complexType>
            <xs:choice maxOccurs="unbounded" minOccurs="0">
                <xs:element ref="product"/>
            </xs:choice>
        </xs:complexType>
    </xs:element>
    
</xs:schema>
StackzOfZtuff
  • 2,534
  • 1
  • 28
  • 25
David
  • 6,462
  • 2
  • 25
  • 22
  • 3
    tiny bit shorter: `[0-9]{3}` – 13ren Jan 11 '13 at 00:56
  • Thank you for your input. Se my edit above. I know how to do a xs:restriction but I don't know how to combine it all into one piece. Please provide more or full for my example. ComplexType with SimpleType Attributes with Restrictions if I understand correctly. – ZiggyStardust Jan 11 '13 at 07:15
  • Seem to do the trick. Thank you for your assistence. I hope I will be able to put together my own from your input. Cheers. – ZiggyStardust Jan 13 '13 at 11:29
2

The following should work:

<element name="umbrella" nillable="true" type="umbrellaType"></element>

<complexType name="umbrellaType">
    <attribute name="color">
        <simpleType>
            <restriction base="int">
                <minExclusive value="99"/>
                <maxInclusive value="999"/>
            </restriction>
        </simpleType>
    </attribute>
</complexType>
StackzOfZtuff
  • 2,534
  • 1
  • 28
  • 25
Baski
  • 829
  • 8
  • 14
  • Thank you for your input. Se my edit above. I know how to do a xs:restriction but I don't know how to combine it all into one piece. Please provide more or full for my example. ComplexType with SimpleType Attributes with Restrictions if I understand correctly. – ZiggyStardust Jan 11 '13 at 07:16