6

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?

Abe
  • 298
  • 4
  • 12
  • 1
    It's not violating any rule. It's perfectly valid. – Robbert Jul 30 '14 at 00:51
  • @Robert: It's not violating any rule, because there are no rules with that document. Therefore it can not be valid (nothing to validate against). It's just well-formed XML. – hakre Aug 03 '14 at 19:02

2 Answers2

2

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.

Khary Mendez
  • 1,818
  • 1
  • 14
  • 18
  • -1: the answer is wrong. It's not valid XML, it's just well-formed. – hakre Aug 03 '14 at 19:01
  • Please refer to these links http://www.w3schools.com/xml/xml_dtd.asp http://stackoverflow.com/questions/134494/is-there-a-difference-between-valid-xml-and-well-formed-xml It is valid since there is a schema to which it validates (provided above). – Khary Mendez Aug 03 '14 at 20:28
  • The schema *is not* part of the question. The XML given in the question has no schema. Therefore it's not valid, just well-formed. See the SO link you've got in your comment. And forged w3schools for that matter. Not your fault, but it might look insulting if you paste links to the site here. – hakre Aug 03 '14 at 20:59
  • 3
    Thank you for the clarification. I see your perspective and agree no schema was provided in the question. It was implied there was a schema as the poster claims to have used an online validation service, which would require a schema if it is truly validating. I provided one as an example in my answer. – Khary Mendez Aug 03 '14 at 21:04
  • I'd say as there was not further reference given, that the OP actually did ask about validity not understanding what valid XML actually means. Abe most likely was asking about what are best practices to encode date into XML for later re-use (e.g. [serializing into an array](http://stackoverflow.com/q/6578832/367456)). However as that is opinion based, clearly off-topic. As your answer got accepted by the OP while *you* invented the schema, this seems to back-up the scenario I outline. – hakre Aug 03 '14 at 21:09
-1

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>