0

I want to write an XSD file to validate the following XML

The xml file:

<?xml version="1.0" encoding="UTF-8"?>
<eventos xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="eventos31.xsd">
<evento tipo="musical">
    <compania>
        <nombre_compania>La Joven Compañia</nombre_compania>
        <interpretes>
            <interprete codint="I1"> Javier   Ariano</interprete>
            <interprete codint="I2"> Alejandro Chaparro</interprete>
            <interprete codint="I3"> Jesús Lavi</interprete>
            <interprete codint="I4"> Rosa Martí</interprete>
            <interprete codint="I5"> Laura Montesinos</interprete>
            <interprete codint="I6"> Maria Romero</interprete>
            <interprete codint="I7"> Ález Villazán</interprete>
            <interprete codint="I8"> Jorge Yumar</interprete>
        </interpretes>
        <director-a> José Luis Arellano García</director-a>
        <web_compania dirweb= "www.lajovencompania.com"/>
    </compania> 
    <nombre_espectaculo>Priscila, reina del desierto</nombre_espectaculo>
    <cuando>
        <fecha dia="22" mes="febrero" anio="2018"/>
        <hora inicio="20:30" fin="23:00"/>
    </cuando>
    <descripcion>Narra las aventuras de tres artistas drag</descripcion>
    <precio>55€</precio>
    <duracion>2h 30min</duracion>
</evento>
<evento tipo="magia">
    <mago>
        <nombre_mago> Jorge Blass</nombre_mago>
        <mail dirmail= "naka@7rojo.com"/>
        <shows>
            <show codshow="C1"> Palabra de Mago</show>
            <show codshow="C2"> Arte de la Magia</show>
            <show codshow="C3"> Birlibirloque</show>
        </shows>
    </mago> 
    <nombre_espectaculo>Palabra de mago</nombre_espectaculo>
    <cuando>
        <fecha dia="02" mes="Junio" anio="2018"/>
        <hora inicio="19:00" fin="20:30"/>
    </cuando>
    <descripcion>Trucos de magia de Jorge Blas</descripcion>
    <precio>18€</precio>
    <duracion>1h 30min</duracion>
</evento>
<evento tipo="humor">
    <comico>
        <nombre_comico> Dani Mateo</nombre_comico>
        <ciudad> Granollers</ciudad>
        <edad> 38</edad>
        <redes_sociales>
            <blog> danimateo.blogspot.com</blog>
            <twitter> @DaniMateoAgain</twitter>
            <facebook> @dani.mateo</facebook>
        </redes_sociales>
    </comico>   
    <nombre_espectaculo>Dani Mateo,10 aniversario</nombre_espectaculo>
    <cuando>
        <fecha dia="09" mes="Junio" anio="2018"/>
        <hora inicio="20:30" fin="22:00"/>
    </cuando>
    <precio>21€</precio>
    <duracion>1h 30min</duracion>
</evento>

This is, what I have tried so far

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="eventos">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="evento" maxOccurs="unbounded">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="compania">
                <xs:complexType>
                    <xs:sequence>
                    <xs:element name="nombre_compania" type="xs:string" />
                    <xs:element name="interpretes">
                        <xs:complexType>
                        <xs:sequence>
                            <xs:element name="interprete" maxOccurs="unbounded"/>   
                        </xs:sequence>
                            <xs:attribute name="codint" type="xs:string" use="required"/>
                        </xs:complexType>
                    </xs:element>   
                    <xs:element name="director-a" type="xs:string" />
                    <xs:element name="web_compania"/>   
                    </xs:sequence>
                    <xs:attribute name="dirweb" type="xs:string"/>
                </xs:complexType>
            </xs:element>   
            <xs:element name="nombre_espectaculo" type="xs:string" maxOccurs="unbounded"/>  
            <xs:element name="cuando">
                        <xs:complexType>
                            <xs:sequence>
                            <xs:element name="fecha"/>
                            <xs:element name="hora"/>
                            </xs:sequence>
                            <xs:attribute name="dia" type="xs:integer"/>
                            <xs:attribute name="mes" type="xs:string"/>
                            <xs:attribute name="anio" type="xs:integer"/>
                            <xs:attribute name="inicio" type="xs:string"/>
                            <xs:attribute name="fin" type="xs:string"/>
                        </xs:complexType>
            </xs:element>   
            <xs:element name="descripcion" type="xs:string"/>
            <xs:element name="precio" type="xs:string"/>
            <xs:element name="duracion" type="xs:string"/>
            <xs:element name="mago" minOccurs="0">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="nombre_mago" type="xs:string"/>
                        <xs:element name="mail"/>   
                        <xs:element name="shows">
                            <xs:complexType>
                            <xs:sequence>
                            <xs:element name="show" type="xs:string"/>  
                            </xs:sequence>
                            <xs:attribute name="codshow" type="xs:string" use="required"/>
                            </xs:complexType>
                        </xs:element>   
                    </xs:sequence>
                    <xs:attribute name="dirmail" type="xs:string"/>
                </xs:complexType>
            </xs:element>
            <xs:element name="comico" minOccurs="0">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="nombre_comico" type="xs:string"/>
                        <xs:element name="ciudad" type="xs:string"/>
                        <xs:element name="edad" type="xs:integer"/>
                        <xs:element name="redes_sociales">
                            <xs:complexType>
                                <xs:sequence>
                                    <xs:element name="blog" type="xs:string"/>
                                    <xs:element name="twitter" type="xs:string"/>
                                    <xs:element name="facebook" type="xs:string"/>
                                </xs:sequence>
                            </xs:complexType>
                        </xs:element>   
                    </xs:sequence>
                </xs:complexType>
            </xs:element>   
        </xs:sequence>
        <xs:attribute name="tipo" type="xs:string"/>
    </xs:complexType>   
    </xs:element>
    </xs:sequence>
    </xs:complexType>   
</xs:element>   

Using xmllint xmllint -schema eventos31.xsd 48985313.xml

I get

48985313.xml:6: element interpretes: Schemas validity error : Element 'interpretes': The attribute 'codint' is required but missing.
48985313.xml:29: element mago: Schemas validity error : Element 'mago': This element is not expected. Expected is ( compania ).
48985313.xml:48: element comico: Schemas validity error : Element 'comico': This element is not expected. Expected is ( compania ).
48985313.xml fails to validate

What is the easiest way to get a XSD for an existing XML file?

jschnasse
  • 8,526
  • 6
  • 32
  • 72

2 Answers2

0

Loading your documents into Eclipse with installed "Eclipse Xml Editors and Tools" I can see that you have various errors in your xml document. You should go through the errors line by line, and ask yourself where to correct it - in the XSD or in the XML file.

  1. line 6: cvc-complex-type.4: Attribute 'codint' must appear on element 'interpretes'.

    This can be fixed by adding a codint attribute to the <interpretes> tag in the XML file. It could also be fixed my modifying the XSD definition accordingly.

  2. line 29: cvc-complex-type.2.4.a: Invalid content was found starting with element 'mago'. One of '{compania}' is expected.

    ... go on ... try to understand the error ... try to fix the XSD or the XML ... if nothing works,ask a question on stack overflow

  3. line 32 cvc-type.3.1.1: Element 'show' is a simple type, so it cannot have attributes, excepting those whose namespace name is identical to 'http://www.w3.org/2001/XMLSchema-instance' and whose [local name] is one of 'type', 'nil', 'schemaLocation' or 'noNamespaceSchemaLocation'. However, the attribute, 'codshow' was found.
  4. line 33 Element 'show' is a simple type, so it cannot have attributes ...
  5. line 34 Element 'show' is a simple type, so it cannot have attributes ...
  6. line 35 Element 'show' is a simple type, so it cannot have attributes ...
  7. line 48 Invalid content was found starting with element 'comico'. One of '{compania}' is expected.
jschnasse
  • 8,526
  • 6
  • 32
  • 72
  • Thank you jschanne!!! But the problem is that I don´t kown how to solve the problem and I going crazy for this. Can you solve it? – Manuel Ibáñez Feb 26 '18 at 10:34
  • Crazy, like in craaaaazzzzyyy? :-D Don't panic. Just go through the errors line by line. Try to understand the error. Then decide, is it an error in the XSD, or in the XML. Try to correct the error. Repeat, until the error disappears. Go to the next error. If you have specific questions, just ask. But it is difficult to answer, if the question is not on point. – jschnasse Feb 26 '18 at 10:54
  • I can´t solve the errors. I don´t want to modify the XML. Just modify the XSD. But I don´t know how. – Manuel Ibáñez Feb 26 '18 at 11:20
0

If you do not want to get basic knowledge of XML Schema and fix the XSD by yourself, use a XSD generator like trang to generate an XSD for you. Sometimes this procedure will provide some hints on how to fix the original XSD.

trang 48985313.xml eventos31.xsd

  <?xml version="1.0" encoding="UTF-8"?>
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <xs:import namespace="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="xsi.xsd"/>
    <xs:element name="eventos">
      <xs:complexType>
        <xs:sequence>
          <xs:element maxOccurs="unbounded" ref="evento"/>
        </xs:sequence>
        <xs:attribute ref="xsi:noNamespaceSchemaLocation" use="required"/>
      </xs:complexType>
    </xs:element>
    <xs:element name="evento">
      <xs:complexType>
        <xs:sequence>
          <xs:choice>
            <xs:element ref="comico"/>
            <xs:element ref="compania"/>
            <xs:element ref="mago"/>
          </xs:choice>
          <xs:element ref="nombre_espectaculo"/>
          <xs:element ref="cuando"/>
          <xs:element minOccurs="0" ref="descripcion"/>
          <xs:element ref="precio"/>
          <xs:element ref="duracion"/>
        </xs:sequence>
        <xs:attribute name="tipo" use="required" type="xs:NCName"/>
      </xs:complexType>
    </xs:element>
    <xs:element name="comico">
      <xs:complexType>
        <xs:sequence>
          <xs:element ref="nombre_comico"/>
          <xs:element ref="ciudad"/>
          <xs:element ref="edad"/>
          <xs:element ref="redes_sociales"/>
        </xs:sequence>
      </xs:complexType>
    </xs:element>
    <xs:element name="nombre_comico" type="xs:string"/>
    <xs:element name="ciudad" type="xs:NCName"/>
    <xs:element name="edad" type="xs:integer"/>
    <xs:element name="redes_sociales">
      <xs:complexType>
        <xs:sequence>
          <xs:element ref="blog"/>
          <xs:element ref="twitter"/>
          <xs:element ref="facebook"/>
        </xs:sequence>
      </xs:complexType>
    </xs:element>
    <xs:element name="blog" type="xs:NCName"/>
    <xs:element name="twitter" type="xs:string"/>
    <xs:element name="facebook" type="xs:string"/>
    <xs:element name="compania">
      <xs:complexType>
        <xs:sequence>
          <xs:element ref="nombre_compania"/>
          <xs:element ref="interpretes"/>
          <xs:element ref="director-a"/>
          <xs:element ref="web_compania"/>
        </xs:sequence>
      </xs:complexType>
    </xs:element>
    <xs:element name="nombre_compania" type="xs:string"/>
    <xs:element name="interpretes">
      <xs:complexType>
        <xs:sequence>
          <xs:element maxOccurs="unbounded" ref="interprete"/>
        </xs:sequence>
      </xs:complexType>
    </xs:element>
    <xs:element name="interprete">
      <xs:complexType mixed="true">
        <xs:attribute name="codint" use="required" type="xs:NCName"/>
      </xs:complexType>
    </xs:element>
    <xs:element name="director-a" type="xs:string"/>
    <xs:element name="web_compania">
      <xs:complexType>
        <xs:attribute name="dirweb" use="required" type="xs:NCName"/>
      </xs:complexType>
    </xs:element>
    <xs:element name="mago">
      <xs:complexType>
        <xs:sequence>
          <xs:element ref="nombre_mago"/>
          <xs:element ref="mail"/>
          <xs:element ref="shows"/>
        </xs:sequence>
      </xs:complexType>
    </xs:element>
    <xs:element name="nombre_mago" type="xs:string"/>
    <xs:element name="mail">
      <xs:complexType>
        <xs:attribute name="dirmail" use="required"/>
      </xs:complexType>
    </xs:element>
    <xs:element name="shows">
      <xs:complexType>
        <xs:sequence>
          <xs:element maxOccurs="unbounded" ref="show"/>
        </xs:sequence>
      </xs:complexType>
    </xs:element>
    <xs:element name="show">
      <xs:complexType mixed="true">
        <xs:attribute name="codshow" use="required" type="xs:NCName"/>
      </xs:complexType>
    </xs:element>
    <xs:element name="nombre_espectaculo" type="xs:string"/>
    <xs:element name="cuando">
      <xs:complexType>
        <xs:sequence>
          <xs:element ref="fecha"/>
          <xs:element ref="hora"/>
        </xs:sequence>
      </xs:complexType>
    </xs:element>
    <xs:element name="fecha">
      <xs:complexType>
        <xs:attribute name="anio" use="required" type="xs:integer"/>
        <xs:attribute name="dia" use="required" type="xs:integer"/>
        <xs:attribute name="mes" use="required" type="xs:NCName"/>
      </xs:complexType>
    </xs:element>
    <xs:element name="hora">
      <xs:complexType>
        <xs:attribute name="fin" use="required" type="xs:NMTOKEN"/>
        <xs:attribute name="inicio" use="required" type="xs:NMTOKEN"/>
      </xs:complexType>
    </xs:element>
    <xs:element name="descripcion" type="xs:string"/>
    <xs:element name="precio" type="xs:string"/>
    <xs:element name="duracion" type="xs:string"/>
  </xs:schema>
jschnasse
  • 8,526
  • 6
  • 32
  • 72