Aggiungi attributi a un tipo semplice o restrizione a un tipo complesso nello schema Xml

StackOverflow https://stackoverflow.com/questions/626319

  •  06-07-2019
  •  | 
  •  

Domanda

Il problema è il seguente:

Ho il seguente frammento XML:

<time format="minutes">11:60</time>

Il problema è che non riesco ad aggiungere contemporaneamente l'attributo e la restrizione. Il formato dell'attributo può avere solo i valori minuti, ore e secondi. Il tempo ha il restrictionpattern \d{2}:\d{2}

<xs:element name="time" type="timeType"/>
...
<xs:simpleType name="formatType">
<xs:restriction base="xs:string">
    <xs:enumeration value="minutes"/>
    <xs:enumeration value="hours"/>
    <xs:enumeration value="seconds"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="timeType">
    <xs:attribute name="format">
        <xs:simpleType>
            <xs:restriction base="formatType"/>
        </xs:simpleType>
    </xs:attribute>
</xs:complexType>

Se creo un tipo complesso di timeType, posso aggiungere un attributo, ma non la restrizione, e se creo un tipo semplice, posso aggiungere la restrizione ma non l'attributo. C'è un modo per aggirare questo problema. Questa non è una restrizione molto strana, o è?

È stato utile?

Soluzione

Per aggiungere attributi devi derivare per estensione, per aggiungere aspetti devi derivare per restrizione. Pertanto, ciò deve essere fatto in due passaggi per il contenuto figlio dell'elemento. L'attributo può essere definito in linea:

<xsd:simpleType name="timeValueType">
  <xsd:restriction base="xsd:token">
    <xsd:pattern value="\d{2}:\d{2}"/>
  </xsd:restriction>
</xsd:simpleType>

<xsd:complexType name="timeType">
  <xsd:simpleContent>
    <xsd:extension base="timeValueType">
      <xsd:attribute name="format">
        <xsd:simpleType>
          <xsd:restriction base="xsd:token">
            <xsd:enumeration value="seconds"/>
            <xsd:enumeration value="minutes"/>
            <xsd:enumeration value="hours"/>
          </xsd:restriction>
        </xsd:simpleType>
      </xsd:attribute>
    </xsd:extension>
  </xsd:simpleContent>
</xsd:complexType>

Altri suggerimenti

Vorrei proporre il mio esempio con una spiegazione più dettagliata di ciò che effettivamente richiede per mescolare il tipo ereditato con le restrizioni durante l'aggiunta dell'attributo.

questo è il posto in cui definisci il tuo tipo ereditato (nel mio caso è xs: uno basato su stringa con una limitazione di lunghezza del campo 1024 applicata). non puoi avere questo come tipo standard per il tuo campo poiché stai per aggiungere un attributo " " anche nel tuo campo.

  <xs:simpleType name="string1024Type">
    <xs:restriction base="xs:string">
      <xs:maxLength value="1024"/>
    </xs:restriction>
  </xs:simpleType>

questo è il luogo in cui il tuo elemento è definito in base al tuo tipo privato (nel mio caso è " string1024Type ") e attributo necessario aggiunto:

<xs:element maxOccurs="unbounded" name="event">
  <xs:complexType>
    <xs:simpleContent>
      <xs:extension base="string1024Type">
        <xs:attribute default="list" name="node" type="xs:string"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:element>

entrambi i blocchi potrebbero trovarsi in posizioni completamente separate del tuo schema. il punto importante sta solo seguendo ancora una volta: non puoi mescolare l'ereditarietà e attribuire nella stessa definizione di elemento.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top