简体   繁体   中英

Why won't my XML validate against my XSD?

I try to validate an XML file with this XSD, but I get the following errors:

Cvc-elt.1: Cannot Find The Declaration Of Element 'Interprete'.. Line '1', Column '13'.

Element Type "A" Must Be Followed By Either Attribute Specifications, ">" Or "/>".. Line '10', Column '5'.

Element Type "A" Must Be Followed By Either Attribute Specifications, ">" Or "/>".

XML

<?xml version='1.0' encoding='ISO-8859-15' standalone='no'?>

<Interprete>
 <Nombre>
  <NombreC>Joaquín Sabina</NombreC>
  <Id>sdfryghuia</Id>
 </Nombre>
 <Nacionalidad>español</Nacionalidad>

 <Album tipo="nuevo">
  <NombreA>19 días y 500 noches</NombreA>
  <Año>2002</Año>
  <Cancion estilo="pop">
    <NombreT>19 días y 500 noches</NombreT>
    Tema principal del disco
    <Duracion>04:45</Duracion>
  </Cancion>
   <Cancion estilo="balada">
    <NombreT>Una Canción Para La Magdalena</NombreT>
    Sobre la Magdalena?
    <Duracion>04:15</Duracion>
    <Version>
       <Nombre>
         <NombreC>Bruce Springsteen</NombreC>
         <Id>sdfryghuie</Id>
       </Nombre>
      <IML>springsteen.xml</IML>
    </Version>
  </Cancion>
  <Cancion estilo="rock">
    Y alguno más
    <NombreT>A Mis Cuarenta y Diez</NombreT>
    <Duracion>07:11</Duracion>
  </Cancion>
   <Cancion estilo="rock">
    <NombreT>Cerrado Por Derribo</NombreT>
    <Duracion>04:37</Duracion>
    Para siempre
  </Cancion>
 </Album>

 <Album tipo="nuevo" temas="5">
  <NombreA>Física y Química</NombreA>
  <Año>2001</Año>
  <Cancion estilo="balada">
    <NombreT>Y nos dieron las diez</NombreT>
    Y las once
    <Duracion>05:02</Duracion>
  </Cancion>
   <Cancion estilo="rock">
    <NombreT>La del pirata cojo</NombreT>
    Con pata de palo
    <Duracion>04:37</Duracion>
  </Cancion>
  <Cancion estilo="balada">
    Esta es
    <NombreT>La canción de las noches perdidas</NombreT>
    <Duracion>03:56</Duracion>
  </Cancion>
   <Cancion estilo="balada">
    <NombreT>Peor para el sol</NombreT>
    <Duracion>04:57</Duracion>
    Que se mete en a cama a dormir
  </Cancion>
   <Cancion estilo="pop">
    <NombreT>Pastillas para no soñar</NombreT>
    <Duracion>03:55</Duracion>
    No hay
   </Cancion>
 </Album>

 <Album tipo="recopilatorio">
  <NombreA>Todos hablan de ti</NombreA>
  <Año>2004</Año>
  <Cancion estilo="balada">
    <NombreT>Pongamos que hablo de Madrid</NombreT>
    Al cielo
    <Duracion>04:07</Duracion>
  </Cancion>
   <Cancion estilo="rock">
    <NombreT>Eh, Sabina</NombreT>
    Tú sí que sabes
    <Duracion>03:40</Duracion>
  </Cancion>
  <Cancion estilo="balada">
    Quiero mudarme hace años
    <NombreT>Calle Melancolía</NombreT>
    <Duracion>04:27</Duracion>
  </Cancion>
   <Cancion estilo="pop">
    <NombreT>Juana la Loca</NombreT>
    <Duracion>05:35</Duracion>
    Y Felipe el Hermoso
  </Cancion>
 </Album>

 <Album tipo="mixto" temas="4">
  <NombreA>Diario de un peatón</NombreA>
  <Año>2003</Año>
  <Cancion estilo="balada">
    <NombreT>La canción más hermosa del mundo</NombreT>
    Con Pablo Milanés
    <Duracion>04:56</Duracion>
  </Cancion>
   <Cancion estilo="rock">
    <NombreT>Doble vida</NombreT>
    Y triple a veces
    <Duracion>03:51</Duracion>
  </Cancion>
  <Cancion estilo="pop">
    Nosotros también
    <NombreT>Semos diferentes</NombreT>
    <Duracion>04:30</Duracion>
  </Cancion>
   <Cancion estilo="rock">
    <NombreT>Cuando me hablan del destino</NombreT>
    <Duracion>03:44</Duracion>
    Me pongo furioso
  </Cancion>
 </Album>

 <Album tipo="nuevo">
  <NombreA>Nos sobran los motivos</NombreA>
  <Año>2000</Año>
  <Cancion estilo="balada">
    <NombreT>Que se llama Soledad</NombreT>
    Y seguirá llamándose
    <Duracion>06:20</Duracion>
    <Version>
      <Nombre>
         <NombreG>Siniestro Total</NombreG>
         <Id>sdfryghuii</Id>
      </Nombre>
      <IML>siniestro.xml</IML>
    </Version>
  </Cancion>
   <Cancion estilo="rock">
    <NombreT>Contigo</NombreT>
    Y sin ti
    <Duracion>05:05</Duracion>
  </Cancion>
  <Cancion estilo="balada">
    Así estoy yo sin ti
    <NombreT>¿Quién me ha robado el mes de abril?</NombreT>
    <Duracion>06:54</Duracion>
  </Cancion>
   <Cancion estilo="pop">
    <NombreT>Conductores suicidas</NombreT>
    <Duracion>05:04</Duracion>
    Todos somos un poco así
  </Cancion>
 </Album>

</Interprete>

XSD

<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.w3schools.com" targetNamespace="http://www.w3schools.com" elementFormDefault="qualified">
  <xs:element name="Interprete">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Nombre">
          <xs:complexType>
            <xs:sequence>
              <xs:choice>
                <xs:element name="NombreC" type="xs:string" />
                <xs:element name="NombreG" type="xs:string" />
              </xs:choice>
              <xs:element name="Id">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:length value="10" />
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="Nacionalidad">
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:enumeration value="español" />
              <xs:enumeration value="italiano" />
              <xs:enumeration value="americano" />
            </xs:restriction>
          </xs:simpleType>
        </xs:element>
        <xs:element name="Album" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="NombreA" type="xs:string" />
              <xs:element name="Año" type="xs:string" />
              <xs:element name="Cancion" minOccurs="1" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:sequence>

                    <xs:element name="NombreT" type="xs:string" />

                    <xs:element name="Duracion" type="xs:string" />

                    <xs:element name="Version" minOccurs="0" maxOccurs="unbounded">
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element name="Nombre" />
                          <xs:element name="IML" type="xs:string" />
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>

                  </xs:sequence>                  
                  <xs:attribute type="xs:string" name="estilo" use="optional"/>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="tipo" use="required">
              <xs:simpleType>
                <xs:restriction base="xs:string">
                  <xs:enumeration value="nuevo" />
                  <xs:enumeration value="recopilatorio" />
                  <xs:enumeration value="mixto" />
                </xs:restriction>
              </xs:simpleType>
            </xs:attribute>
            <xs:attribute name="temas" use="optional">
              <xs:simpleType>
                <xs:restriction base="xs:integer">
                  <xs:minInclusive value="1" />
                  <xs:maxInclusive value="30" />
                </xs:restriction>
              </xs:simpleType>
            </xs:attribute>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Could someone tell me where I am wrong?

After these three changes, your XML will be valid against your XSD:

  1. Remove the targetNamespace="http://www.w3schools.com" from your XSD.

  2. Add mixed="true" to the xs:complexType for Cancion :

     <xs:element name="Cancion" minOccurs="1" maxOccurs="unbounded"> <xs:complexType mixed="true"> 
  3. Associate an XSD with your XML via xsi:noNamespaceSchemaLocation :

     <Interprete xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="try.xsd"> 

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