简体   繁体   中英

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?

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.

The XML is valid and well-formed. However, if the order of the elements is important, you should think carefully about the design your document. An equally valid document would show fix power supply first and fix fan second. Or even both <NoInc> elements first and then both <Step> elements.

In addition, if the <NoInc> element depends on the <Step> element in some way, you should show this relationship in your design either by making the NoInc text an attribute of the <Step> element or creating a parent element for each pair of <Step> and <NoInc> elements.

For example:

  <Task>
      <SubTask>
          <step>fix fan</step>
          <NoInc>RT260454</NoInc>
      </SubTask>
      <SubTask>
          <step>fix power supply</step>
          <NoInc>RT260456</NoInc>
      </SubTask>
  </Task>

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM