How do I put multiple occurrences of elements that are part of a sequence that is inside a complex type?
I have the following schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="Measurements">
<xs:annotation>
<xs:documentation>Schema for Measurements data transmission</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element name="m_doc" type="xs:string"/>
<xs:element name="m_parameters">
<xs:complexType>
<xs:sequence>
<xs:element name="m_name" type="xs:string"/>
<xs:element name="m_isEngValue" type="xs:boolean"/>
<xs:element name="m_unit" type="xs:string"/>
<xs:element name="m_radix" type="xs:string"/>
<xs:element name="m_value" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
And I try with the following XML:
<?xml version="1.0" encoding="UTF-8"?>
<Measurements xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<m_doc>TestDoc</m_doc>
<m_parameters>
<!-- Primer grupo -->
<m_name>Length</m_name>
<m_isEngValue>false</m_isEngValue>
<m_unit></m_unit>
<m_radix>HEXADECIMAL</m_radix>
<m_value>0</m_value>
<!-- Segundo grupo -->
<m_name>Height</m_name>
<m_isEngValue>false</m_isEngValue>
<m_unit></m_unit>
<m_radix>DECIMAL</m_radix>
<m_value>0</m_value>
</m_parameters>
</Measurements>
Getting the error:
Cvc-complex-type.2.4.d: Invalid Content Was Found Starting With Element 'm_name'. No Child Element Is Expected At This Point., Line '13', Column '15'.
This error corresponds to the second group of 5 elements that constitute a parameter. If I remove that second group then I have no error.
I'm testing with an online validator: www.freeformatter.com
The schema is a requirement over which I have no control and am bound to abide by. I have to make XML that conforms to that schema.
The schema in your question does not allow any repetition of elements because neither the element
xs:sequence
nor one of the elementsxs:element
carries theminOccurs
/ attributesmaxOccurs
. This means that the scheme only allows the entire sequence, and only once.If you could switch
<xs:sequence>
to<xs:sequence maxOccurs="unbounded">
( https://www.w3.org/TR/xmlschema-1/#declare-contentModel ), you could repeat the entire sequence as many times as you wanted. In addition you could allow for example 10 repetitions with<xs:sequence maxOccurs="10">
.The element
xs:element
also allows adding the two attributes ( https://www.w3.org/TR/xmlschema-1/#declare-element ).