Ограничение XSD по атрибуту - программирование
Подтвердить что ты не робот

Ограничение XSD по атрибуту

Думаю, я много искал об этом, но все равно не хожу.

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

Я пытаюсь ограничить атрибут для элемента с пустым содержимым. "color" должен иметь ограничение только на 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.

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

Btw, "цвет" не ограничен xs: integer. Это фактически строка xs:.

4b9b3361

Ответ 1

Вы можете определить свой атрибут, похожий на следующий. В этом примере используется шаблон для ограничения значения, но вы также можете использовать 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>

Ответ 2

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

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