为什么在根据 XSD 验证 XML 时得到 org.xml.sax.SAXParseException?
Why am I getting org.xml.sax.SAXParseException when validate XML against XSD?
正在尝试正确 XML,但一直出现此错误:
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.
在这里测试:
代码:
.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>
您将 "name"、"surname"、...放在 "myself" 元素中。这意味着您将它用作 ComplexType。
但是,您的 xsd 文件并未声明 [=31=] 是 ComplexType。
您必须将 xsd 文件中的 "myself" 元素定义为 ComplexType,就像您为 "laptopy" 元素所做的那样。
此外,不要使用"me:myself"或"uek:copyright"等。只需使用"myself"和"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="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>
如果您想了解 XML 和 XSD 架构,我建议您阅读这些。这很快并且应该很容易理解:
https://www.w3schools.com/xml/xml_whatis.asp
https://www.w3schools.com/xml/schema_intro.asp
正在尝试正确 XML,但一直出现此错误:
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.
在这里测试:
代码: .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>
您将 "name"、"surname"、...放在 "myself" 元素中。这意味着您将它用作 ComplexType。 但是,您的 xsd 文件并未声明 [=31=] 是 ComplexType。 您必须将 xsd 文件中的 "myself" 元素定义为 ComplexType,就像您为 "laptopy" 元素所做的那样。
此外,不要使用"me:myself"或"uek:copyright"等。只需使用"myself"和"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="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>
如果您想了解 XML 和 XSD 架构,我建议您阅读这些。这很快并且应该很容易理解: https://www.w3schools.com/xml/xml_whatis.asp https://www.w3schools.com/xml/schema_intro.asp