Are there coding standards for XML files?

感情迁移 提交于 2019-12-10 12:46:02

问题


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?


回答1:


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.




回答2:


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




回答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.




回答4:


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

Look under "Well-formedness"

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




回答5:


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




回答6:


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?



来源:https://stackoverflow.com/questions/1071237/are-there-coding-standards-for-xml-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!