开发者

Why won't this Schema validate this XML file?

开发者 https://www.devze.com 2022-12-26 18:47 出处:网络
The XML file: <Lista count=\"3\"> <Pelicula nombre=\"Jurasic Park 3\"> <Genero>Drama</Genero>

The XML file:

<Lista count="3">
  <Pelicula nombre="Jurasic Park 3">
    <Genero>Drama</Genero>
    <Director sexo="M">Esteven Spielberg</Director>
    <Temporada>
       <Anho>2002</Anho>
       <Semestre>Verano</Semestre>
    </Temporada>
  </Pelicula>
  <Pelicula nombre="Maldiciones">
    <Genero>Ficcion</Genero>
    <Director sexo="M">Pedro Almodovar</Director>
    <Temporada>
       <Anho>2002</Anho>
       <Semestre>Verano</Semestre>
    </Temporada>
  </Pelicula>
  <Pelicula nombre="Amor en New York">
    <Genero>Romance</Genero>
    <Director sexo="F">Katia Hertz</Director>
    <Temporada>
       <Anho>2002</Anho>
       <Semestre>Verano</Semestre>
    </Temporada>
  </Pelicula>
</Lista>

And here's the XML Schema file I made, it's not working. :\

<xsd:complexType name="Lista">
  <xsd:attribute name="count" type="xsd:integer" />
  <xsd:complexContent>
    <xsd:element name="Pelicula" type="xsd:string">
      <xsd:attribute name="nombre" type="xsd:string" />
      <xsd:complexType>
        <xsd:sequence>
          <xsd:element name="Genero" type="generoType"/>
          <xsd:element name="Director" type="directorType">
            <xsd:attribute name="sexo" type="sexoType"/>
          </xsd:element>
          </xsd:element name="Temp开发者_Python百科orada">
           <xsd:complexType>
             <xsd:sequence>
               <xsd:element name="Anho" type="anhoType" />
               <xsd:element name="Semestre" type="semestreType" />
             </xsd:sequence>
           </xsd:complexType>
          <xsd:element></xsd:element>
        </xsd:sequence>
      </xsd:complexType>
    </xsd:element>
  </xsd:complexContent>
</xsd:complexType>

<xsd:simpleType name="sexoType">
  <xsd:restriction base="xsd:string">
    <xsd:enumeration value="F"/>
    <xsd:enumeration value="M"/>
  </xsd:restriction>
</xsd:simpleType>

<xsd:simpleType name="directorType">
  <xsd:restriction base="xsd:string" />
</xsd:simpleType>

<xsd:simpleType name="generoType">
  <xsd:restriction base="xsd:string">
    <xsd:enumeration value="Drama"/>
    <xsd:enumeration value="Accion"/>
    <xsd:enumeration value="Romance"/>
    <xsd:enumeration value="Ficcion"/>
  </xsd:restriction>
</xsd:simpleType>

<xsd:simpleType name="semestreType">
  <xsd:restriction base="xsd:string">
    <xsd:enumeration value="Verano"/>
    <xsd:enumeration value="Invierno"/>
  </xsd:restriction>
</xsd:simpleType>

<xsd:simpleType name="anhoType">
  <xsd:restriction base="xsd:integer">
    <xsd:minInclusive value="1970"/>
    <xsd:maxInclusive value="2020"/>
  </xsd:restriction>
</xsd:simpleType>


Try declaring and using your types separately. This makes the XSD a bit longer, but less nested and more readable (and more reusable, too):

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <!-- document element -->
  <xs:element name="Lista" type="listaType" />

  <!-- type definitions -->
  <xs:complexType name="listaType">
    <xs:sequence>
      <xs:element name="Pelicula" type="peliculaType" minOccurs="0" maxOccurs="unbounded" />
    </xs:sequence>
    <xs:attribute name="count" type="countType" />
  </xs:complexType>

  <xs:complexType name="peliculaType">
    <xs:all>
      <xs:element name="Genero" type="generoType" />
      <xs:element name="Director" type="directorType" />
      <xs:element name="Temporada" type="temporadaType" />
    </xs:all>
    <xs:attribute name="nombre" type="xs:string" />
  </xs:complexType>

  <xs:complexType name="directorType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="sexo" type="sexoType" />
      </xs:extension>
    </xs:simpleContent> 
  </xs:complexType>

  <xs:complexType name="temporadaType">
    <xs:all>
      <xs:element name="Anho" type="anhoType" />
      <xs:element name="Semestre" type="semestreType" />
    </xs:all>
  </xs:complexType>

  <xs:simpleType name="sexoType">
    <xs:restriction base="xs:string"> 
      <xs:enumeration value="F" /> 
      <xs:enumeration value="M" /> 
    </xs:restriction> 
  </xs:simpleType>

  <xs:simpleType name="generoType"> 
    <xs:restriction base="xs:string"> 
      <xs:enumeration value="Drama" /> 
      <xs:enumeration value="Accion" /> 
      <xs:enumeration value="Romance" /> 
      <xs:enumeration value="Ficcion" /> 
    </xs:restriction> 
  </xs:simpleType>

  <xs:simpleType name="semestreType"> 
    <xs:restriction base="xs:string"> 
      <xs:enumeration value="Verano" /> 
      <xs:enumeration value="Invierno" /> 
    </xs:restriction> 
  </xs:simpleType>

  <xs:simpleType name="anhoType">
    <xs:restriction base="xs:integer"> 
      <xs:minInclusive value="1970" /> 
      <xs:maxInclusive value="2020" /> 
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="countType">
    <xs:restriction base="xs:integer">
      <xs:minInclusive value="0" />
    </xs:restriction>
  </xs:simpleType>

</xs:schema>

The above validates your input XML.

Note that:

  • The convention for the XML Schema namespace prefix is xs (AFAIK - you can still go on and use xsd if you prefer).
  • The count attribute is really unnecessary, since the count can a) easily be calculated and b) there is a risk of something going wrong when the count attribute value and the actual count differ for some reason. Things that are derivable from the data should never be part of the data.

Oh, and to answer your initial question (why does mine not work):

  • You never declare an actual document element ("Lista"), you just declare its type. Compare with my solution.
  • In the complexType name="Lista":
    • attribute cannot be the first child of a complex type. Attributes must be declared after everything else.
    • complexContent cannot contain element.
    • in fact, you don't need a complexContent at all - just use a sequence instead.
  • In element name="Pelicula":
    • The type attribute is illegal when you declare a complexType within.
  • In the complexType for "Pelicula":
    • Again, attributes last.
    • Don't use a sequence unless you want to make any other order of children illegal. In this type of document I would guess child order is irrelevant.
  • In element name="Director":
    • You can't declare any attributes since when you already declared a type. Include the sexo attribute in the directorType
  • In simpleType name="directorType":
    • This should in reality be a complexType containing simpleContent with an extension. This way you can include the sexo attribute
  • Your XSD isn't even well formed XML.
  • Your XML iswas not well-formed, either. I have fixed it to be able to test in the first place.

P.S.: There is enough XSD documentation freely available to fix many of the basic problems you've had. There are XSD validators on the Net that help you by telling you what constructs are illegal. Knowing everything is absolutely not necessary, but a little reading + trial and error would have helped. ;-)


For a start:

         <Semestre>Verano<Semestre>

...doesn't look well-formed.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号