0
\$\begingroup\$

For this flowchart on finding the minimum of 2 numbers:

enter image description here

I've created XML for this flowchart:

<start>
    <InOut>
      <in>  a </in>
      <in>  b </in>
      <out> min </out>
    </InOut>
    <calc>
        <ins> a=3 </ins>
        <ins> b=5 </ins>
        <ins> min=a </ins>
    </calc>    
    <con gurd="min>b"  type="if">
        <calc>
            <ins>min=b</ins>
        </calc>
    </con>
    <con gurd="min>b" type="else">
        <calc>
            <ins>min=a</ins>
        </calc>
    </con>
</start>

But I don't think it's not good XML coding. What are your suggests for improving this?

\$\endgroup\$
  • \$\begingroup\$ Does it still work properly? \$\endgroup\$ – Jamal Jul 12 '14 at 20:43
  • \$\begingroup\$ Wait, what's the significance of the C++ code? \$\endgroup\$ – Jamal Jul 12 '14 at 20:47
  • \$\begingroup\$ @Jamal c++ is sample and not important for me i want create xml for code like that \$\endgroup\$ – Mohammad Jul 12 '14 at 20:52
  • \$\begingroup\$ Why are you trying to make it look like a completely different language? Why not the most concise in the particular language you're using? The least you could do is replace the C++ code with pseudocode. \$\endgroup\$ – Jamal Jul 12 '14 at 20:53
  • \$\begingroup\$ i have flowchart for detect minimum in 2 number i write c++ code replace flowchart image i edit this code and replace flowchart with c++ code @Jamal \$\endgroup\$ – Mohammad Jul 12 '14 at 20:56
3
\$\begingroup\$

There are some confusing aspects to your XML, and some practices that you may end up appreciating.....

Technical

Your XML is valid, but it does not follow best practices. The XML:

   <con gurd="min>b"  type="if">

This should be written:

    <con gurd="min&gt;b"  type="if">

Even though your XML is valid, the > in the attribute value can lead to confusion. Even the XML Syntax parser here on Stack Exchange is failing to parse that part right....

Naming

XML is by nature relatively verbose. You are already 'paying' for that, so you may as well use it to your advantage. con should be condition, ins should be ..... instruction ? What is gurd ?

Additionally, you should be consistent in cases. InOut should be inputoutput or something. Mixing cases for XML tag names is a problem. I recommend making it all lowercase. I would actually call it 'interface', and have 'input' and 'output' as child elements.

Functional

When you use XML, you need a parser. There is always a balance between what you parse, and what you let the parser parse. The 'text' component in the XML elements are technically called PCDATA (Parsed Character Data). The XML Parser will Parse that data looking for more XML. That's all it does. Your actual data has things like: min=a Is your application manually parsing that content as well? If it is, you should probably make your text content attribute values instead.

Additionally, the white-space in your element text is scattered all over the place. Consider:

<calc>
    <ins> a=3 </ins>
    <ins> b=5 </ins>
    <ins> min=a </ins>
</calc>    
<con gurd="min>b"  type="if">
    <calc>
        <ins>min=b</ins>
    </calc>
</con>

In your first <ins> elements you have space surrounding the text. In your later <ins> there is no space.

Is the space important? If it is, you should either use attributes, or set xml:space="preserve" on your XML.

Update

FYI, I ran your XML through xmllint and it output your XML as:

<?xml version="1.0"?>
<start>
  <InOut>
    <in>  a </in>
    <in>  b </in>
    <out> min </out>
  </InOut>
  <calc>
    <ins> a=3 </ins>
    <ins> b=5 </ins>
    <ins> min=a </ins>
  </calc>
  <con gurd="min&gt;b" type="if">
    <calc>
      <ins>min=b</ins>
    </calc>
  </con>
  <con gurd="min&gt;b" type="else">
    <calc>
      <ins>min=a</ins>
    </calc>
  </con>
</start>

xmllint --format your.xml is a fantastic commandline tool that validates and reformats your XML to be consistent. I recommend it to anyone using XML that has access to Linux

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ that has access to Linux ... how exactly would one go about not having access to Linux? \$\endgroup\$ – nhgrif Jul 12 '14 at 23:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.