简体   繁体   中英

Why am I getting org.xml.sax.SAXParseException when validate XML against XSD?

Trying to make proper XML, but keeping getting this error :

Error - Line 13, 16: org.xml.sax.SAXParseException; lineNumber: 13; columnNumber: 16; cvc-complex-type.2.4.a: Invalid content was found starting with element 'me:myself'. One of '{"urn:Test.Namespace":myself}' is expected.

Testing here :

http://www.utilities-online.info/xsdvalidation

Code: .xml:

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

<root
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="urn:Test.Namespace"
        xsi:schemaLocation="urn:Test.Namespace Laptopy-ShchypylovNikita.xsd"
        xmlns:me="me@me.com"
        xmlns:uek="www.uek.krakow.pl"

>
    <me:myself>

        <me:name>Nikita</me:name>
        <me:surename>Shchypylov</me:surename>
        <me:sex>male</me:sex>
        <me:age>19</me:age>
        <me:city>Cracow</me:city>
        <me:homeland>Ukraine</me:homeland>
        <me:course>IT</me:course>
        <me:numer>1</me:numer>
        <me:group>Kr1013</me:group>
    </me:myself>

    <uek:copyright >
        Wydzial Zarzadzania, Universytet Economiczny, Krakow
    </uek:copyright>
    <laptopy>
        <laptop type="dell">
            <ekran producent="samsung">
                wide
            </ekran>
            <card from="intel">
                very good
            </card>
            <gui>
                dynamic
            </gui>
            <ram>
                8
            </ram>
            <material made="in_china">
                steel
            </material>
        </laptop>
        <laptop type="samsung">
            <ekran producent="samsung">
                wide
            </ekran>
            <card from="intel">
                good
            </card>
            <gui>
                dynamic
            </gui>
            <ram>
                8
            </ram>
            <material made="in_china">
                steel
            </material>
        </laptop>
        <laptop type="dell">
            <ekran producent="samsung">
                wide
            </ekran>
            <card from="intel">
                bad
            </card>
            <gui>
                dynamic
            </gui>
            <ram>
                8
            </ram>
            <material made="in_china">
                steel
            </material>
        </laptop>
        <laptop type="razer">
            <ekran producent="samsung">
                wide
            </ekran>
            <card from="intel">
                very good
            </card>
            <gui>
                dynamic
            </gui>
            <ram>
                8
            </ram>
            <material made="in_china">
                steel
            </material>
        </laptop>
        <laptop type="dell">
            <ekran producent="samsung">
                wide
            </ekran>
            <card from="intel">
                perfect
            </card>
            <gui>
                dynamic
            </gui>
            <ram>
                8
            </ram>
            <material made="in_china">
                wood
            </material>
        </laptop>
        <laptop type="apple">
            <ekran producent="samsung">
                wide
            </ekran>
            <card from="intel">
                very good
            </card>
            <gui>
                dynamic
            </gui>
            <ram>
                8
            </ram>
            <material made="in_china">
                steel
            </material>
        </laptop>
        <laptop type="asus">
            <ekran producent="samsung">
                wide
            </ekran>
            <card from="intel">
                excellent
            </card>
            <gui>
                dynamic
            </gui>
            <ram>
                8
            </ram>
            <material made="in_china">
                wood
            </material>
        </laptop>
        <laptop type="toshiba">
            <ekran producent="samsung">
                wide
            </ekran>
            <card from="intel">
                very good
            </card>
            <gui>
                dynamic
            </gui>
            <ram>
                8
            </ram>
            <material made="in_china">
                steel
            </material>
        </laptop>
        <laptop type="dell">
            <ekran producent="samsung">
                wide
            </ekran>
            <card from="intel">
                very good
            </card>
            <gui>
                dynamic
            </gui>
            <ram>
                8
            </ram>
            <material made="in_china">
                steel
            </material>
        </laptop>
        <laptop type="samsung">
            <ekran producent="samsung">
                wide
            </ekran>
            <card from="intel">
                very good
            </card>
            <gui>
                dynamic
            </gui>
            <ram>
                8
            </ram>
            <material made="in_china">
                wood
            </material>
        </laptop>
    </laptopy>
</root>

.xsd:

<?xml version="1.0"?>

<!--Nikita Shchypylov-->
<!--Krdzis1013-->

<xsd:schema
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        targetNamespace="urn:Test.Namespace"
        xmlns="urn:Test.Namespace"
        elementFormDefault="qualified"
        xmlns:me="me@me.com"
        xmlns:uek="www.uek.krakow.pl"

>

    <xsd:element name="root">
        <xsd:complexType>
            <xsd:sequence>
                                <xsd:element name="myself" maxOccurs="unbounded">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="name" type="xsd:string"/>
                        <xsd:element name="surename" type="xsd:string"/>
                        <xsd:element name="sex" type="xsd:string"/>
                        <xsd:element name="age" type="xsd:integer"/>
                        <xsd:element name="city" type="xsd:string"/>
                        <xsd:element name="homeland" type="xsd:string"/>
                        <xsd:element name="course" type="xsd:string"/>
                        <xsd:element name="numer" type="xsd:integer"/>
                        <xsd:element name="group" type="xsd:integer"/>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
                <xsd:element name="copyright" maxOccurs="unbounded"/>
                <xsd:element name="laptopy" maxOccurs="unbounded">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element name="laptop"
                                         minOccurs="10" maxOccurs="unbounded">
                                <xsd:complexType>
                                    <xsd:sequence>
                                        <xsd:element name="ekran" type="ekranType"/>
                                        <xsd:element name="card" type="cardType"/>
                                        <xsd:element name="gui" type="xsd:string"/>
                                        <xsd:element name="ram" type="xsd:integer"/>
                                        <xsd:element name="material" type="materialType">
                                        </xsd:element>
                                    </xsd:sequence>
                                    <xsd:attribute name="type" type="xsd:string"/>
                                </xsd:complexType>
                            </xsd:element>
                        </xsd:sequence>
                    </xsd:complexType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>

    <xsd:complexType name="ekranType">
        <xsd:simpleContent>
            <xsd:extension base="xsd:string">
                <xsd:attribute name="producent" type="xsd:string"/>
            </xsd:extension>
        </xsd:simpleContent>
    </xsd:complexType>

    <xsd:complexType name="cardType">
        <xsd:simpleContent>
            <xsd:extension base="xsd:string">
                <xsd:attribute name="from" type="xsd:string"/>
            </xsd:extension>
        </xsd:simpleContent>
    </xsd:complexType>

    <xsd:complexType name="materialType">
        <xsd:simpleContent>
            <xsd:extension base="xsd:string">
                <xsd:attribute name="made" type="xsd:string"/>
            </xsd:extension>
        </xsd:simpleContent>
    </xsd:complexType>

</xsd:schema>

You put "name", "surname", ... inside your "myself" element. This means that you use it as a ComplexType. However, your xsd file doesn't states that "myself" is a ComplexType. You must define your "myself" element in the xsd file to be of ComplexType, just as you did for your "laptopy" element for instance.

Also, don't use "me:myself" or "uek:copyright", etc. Just use "myself" and "copyright".

Finally, your "group" element should have the integer type (as your xsd states), but you use it with a string "Kr1013".

See the fixed XML code bellow which is now well formed and valid with regards to your XSD schema (which I did not modify):

 <?xml version="1.0" encoding="utf-8" ?> <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:Test.Namespace" xsi:schemaLocation="urn:Test.Namespace Laptopy-ShchypylovNikita.xsd" xmlns:me="me@me.com" xmlns:uek="www.uek.krakow.pl" > <myself> <name>Nikita</name> <surename>Shchypylov</surename> <sex>male</sex> <age>19</age> <city>Cracow</city> <homeland>Ukraine</homeland> <course>IT</course> <numer>1</numer> <group>1013</group> </myself> <copyright > Wydzial Zarzadzania, Universytet Economiczny, Krakow </copyright> <laptopy> <laptop type="dell"> <ekran producent="samsung"> wide </ekran> <card from="intel"> very good </card> <gui> dynamic </gui> <ram> 8 </ram> <material made="in_china"> steel </material> </laptop> <laptop type="samsung"> <ekran producent="samsung"> wide </ekran> <card from="intel"> good </card> <gui> dynamic </gui> <ram> 8 </ram> <material made="in_china"> steel </material> </laptop> <laptop type="dell"> <ekran producent="samsung"> wide </ekran> <card from="intel"> bad </card> <gui> dynamic </gui> <ram> 8 </ram> <material made="in_china"> steel </material> </laptop> <laptop type="razer"> <ekran producent="samsung"> wide </ekran> <card from="intel"> very good </card> <gui> dynamic </gui> <ram> 8 </ram> <material made="in_china"> steel </material> </laptop> <laptop type="dell"> <ekran producent="samsung"> wide </ekran> <card from="intel"> perfect </card> <gui> dynamic </gui> <ram> 8 </ram> <material made="in_china"> wood </material> </laptop> <laptop type="apple"> <ekran producent="samsung"> wide </ekran> <card from="intel"> very good </card> <gui> dynamic </gui> <ram> 8 </ram> <material made="in_china"> steel </material> </laptop> <laptop type="asus"> <ekran producent="samsung"> wide </ekran> <card from="intel"> excellent </card> <gui> dynamic </gui> <ram> 8 </ram> <material made="in_china"> wood </material> </laptop> <laptop type="toshiba"> <ekran producent="samsung"> wide </ekran> <card from="intel"> very good </card> <gui> dynamic </gui> <ram> 8 </ram> <material made="in_china"> steel </material> </laptop> <laptop type="dell"> <ekran producent="samsung"> wide </ekran> <card from="intel"> very good </card> <gui> dynamic </gui> <ram> 8 </ram> <material made="in_china"> steel </material> </laptop> <laptop type="samsung"> <ekran producent="samsung"> wide </ekran> <card from="intel"> very good </card> <gui> dynamic </gui> <ram> 8 </ram> <material made="in_china"> wood </material> </laptop> </laptopy> </root> 

In case you want to learn about XML and XSD schema, I suggest you read these. This is quick and should be easy to understand: https://www.w3schools.com/xml/xml_whatis.asp https://www.w3schools.com/xml/schema_intro.asp

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