问题
I need to create XML from this XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="group">
<xs:complexType>
<xs:sequence>
<xs:element name="person" minOccurs="5" maxOccurs="20" type="xs:string"/>
</xs:sequence>
<xs:attribute name="name" use="required" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:schema>
Here is the XML I've tried:
<?xml version="1.0" ?>
<group name="abcd">
xmlns="www.example.org"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="ex1.xsd">
<person>Joao</person>
<person>Andre</person>
<person>Filipe</person>
<person>Joaquim</person>
<person>Rui</person>
</group>
I am getting this error:
Not valid. Error - Line 10, 9: org.xml.sax.SAXParseException; lineNumber: 10; columnNumber: 9; cvc-complex-type.2.3: Element 'group' cannot have character [children], because the type's content type is element-only.
回答1:
Number of issues:
- As Filburt mentioned, you've prematurely closed the opening
group
tag. This is the direct cause of your immediate error. It causes the parser to misinterpret what you intend to be attributes as text content to thegroup
element. schemaLocation
must take namespace-XSD pairs.elementFormDefault="qualified"
- etc.
Altogether, the following XSD will validate the following XML successfully.
XSD
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org"
elementFormDefault="qualified">
<xs:element name="group">
<xs:complexType>
<xs:sequence>
<xs:element name="person" minOccurs="5" maxOccurs="20" type="xs:string"/>
</xs:sequence>
<xs:attribute name="name" use="required" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:schema>
XML
<?xml version="1.0" ?>
<group name="abcd"
xmlns="http://www.example.org"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org ex1.xsd">
<person>Joao</person>
<person>Andre</person>
<person>Filipe</person>
<person>Joaquim</person>
<person>Rui</person>
</group>
回答2:
Change the 'group' definition in XSD to include mixed="true"
<xs:element name="group">
<xs:complexType mixed="true">
来源:https://stackoverflow.com/questions/42010528/cvc-complex-type-2-3-element-group-cannot-have-character-children-because