10

I would like to know if there are any xml coding standards.

<?xml version="1.0"?>
<overlay id="tutorboy-toolbar-Overlay" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
    <toolbox id="navigator-toolbox">
        <toolbar id="tutorboy-toolbar-Toolbar" 
                  toolbarname="TutorBoy Toolbar" 
                  accesskey="T" 
                  class="chromeclass-toolbar" 
                  context="toolbar-context-menu" 
                  hidden="false" 
                  persist="hidden">
              <toolbarbutton label="TutorBoy" 
                                id="tutorboy-toolbar-button-home" 
                                accesskey="d" 
                                image="chrome://tutorboy-toolbar/skin/logo.png" 
                                oncommand="loadURL('http://tutorboy.com');" 
                                tooltiptext="Click here to go to the Tutorboy.com homepage." /> 

is this way of arrangement allowed?

coderex
  • 27,225
  • 45
  • 116
  • 170

6 Answers6

16

The W3C defines an XML specification recommendation: http://www.w3.org/TR/REC-xml/

Since you've narrowed your question to XML formatting, there's no "universal" answer to how your XML should be formatted. Beyond conforming to whatever DTD or schema you happen to be dealing with, the importance of particular spacing/indentation of your tags lies with the people who will be dealing with your data.

If you're creating XML data to be sent across a network as part of a web service or some sort, then generally you're going to want to eliminate any unnecessary whitespace before transfer in order to optimize your data transfer rate. This means no line breaks, no indentation, no comments.

If your creating an XML document that others will be reading/modifying on a regular basis, then obviously you'll want to put some consideration into keeping the document readable. What constitutes "readable" is determined by everyone involved on that particular team or project.

Jeff L
  • 6,108
  • 3
  • 23
  • 29
  • 1
    Precisely. White space should be completely meaningless to the semantics, and its presence or absence is a matter of taste. And since XML is primarily *not* intended for human readability, formatting is of no particular concern. – GalacticCowboy Jul 02 '09 at 13:08
  • 3
    @Jeff L I'm sure when it was first conceived XML was not expected to be widely consumed by humans. But I'm a contract programmer for .Net/WPF and Android. In both environments I'm up to my elbows every day in deep XML because that's their UI formatting language. Microsoft has a tool called Expression Blend to allow WYSIWYG design of XAML but it's a very expensive tool so I've never seen it in use in any shop I've worked. Visual Studio's own visual designer requires extensive tweaking of the resulting XAML. Android/Eclipse pretty much requires hand-coding. – user316117 Mar 15 '13 at 18:26
  • 2
    @GalacticCowboy "XML documents should be human-legible and reasonably clear." from http://www.w3.org/TR/REC-xml/#sec-origin-goals - point 6. – low_rents Jan 14 '15 at 10:15
4

Well, I would suggest looking at

http://www.xfront.com/BestPracticesHomepage.html or the results of any google search under XML best practices.

I would say the standards include linking to an XSD file, proper escape characters ; etc etc

ist_lion
  • 3,149
  • 9
  • 43
  • 73
3

There is one reference formatting, XML Canonical, which is implemented in tools like xmllint (option --c14n). These tools can therefore be used as pretty-printers.

But of course you are not forced to use it. Like any formatting rules, it is a matter of taste. Just be consistent.

bortzmeyer
  • 34,164
  • 12
  • 67
  • 91
  • Actually this is kind of the formatting standard. IMHO this answers the question. – Boldewyn Jul 05 '09 at 18:04
  • 1
    This will not standardize indentation though. xml-c14n is schema-agnostic and therefore preserves most whitespace (such as indentation) when converting a document to its canonical form. Without knowing the schema, it is not possible to know which whitespace is semantically important. What looks like indentation whitespace could be semantically important content in [mixed content](http://www.w3.org/TR/REC-xml/#sec-mixed-content) elements. – Wim Coenen Nov 19 '15 at 15:14
2

http://en.wikipedia.org/wiki/XML

Look under "Well-formedness"

There aren't many standards since it is meant to be very flexible.

openshac
  • 4,966
  • 5
  • 46
  • 77
Sam
  • 7,543
  • 7
  • 48
  • 62
  • Specifically, the posted XML is not well-formed. The ones you'll be interested in are: there can only be one root-level element, and every tag that is not self-closing (does not end with /> ) must have a closing tag. – GalacticCowboy Jul 01 '09 at 21:09
1

as a general rule of thumb: Put data in separate elements and metadata in attributes.

tache
  • 11
  • 1
0

I suggest you to take a look at XMLPatterns. You won't find "coding style" suggestions, but you will find interesting design patterns (a la GangOfFour) for XML document structure.

For "coding style", I would check examples and try to imitate them. When in doubt, try different strategies and find the one that is clear and appealing. My opinion about your example is positive. tag/attribute names can however be an issue: lowercase no spaced, lowercase underscore spaced, camel case, capitalized camel case?

Stefano Borini
  • 138,652
  • 96
  • 297
  • 431