Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying attribute values as CDATA

Tags:

validation

xml

Can one specify XML attribute values as CDATA ? If yes - what would be the schema for the same. If not - why is this limitation not addressed in XML ?

like image 215
gnlogic Avatar asked Dec 11 '08 13:12

gnlogic


People also ask

Can we use CDATA in XML attribute?

In a Document Type Definition (DTD) an attributes type can be set to be CDATA. The resulting attribute within an XML document may contain arbitrary character data. So basically the DTD CDATA type is a string type with no restrictions placed on it, it can contain any textual data (as long as its suitably escaped).

What CDATA attribute is consist of?

CDATA-type attribute value In Document Type Definition (DTD) files for SGML and XML, an attribute value may be designated as being of type CDATA: arbitrary character data. Within a CDATA-type attribute, character and entity reference markup is allowed and will be processed when the document is read.

How do I add CDATA to XML?

CDATA sections can appear inside element content and allow < and & character literals to appear. A CDATA section begins with the character sequence <! [CDATA[ and ends with the character sequence ]]>. Between the two character sequences, an XML processor ignores all markup characters such as <, >, and &.

How can we declare attributes of an element in DTD?

In a DTD, attributes are declared with an ATTLIST declaration.


2 Answers

No you cannot do this.

There's a very fine line (and a very large debate) between what constitutes an attribute and what constitutes a child element. See here for example.

That given, the "limitation" isn't addressed in XML because it doesn't exist. You always have the ability to put this data in a child element, and in fact I would go so far as to say that if you even have to think about this, an element is the correct structure.

Edit: More reading material

  • The Attribute/Text Conundrum
  • Principles of XML design: When to use elements versus attributesl
  • w3schools take
like image 121
annakata Avatar answered Oct 19 '22 11:10

annakata


The way you handle that is to use a child element rather than an attribute.

like image 37
Joel Coehoorn Avatar answered Oct 19 '22 09:10

Joel Coehoorn