I have to write a schema where it does not matter the order of nested elements, and there could more nested elements of the same type.
I tried to use xs:all
and xs:sequence
, but it doesn't work.
This XML should be valid:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test.xsd">
<elementB>B</elementB>
<elementA>A</elementA>
<elementA>A</elementA>
</test>
and here are the XSDs which I tried:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="test">
<xs:complexType>
<xs:sequence>
<xs:element name="elementA" type="xs:string"
minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="elementB" type="xs:string"
minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
First XSD schema has problem with ordering.
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
vc:minVersion="1.1">
<xs:element name="test">
<xs:complexType>
<xs:all>
<xs:element name="elementA" type="xs:string"
minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="elementB" type="xs:string"
minOccurs="0" maxOccurs="unbounded"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
The second XSD looks promising but I can't setup Intellij to use it correctly for XML. (I switched ussing of XML Schema 1.1 here File->Settings->Languages & Frameworks->Schemas and DTDs->Default XML Schemas
) My IntelliJ version is 2019.2.3.
Clearly the xs:sequence
version of the XSD imposes an ordering.
The xs:all
version of the XSD uses xs:all/xs:element[@maxOccurs="unbounded"]
, which requires XSD 1.1.
XML Oxygen validates your XML against your xs:all
XSD successfully.
IntelliJ has XSD 1.1 support, but perhaps it's incomplete, or perhaps the spec is ambiguous here – no time to investigate that right now.
However, if your actual case is as simple as your posted case, you can use xs:choice
to achieve order independence without a dependency on XSD 1.1:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
vc:minVersion="1.0">
<xs:element name="test">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="elementA" type="xs:string"/>
<xs:element name="elementB" type="xs:string"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
Hopefully this will allow you to work-around any IntelliJ issue that may have been blocking you.
Your second schema (the "promising" one) works for me. Tried it in oXygen and it works fine. I don't know how to set up IntelliJ for XSD 1.1 validation, I'm afraid.
If any of the elements in a fixed set (A, B, C...) can appear any number of times then you don't need XSD 1.1: you can write the content model as
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="A">
<xs:element name="B">
<xs:element name="C">
</xs:choice>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.