Rudolf Lamprecht
Rudolf Lamprecht

Reputation: 1070

Is it legal in XML to mix text and tags?

Is the following an acceptable XML structure?

<Root>
    <Child ID="1" Name="test">some inner text
        <SecondChild ID="1" Name="test1">some text</SecondChild>
        <SecondChild ID="2" Name="test2">some text 2</SecondChild>
    </Child>
</Root>

I want to insert innertext() as well as child nodes to the <Child/> node. Is this legal within XML?

Upvotes: 11

Views: 3417

Answers (2)

Alexandre
Alexandre

Reputation: 134

It is legal, but not recommended, because:

  • you break the semantic of the objects that are represented by the XML content
  • you cannot defined a XSD in order to validate your XML files
  • the code to parse the elements containing both text and tags is more complex to write and test (you have to handle the inner space and newline characters)

What can be done:

  • if you get to this situation because you need to add text in your element, embed the text part into a new child of the element or as an attribute to the tag
  • if you get to this situation because you need to add new information to your element, you may either :

    • move the text part of your element to a child of this element, and add the new items as new children

    • add the new items as attributes to the existing tag

Upvotes: 1

Alex
Alex

Reputation: 4669

Normally, you don't have to insert some raw text in addition of childs elements in your tag ...

What are you trying to do please ?

anyway you can verify it here : http://www.w3schools.com/xml/xml_validator.asp

There is apparently no errors in your document so far.

Upvotes: 2

Related Questions