Solved

Is it okay for XML element order to matter?

Posted on 2013-11-20
8
513 Views
Last Modified: 2013-11-26
Hello, I have created a flexible network interface block for Simulink, written in C (in the form of a CMEX block) that is XML-configurable.  Part of my XML format is as follows, for example:

<Reads>
<Read name="charge" type="INT32" ip="127.0.0.1" port="2501" register="23">
<Read name="voltage" type="INT32" ip="127.0.0.1" port="2501" register="27">
<Read name="level (ft)" type="INT32" ip="127.0.0.1" port="2501" register="29">
<Read name="speed (ft/s)" type="INT32" ip="127.0.0.1" port="2501" register="24">
</Reads>

What my block does is work down the list, collect the four values from the network, and then populate a run-time-sized "vector" (ie an array that is passed between my C code and the simulink model) with the four values [charge, voltage,level,speed].  The simulink must be designed to extract the four engineering values in this order and do something with them.  So this is a "contract" between the simulink design and the XML content.  The idea is that if you make changes to the system you can modify the list of gathered values without changing the C code, you just have to update the XML and the simulink to handle the gathered data.

Make any sense?

In any case, I was very proud to have made the C code "futureproof" (ie not have to be changed again, since it was a pain to change through the CMEX editor) with this flexible interface -- and be able to do anything needed by working with just Simulink and XML.   But I am getting robust push back on the fact that the order of those four nodes matter (ie if you mix up the order of two elements it will not work correctly).  The client feels that this isn't standard XML if the order matters, and feels that it is a maintainability issue for the author of the XML to get the order perfect (ie if they swap two rows then the values get crossed).

They would like to build in some sort of lookup table or index attribute so that the order of the nodes can be mixed up, but some intelligence on the C or Simulink side will put them in the proper order.

I think this will add needless complexity.  There is no way around the implied contract, no way to really reduce the maintenance burden, at best just move it around.

So any reactions?  Is the burden of getting the order correct too onerous?  And/or have I "broken a rule" by making order matter within my XML elements?
0
Comment
Question by:RonMexico
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 18

Assisted Solution

by:zc2
zc2 earned 250 total points
ID: 39664359
I think you should not worry. The nodes will be read in the same order as they are present in the XML file. If you still in doubt, you could add an incrementing index attribute to the elements and verify the order against it while doing the reading.
0
 

Author Comment

by:RonMexico
ID: 39664424
Is there anything about this that is "nonstandard" XML?
0
 
LVL 18

Expert Comment

by:zc2
ID: 39664607
Yes, your XML is not well-formed. The read elements should close themselves, like
<Read name="speed (ft/s)" type="INT32" ip="127.0.0.1" port="2501" register="24"/>
(Note the slash in the end).
Everything else looks fine.
Whatever method will be used to read the XML (event based or loading into a tree structure) the sequence of the elements will remain unchanged.
0
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 

Author Comment

by:RonMexico
ID: 39664616
Thanks zc2, that was just a typo.

I appreciate the input.  If you don't mind I'll leave this open a little bit to see if anyone has a different (or same) opinion... I'm trying to build an argument that we haven't done anything improper and the more opinions, the better.
0
 
LVL 18

Expert Comment

by:zc2
ID: 39664631
sure
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39665670
Your client does kind of have a point - XML parsers do not guarantee the order in which nodes are retrieved. Generally they will pull them in the order they are in the document but it isn't guaranteed that they will.

An extra attribute to denote a sequence would remove the issue. I'm guessing your client just wants something more definitive in the document that explicitly states the sequence the nodes are intended to be interpreted in.

You need to remember that XML purely describes the data, there is no implied ordering or formatting associated with it.
0
 

Author Comment

by:RonMexico
ID: 39665696
Carl, thank you.  If that's true then I'll have to do what the client says.

I'm surprised though, that XML spec wouldn't guarantee that.  If they did then order could matter, it would be up to the application... And it wouldn't harm the majority of applications for which it doesn't.

Do you have a reference for that non-guarantee, by the way ?  Thanks again.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 250 total points
ID: 39666925
What you are dealing with in your document are Mixed content elements. What the specification says is:

3.2.2 Mixed Content

[Definition: An element type has mixed content when elements of that type may contain character data, optionally interspersed with child elements.] In this case, the types of the child elements may be constrained, but not their order or their number of occurrences:
But, as I mentioned earlier, most parsers will honour the order in the document, but it isn't guaranteed.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Fluent Interface Design Pattern You can use the Fluent Interface (http://en.wikipedia.org/wiki/Fluent_interface) design pattern to make your PHP code easier to read and maintain.  "Fluent Interface" is an object-oriented design pattern that r…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question