Search code examples
xml

xml - repeating elements - is this valid?


This XML seems to be valid according to on-line validation servces, but I suspect that each step should be wrapped in a tag to make it unique. What rule is this violating?

<tasks>                                               
       <step>fix fan</step>
       <NoInc>RT260454</NoInc>             

       <step>fix power supply</step>
       <NoInc>RT260456</NoInc>                 
</tasks> 

Is it better to express like this?

<tasks>                                               
   <task>
       <step>fix fan</step>
       <NoInc>RT260454</NoInc>             
   </task>
   <task>        
       <step>fix power supply</step>
       <NoInc>RT260456</NoInc>                 
   </task>
</tasks> 

When mapped this to an array, would I risk overriding the first step with the second?


Solution

  • It is valid xml, here is an XSD that supports that xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
          xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="tasks" type="tasksType"/>
      <xs:complexType name="tasksType">
        <xs:choice maxOccurs="unbounded" minOccurs="0">
          <xs:element type="xs:string" name="step"/>
          <xs:element type="xs:string" name="NoInc"/>
        </xs:choice>
      </xs:complexType>
    </xs:schema>
    

    If a 1 to 1 mapping is required between step and noinc, then it would make sense to wrap them in another tag.