Is it wrong in XML to have element nodes as siblings of text nodes?

痞子三分冷 提交于 2019-12-01 22:32:35

问题


Is the following "allowed" or simply bad use of XML? I cannot find restrictions on this, but it feels a bit weird.

<example>
    You can choose from
    <option>this</option>
    <option>that</option>
    <option>something else</option>
    Choose wisely.
</example>

If it is wrong, what is a nice alternative?


回答1:


No, mixing elements and text is completely normal in XML.

You may be more familiar with data-oriented uses of XML, but XML actually evolved from a basis in representing documents, where marking up text within other text is the norm. Its predecessor, SGML, was like this, and HTML is still like this today.

In XML Schema (XSD), mixed="true" designates mixed text in a content model.




回答2:


I think it's best to reserve mixed content for one purpose only: to annotate (or "mark-up") sections of natural language text. So a good test to apply is, does the text still make sense if the markup is removed?

In your example, my only criticism would be that if you remove the markup, what's left isn't grammatical: it's missing a full-stop after the first sentence.




回答3:


In addition to the answers above (with which I agree) I would add that it depends on the purpose of the XML document. If the purpose is to communicate business data between applications then validation of the entire document may be a requirement. Mixed content cannot be strictly validated using XML Schema, and is usually avoided in business-to-business communications unless it is for documentation purposes only.



来源:https://stackoverflow.com/questions/36116608/is-it-wrong-in-xml-to-have-element-nodes-as-siblings-of-text-nodes

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