2013-01-11 18 views
15

Myślę, że dużo o tym przeszukałem, ale wciąż nie mogę odejść.Ograniczenia XSD dotyczące atrybutu

Doceniamy każdą pomoc.

Próbuję ograniczyć atrybut dla elementu z pustą zawartością. "kolor" powinien mieć ograniczenie tylko do trzymania 3 cyfr lub minimalnej długości = 3 i maksymalnej długości = 3. Nie powinien zawierać żadnych treści.

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

EDYCJA: Wiem, jak zrobić ograniczenie XSD na simpleType. Ale nie wiem, jak połączyć go z jednym obiektem z ComplexType.

Jeśli mógłbyś podać bardziej szczegółowe (lub pełne) rozwiązanie, byłbym szczęśliwy.

Btw, "kolor" nie jest ograniczony do xs: integer. W rzeczywistości jest to ciąg xs: string.

Odpowiedz

23

Możesz zdefiniować swój atrybut podobny do poniższego. W tym przykładzie zastosowano wzór do ograniczenia wartości, ale można również użyć wartości minimalnej i maksymalnej, jeśli jest to bardziej odpowiednie.

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

Następnie w definicji elementu, wystarczy użyć ref odniesienia zdefiniowanego atrybutu:

<xs:attribute ref="color"/> 

UPDATE (w odpowiedzi na komentarz z PO):

Oto co cały schemat może wyglądać następująco:

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

odrobinę krótsza: '[0-9] {3}' – 13ren

+0

Dziękuję za wejście. Se moja edycja powyżej. Wiem, jak zrobić xs: ograniczenie, ale nie wiem jak połączyć to wszystko w jedną całość. Proszę podać więcej lub więcej dla mojego przykładu. ComplexType z atrybutami SimpleType z ograniczeniami, jeśli rozumiem poprawnie. – ZiggyStardust

+0

@ZiggyStardust Zobacz aktualizację. – David

1

Następujące powinny działać

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

Dziękuję za twój wkład. Se moja edycja powyżej. Wiem, jak zrobić xs: ograniczenie, ale nie wiem jak połączyć to wszystko w jedną całość. Proszę podać więcej lub więcej dla mojego przykładu. ComplexType z atrybutami SimpleType z ograniczeniami, jeśli rozumiem poprawnie. – ZiggyStardust

Powiązane problemy