简体   繁体   中英

Union element validation XML Schema

While validating my files using this online validator I get following error messages:

cvc-datatype-valid.1.2.3: 'Gomorra 20140506' is not a valid value of union type '#AnonType_alternatywny_tekstzdjecie'.

cvc-type.3.1.3: The value 'Gomorra 20140506' of element 'alternatywny_tekst' is not valid.

I formated my code like in w3schools examples. Could anyone tell me what's wrong with my code?

<xs:element name="zdjecie">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="wysokosc" type="xs:decimal"/>
      <xs:element name="szerokosc" type="xs:decimal"/>
      <xs:element ref="zrodlo"/>
      <xs:element name="alternatywny_tekst">
        <xs:simpleType>
          <xs:union memberTypes="tekst_1 tekst_2"/>
        </xs:simpleType>
      </xs:element>
    </xs:sequence>
    <xs:attribute ref="kod"/>
  </xs:complexType>
</xs:element>

<xs:simpleType name="tekst_1">
  <xs:restriction base="xs:string">
    <xs:enumeration value="Gomorra"/>
    <xs:enumeration value="Grand Budapest Hotel"/>
    <xs:enumeration value="Fargo"/>
    <xs:enumeration value="Wściekłe psy"/>
  </xs:restriction>
</xs:simpleType>

<xs:simpleType name="tekst_2">
  <xs:restriction base="xs:integer">
    <xs:maxExclusive value="20161130"/>
  </xs:restriction>
</xs:simpleType>

XML

<zdjecie kod="GO.2014.001">
  <wysokosc>735</wysokosc>
  <szerokosc>500</szerokosc>
  <zrodlo>Obrazki/gomorra.jpg</zrodlo>
  <alternatywny_tekst>Gomorra 20140506</alternatywny_tekst>
</zdjecie>

The value space of an xs:union is the union of its member types, but alone it does not allow multiple members to be present. From your XML, it appears that you would like to allow multiple members. You can achieve this via xs:list :

    <xs:element name="alternatywny_tekst">
      <xs:simpleType>
        <xs:list>
          <xs:simpleType>
            <xs:union memberTypes="tekst_1 tekst_2"/>
          </xs:simpleType>
        </xs:list>
      </xs:simpleType>
    </xs:element>

Note that this allows combinations from the union of the values allowed by tekst_1 and tekst_2 -- not necessarily a tekst_1 followed by a tekst_2 .

Here is the above declaration in the context of a complete XSD that'll successfully validate your XML:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="zdjecie">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="wysokosc" type="xs:decimal"/>
        <xs:element name="szerokosc" type="xs:decimal"/>
        <xs:element name="zrodlo" type="xs:string"/>
        <xs:element name="alternatywny_tekst">
          <xs:simpleType>
            <xs:list>
              <xs:simpleType>
                <xs:union memberTypes="tekst_1 tekst_2"/>
              </xs:simpleType>
            </xs:list>
          </xs:simpleType>
        </xs:element>
      </xs:sequence>
      <xs:attribute name="kod" type="xs:string"/>
    </xs:complexType>
  </xs:element>
  <xs:simpleType name="tekst_1">
    <xs:restriction base="xs:string">
      <xs:enumeration value="Gomorra"/>
      <xs:enumeration value="Grand Budapest Hotel"/>
      <xs:enumeration value="Fargo"/>
      <xs:enumeration value="Wściekłe psy"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="tekst_2">
    <xs:restriction base="xs:integer">
      <xs:maxExclusive value="20161130"/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM