Problems to validate an XML with an XSD

2

I have problems validating an XML document with a Schema XSD.

The error you give me is the following. I have another document to validate in which I get the same error. I do not understand what I'm missing.

  

Error at line 19, column 25: content of element declaration must match   (annotation ?, (simpleType | complexType) ?, (unique | key | keyref) *)

XML

<?xml version="1.0" encoding="UTF-8"?>

<Videoteca data_creación="24/02/2009" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="peliculas.xsd">>
    <Película id="1">
            <Título>El santo</Título>
            <TítuloOrixinal>The Saint</TítuloOrixinal>
            <Ano>1997</Ano>
            <Director>Phillip Noyce</Director>
            <Xénero>Acción</Xénero>
            <Duración>111</Duración>
            <Actor principal="SI" sexo="home">
                    <Nome>Val Kilmer</Nome>
                    <DataNacemento>31/12/1959</DataNacemento>
            </Actor>
            <!-- Elisabeth Shue -->
            <Actor principal="NO" id="51"/>
    </Película>
    <Película id="2">
            <Título>¿Teléfono rojo? Volamos hacia Moscú</Título>
            <TítuloOrixinal>Dr. Strangelove, or How I Learned to Stop Worrying and Love the Bomb</TítuloOrixinal>
            <Ano>1963</Ano>
            <Director>Stanley Kubrick</Director>
            <Duración>90</Duración>
            <Actor principal="SI" sexo="home">
                    <Nome>Peter Sellers</Nome>
            </Actor>
    </Película>
    <Película id="3">
            <Título>Leaving Las Vegas</Título>
            <TítuloOrixinal>Leaving Las Vegas</TítuloOrixinal>
            <Ano>1995</Ano>
            <Director>Mike Figgis</Director>
            <Xénero>Drama</Xénero>
            <Duración>107</Duración>
            <Actor principal="SI" sexo="home">
                    <Nome>Nicolas Cage</Nome>
                    <DataNacemento>7/01/1964</DataNacemento>
            </Actor>
            <!-- Elisabeth Shue -->
            <Actor id="51" principal="SI"/>
            <Actor sexo="home">
                    <Nome>Julian Sands</Nome>
            </Actor>
    </Película>
    <Película id="4">
            <Título>¿A quién ama Gilbert Grape?</Título>
            <TítuloOrixinal>What's Eating Gilbert Grape?</TítuloOrixinal>
            <Ano>1993</Ano>
            <Director>Lasse Hallström</Director>
            <Xénero>Drama</Xénero>
            <Duración>118</Duración>
            <!-- Johnny Depp -->
            <Actor id="139" principal="SI"/>
            <Actor sexo="muller">
                    <Nome>Juliette Lewis</Nome>
            </Actor>
    </Película>
</Videoteca>

XSD

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="Videoteca">
<xs:complexType>
  <xs:sequence>
    <xs:element name="Pelicula" minOccurs="1" maxOccurs="unbounded">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Tïtulo" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>
        <xs:element name="TïtuloOrixinal" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>
        <xs:element name="Ano" type="xs:NMTOKEN" minOccurs="1" maxOccurs="unbounded"/>
        <xs:element name="Director" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>
        <xs:element name="Xénero" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>
        <xs:element name="Duración" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>
      </xs:sequence>
          <xs:attribute name="data_creación" type="xs:NMTOKEN" use="required"/>
          <xs:attribute name="id" type="xs:string" use="required"/>
    </xs:complexType>
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Actor" type="xs:string" minOccurs="1" maxOccurs="unbounded">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="Nome" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>
            <xs:element name="DataNacemento" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
          </xs:sequence>
          <xs:attribute name="principal" type="xs:string" use="required">
              <xs:simpleType>
                  <xs:restriction base="xs:string">
                      <xs:enumeration value="SI"/>
                      <xs:enumeration value="NO"/>
                  </xs:restriction>
              </xs:simpleType>
          </xs:attribute>
          <xs:attribute name="id" type="xs:string" use="required"/>
          <xs:attribute name="sexo" type="xs:string" use="required">
              <xs:simpleType>
                  <xs:restriction base="xs:string">
                      <xs:enumeration value="home"/>
                      <xs:enumeration value="muller"/>
                  </xs:restriction>
              </xs:simpleType>
          </xs:attribute>
    </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
    </xs:element>
  </xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
    
asked by djrubens 22.04.2017 в 15:54
source

2 answers

2

It is a problem of formation of the elements, which must correspond in the XML and in XSD both in the names and in the structure of the data.

There are mismatch errors, for example Título is not the same as Tïtulo :

XML

<Título>El santo</Título>

XSD

<xs:element name="Tïtulo" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>

Then, the different Movie elements of the XML do not seem to be structured in the same way. A movie should be like an entity and all movies should have the same data structure : Author, Date, Title, etc. Neither should there be repeated ids, which was one of the mistakes I made when trying to validate the document.

Also I would avoid the use of accents or accents in the names of the tags and the elements. .

This would be a simple example.

Look like in the XML each element book , that is, bk001 and bk002 have the same structure of data :

 id,author,title,genre,price,review

Then, notice that in the XSD, in complexType name="BookForm we have this same structure, that is: id,author,title,genre,price,review written in the same way:

<xsd:complexType name="BookForm">
    <xsd:sequence>
      <xsd:element name="author"   type="xsd:string"/>
      <xsd:element name="title"    type="xsd:string"/>
      <xsd:element name="genre"    type="xsd:string"/>
      <xsd:element name="price"    type="xsd:float" />
      <xsd:element name="review"   type="xsd:string"/>
    </xsd:sequence>
    <xsd:attribute name="id"   type="xsd:string"/>
</xsd:complexType>

Two complete sample files XML
<?xml version="1.0"?>
<x:books xmlns:x="urn:books">
   <book id="bk001">
      <author>Writer</author>
      <title>The First Book</title>
      <genre>Fiction</genre>
      <price>44.95</price>
      <review>An amazing story of nothing.</review>
   </book>

   <book id="bk002">
      <author>Poet</author>
      <title>The Poet's First Poem</title>
      <genre>Poem</genre>
      <price>24.95</price>
      <review>Least poetic poems.</review>
   </book>
</x:books>

XSD

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="urn:books"
            xmlns:bks="urn:books">

  <xsd:element name="books" type="bks:BooksForm"/>

  <xsd:complexType name="BooksForm">
    <xsd:sequence>
      <xsd:element name="book" 
                  type="bks:BookForm" 
                  minOccurs="0" 
                  maxOccurs="unbounded"/>
      </xsd:sequence>
  </xsd:complexType>

  <xsd:complexType name="BookForm">
    <xsd:sequence>
      <xsd:element name="author"   type="xsd:string"/>
      <xsd:element name="title"    type="xsd:string"/>
      <xsd:element name="genre"    type="xsd:string"/>
      <xsd:element name="price"    type="xsd:float" />
      <xsd:element name="review"   type="xsd:string"/>
    </xsd:sequence>
    <xsd:attribute name="id"   type="xsd:string"/>
  </xsd:complexType>
</xsd:schema>
    
answered by 22.04.2017 в 18:59
0

I already came up with the problem, the Actor had to go in the complextype of Film and not in a separate complextype as I had it. Now if well formed and validated. Thank you very much for your help

    
answered by 22.04.2017 в 23:25