XSD Restriction on Attribute

ZiggyStardust picture ZiggyStardust · Jan 11, 2013 · Viewed 40.6k times · Source

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.

<?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 a xs:string.

Answer

David picture David · Jan 11, 2013

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>