Cristina García
Cristina García

Reputation: 23

XSD Schema using element with child and attribute error

I created a "database" of car models in xml. I'm having trouble using XSD Schema to validate it. The problem is that this structure is not working as a element with attribute and a child:

    <xs:element name="revision">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="observaciones" type="xs:string"></xs:element>
        </xs:sequence>
        <xs:attribute name="fecha" type="xs:string" use="required"></xs:attribute>
    </xs:complexType>

I use it multiple times in the XSD but it does not recognize the attribute, resulting also in this error :

cvc-complex-type.3.2.2: Attribute 'numero' is not allowed to appear in element 'coche'

These are the full codes:

XML

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

<coches xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="coches.xsd">
    <coche numero="1">
        <marca>Audi</marca>
        <modelo>A1 8X</modelo>
        <anio>2010</anio>
        <color>negro brillante</color>
        <precio moneda="EUR">8600</precio>
        <potencia>105</potencia>
        <combustible>Gasolina</combustible>
        <transmision>Manual</transmision>
        <kilometraje>101001</kilometraje>
        <revisiones>
            <revision fecha="21/01/2025">
                <observaciones>Retrovisor roto y luz trasera fundida</observaciones>
            </revision>
        </revisiones>
    </coche>

XSD:

    <?xml   version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="coches">
        <xs:complexType mixed="true">
            <xs:sequence> <!-- Utilizamos Sequience porque como es una BBDD necesitamos que los
                campos esten en orden-->
                <xs:element name="coche" minOccurs="1" maxOccurs="unbounded"> <!-- Como mínimo debe
                    haber 1 coche y no hay máximo-->
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="marca" type="xs:string"></xs:element>
                            <xs:element name="modelo" type="xs:string"></xs:element>
                            <xs:element name="anio" type="anio"></xs:element> <!-- Usamos el tipo que
                            hemos definido como año-->
                            <xs:element name="color" type="xs:string"></xs:element>
                            <!-- El precio lleva un atributo, por tanto tenemos que utilizar
                            xs:simpleContent y establecer que la base o el tipo de datos es de tipo
                            "anio"
                            y el atributo "moneda" es del tipo "moneda" como hemos decidido más abajo-->
                            <xs:element name="precio">
                                <xs:complexType>
                                    <xs:simpleContent>
                                        <xs:extension base="precio">
                                            <xs:attribute name="moneda" type="moneda" />
                                        </xs:extension>
                                    </xs:simpleContent>
                                </xs:complexType>
                            </xs:element>
                            <xs:element name="potencia" type="xs:string"></xs:element>
                            <xs:element name="combustible" type="combustible"></xs:element> <!-- ya
                            tenemos definido el tipo combustible abajo-->
                            <xs:element name="transmision" type="transmision"></xs:element>
                            <xs:element name="kilometraje" type="kilometraje"></xs:element>
                            <xs:element name="revisiones">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="revision">
                                            <xs:complexType>
                                                <xs:sequence>
                                                    <xs:element name="observaciones" type="xs:string"></xs:element>
                                                </xs:sequence>
                                                <xs:attribute name="fecha" type="xs:string" use="required"></xs:attribute>
                                            </xs:complexType>
                                        </xs:element>
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
            <xs:attribute name="numero" type="xs:integer"></xs:attribute>
        </xs:complexType>
    </xs:element>

    <!-- Restricciones en los tipos de datos-->
    <!-- Los escribo de forma externa-->
    <xs:simpleType name="anio">
        <xs:restriction base="xs:integer">
            <xs:minInclusive value="1950" /> <!-- Los años de los coches deben estar entre 1950 y
            2025-->
            <xs:maxInclusive value="2025" />
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="moneda"> <!-- Las monedas son en EUR o USD-->
        <xs:restriction base="xs:string">
            <xs:enumeration value="EUR" />
            <xs:enumeration value="USD" />
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="combustible">
        <xs:restriction base="xs:string">
            <xs:enumeration value="Gasolina" />
            <xs:enumeration value="GLP" />
            <xs:enumeration value="Diesel" />
            <xs:enumeration value="hibrido" />
            <xs:enumeration value="electrico" />
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="transmision">
        <xs:restriction base="xs:string">
            <xs:enumeration value="Manual" />
            <xs:enumeration value="Automático" />
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="kilometraje">
        <xs:restriction base="xs:integer">
            <xs:minInclusive value="0" /> <!-- El kilometraje no puede ser negativo-->
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="precio">
        <xs:restriction base="xs:integer">
            <xs:minInclusive value="0"></xs:minInclusive> <!-- El precio no puede ser negativo-->
        </xs:restriction>
    </xs:simpleType>

</xs:schema>

Any ideas? Thank you

Upvotes: 1

Views: 40

Answers (0)

Related Questions