Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xml

markup

nodes

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?

like image 321
Phrytes Avatar asked Nov 16 '25 04:11

Phrytes


2 Answers

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.

like image 137
kjhughes Avatar answered Nov 17 '25 20:11

kjhughes


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.

like image 22
Michael Kay Avatar answered Nov 17 '25 22:11

Michael Kay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!