XML无法通过XSD验证的问题排查与解决方案请求
Let's work through your two validation issues one by one, then share corrected versions of both files to get everything working.
Error 1: s4s-elt-must-match.1
What's going wrong?
This is a common XML Schema gotcha—you placed <xs:attribute> elements directly inside <xs:sequence> blocks, which breaks the schema rules. Sequences are only meant to organize child elements (like <element> or <group>), not attributes. Attributes need to live as direct children of their parent <xs:complexType>, right after the closing tag of the sequence.
How to fix it
Move all attribute definitions out of the <xs:sequence> containers and into their corresponding <xs:complexType> sections. For example, the mmr attribute for <team> should go after the </xs:sequence> tag of the team's complex type.
Error 2: cvc-elt.1.a: 无法找到元素'esport'的声明
What's going wrong?
There are a few overlapping issues here:
- Missing namespace & schema link: Your XSD defines a target namespace (
http://www.w3schools.com), but your XML doesn't declare this namespace or reference the XSD file. - Element name mismatches: XML uses
<dota2>but your XSD defines<dota>; XML has<member>elements inside<members>but your XSD never defines<member>(instead, you incorrectly added attributes directly to<members>). - Required attribute conflict: The second
<team>in your XML has nommrattribute, but your XSD didn't markmmras optional (it's required by default). - Typos & invalid type: You wrote
<xs:patern>instead of<xs:pattern>, and tried to apply a pattern to anxs:integertype (invalid, sincekda_avghas slashes—usexs:stringinstead).
How to fix it
- Add namespace declarations and a schema location to the XML root element
- Align element names between XML and XSD (rename XSD's
<dota>to<dota2>) - Define the
<member>element properly inside<members> - Mark
mmras optional withuse="optional" - Fix the
patterntypo and correct the type forkda_avg
Corrected XSD
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com" elementFormDefault="qualified"> <xs:element name="esport"> <xs:complexType> <xs:sequence> <xs:element name="dota2"> <xs:complexType> <xs:sequence> <xs:element name="team" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="country" type="xs:string"/> <xs:element name="members"> <xs:complexType> <xs:sequence> <xs:element name="member" maxOccurs="unbounded"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="role" use="required"> <xs:restriction base="xs:string"> <xs:enumeration value="solomid"/> <xs:enumeration value="support"/> <xs:enumeration value="carry"/> <xs:enumeration value="offlaner"/> </xs:restriction> </xs:attribute> <xs:attribute name="kda_avg" use="required"> <xs:restriction base="xs:string"> <xs:pattern value="[0-9]{1,3}/[0-9]{1,3}/[0-9]{1,3}"/> </xs:restriction> </xs:attribute> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> <xs:attribute name="mmr" type="xs:integer" use="optional"> <xs:restriction base="xs:integer"> <xs:minInclusive value="3000"/> <xs:maxInclusive value="7000"/> </xs:restriction> </xs:attribute> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
Corrected XML
<?xml version="1.0" encoding="utf-8"?> <esport xmlns="http://www.w3schools.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com your-schema-file-name.xsd"> <dota2> <team mmr="5660"> <name>The Alliance</name> <country>Sweden</country> <members> <member role="solomid" kda_avg="12/5/4">s4</member> <member role="offlaner" kda_avg="9/6/5">Admiral</member> <member role="support" kda_avg="7/7/3">Akke</member> <member role="support" kda_avg="6/7/2">EGM</member> <member role="carry" kda_avg="15/6/5">Loda</member> </members> </team> <team> <name>Neolution Orange</name> <country>Malaysia</country> <members> <member role="carry" kda_avg="12/5/4">Mushi</member> <member role="support" kda_avg="9/6/5">Net</member> <member role="offlaner" kda_avg="7/7/3">ky.xy</member> <member role="support" kda_avg="6/7/2">XtincT</member> <member role="solomid" kda_avg="15/6/5">Ohaiyo</member> </members> </team> </dota2> </esport>
内容的提问来源于stack exchange,提问作者Khairul Izzuan

