简体   繁体   English

当针对XSD验证XML时,为什么会出现org.xml.sax.SAXParseException?

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

Trying to make proper XML, but keeping getting this error : 尝试制作适当的XML,但不断出现此错误:

Error - Line 13, 16: org.xml.sax.SAXParseException; 错误-第13行,16:org.xml.sax.SAXParseException; lineNumber: 13; lineNumber:13; columnNumber: 16; columnNumber:16; cvc-complex-type.2.4.a: Invalid content was found starting with element 'me:myself'. cvc-complex-type.2.4.a:发现无效的内容,以元素“ me:myself”开头。 One of '{"urn:Test.Namespace":myself}' is expected. 预期为“ {{urn:Test.Namespace“:myself}”之一。

Testing here : 在这里测试:

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

Code: .xml: 代码:.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: .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. 这意味着您将其用作ComplexType。 However, your xsd file doesn't states that "myself" is a ComplexType. 但是,您的xsd文件没有声明“我自己”是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. 您必须将xsd文件中的“ myself”元素定义为ComplexType,就像您对“ laptopy”元素所做的一样。

Also, don't use "me:myself" or "uek:copyright", etc. Just use "myself" and "copyright". 另外,请勿使用“ me:myself”或“ uek:copyright”等。只需使用“ myself”和“ copyright”即可。

Finally, your "group" element should have the integer type (as your xsd states), but you use it with a string "Kr1013". 最后,“ group”元素应具有整数类型(如xsd所示),但是您可以将其与字符串“ 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代码,它在您的XSD模式(我未修改)方面已经形成并且有效:

 <?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. 如果您想了解XML和XSD模式,建议您阅读这些内容。 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 这是快速的并且应该易于理解: https : //www.w3schools.com/xml/xml_whatis.asp https://www.w3schools.com/xml/schema_intro.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM