Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it legal in XML to mix text and tags?

Tags:

xml

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?

like image 566
Rudolf Lamprecht Avatar asked Mar 10 '14 08:03

Rudolf Lamprecht


People also ask

Can we create tags in XML?

You can create tags from scratch or load them from another source, such as an InDesign document, InCopy document, or DTD file. Use any of the following methods to create or load XML tags for your document: Create a tag with the New Tag command. Load tags from an XML file or another document.

Can XML have multiple tags with same name?

The general rule for XML binding consists of using the full path and attribute name. However, this may result in multiple input objects in the JSP with the same name. Input objects with the same name are posted as an array of objects and are not posted to the API.

Are numbers allowed in XML tags?

XML elements must follow these naming rules: Names can contain letters, numbers, and other characters. Names cannot start with a number or punctuation character.

Is Dot allowed in XML?

To XML, there's no significance of a period in an element's name. a , a. , a.b , and a.b.c are all legal (and unique) element names.


2 Answers

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.

like image 57
Alex Avatar answered Sep 24 '22 05:09

Alex


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

like image 29
Alexandre Avatar answered Sep 22 '22 05:09

Alexandre