Create a sequence in a complex type in XML and validate it against a schema

4

How do I put several repetitions of the elements that are part of a sequence that is within a complex type?

I have the following scheme:

<?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 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 scheme is a requirement over which I have no control and I am obliged to respect. I have to make XML that fits that scheme.

    
asked by Jose Antonio Dura Olmos 09.06.2017 в 13:33
source

1 answer

3

The schema in your question does not allow any repetition of elements because neither the xs:sequence element nor one of the xs:element elements carries the attributes minOccurs / maxOccurs . This means that the scheme only allows the complete sequence, and only once.

If you could change <xs:sequence> to <xs:sequence maxOccurs="unbounded"> ( link ), you could repeat The whole sequence as many times as you want. Also you could allow for example 10 repetitions with <xs:sequence maxOccurs="10"> .

The xs:element element also allows you to add the two attributes ( link ).

    
answered by 09.06.2017 / 14:21
source