为什么在针对XSD验证XML时会收到org.xml.sax.SAXParseException?

问题描述 投票:0回答:1

尝试制作正确的XML,但不断收到此错误:

错误 - 第13行,第16行:org.xml.sax.SAXParseException; lineNumber:13; columnNumber:16; cvc-complex-type.2.4.a:从元素'me:myself'开始发现无效内容。其中一个'{“urn:Test.Namespace”:我自己}'是预期的。

在这里测试:

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="[email protected]"
        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="[email protected]"
        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>
java xml xsd xsd-validation
1个回答
0
投票

你把“名字”,“姓”,......放在你的“我自己”元素里面。这意味着您将其用作ComplexType。但是,您的xsd文件未声明“我自己”是ComplexType。您必须将xsd文件中的“我自己”元素定义为ComplexType,就像您对“laptopy”元素所做的那样。

另外,不要使用“我:我自己”或“uek:copyright”等。只需使用“我自己”和“版权”即可。

最后,您的“group”元素应该具有整数类型(作为您的xsd状态),但是您将其与字符串“Kr1013”一起使用。

请参阅下面的固定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="[email protected]"
        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>

如果您想了解XML和XSD架构,我建议您阅读这些内容。这很快,应该很容易理解:https://www.w3schools.com/xml/xml_whatis.asp https://www.w3schools.com/xml/schema_intro.asp

© www.soinside.com 2019 - 2024. All rights reserved.