Ограничение xsd на атрибут

Я думаю, что я искал много об этом, но все равно не пойдет.

будет признателен за любую помощь.

Я пытаюсь ограничить атрибут для элемента с пустым содержимым. "цвет" должен иметь ограничение только на 3 цифры или minLength=3 и maxLength=3. Она не должна иметь никакого содержания.

<?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: я знаю, как сделать ограничение XSD на simpleType. Но я не знаю, как объединить его в одну сущность с ComplexType.

Если вы мог бы предоставить более подробное (или полное) решение, я был бы рад.

кстати, "цвет" не ограничивается xs:integer. На самом деле это xs: string.

2 ответов


вы можете определить свой атрибут, подобный следующему. В этом примере используется шаблон для ограничения значения, но вы также можете использовать min и max, если это более уместно.

<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>

тогда в определении элемента вы просто используете ref для ссылки на определенный атрибут:

<xs:attribute ref="color"/>

UPDATE (в ответ на комментарий от OP):

вот как может выглядеть вся схема:

<?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>

должно работать следующее

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

<complexType name="umbrellaType">
   <attribute name="color">
     <simpleType>
       <restriction base="int">
        <minExclusive value="99"></minExclusive>
        <maxInclusive value="999"></maxInclusive>
       </restriction>
     </simpleType>
   </attribute>
</complexType>