tags:

views:

130

answers:

2

In the xml spec, it says:

[Definition: An element type has mixed content when elements of that type may contain character data, optionally interspersed with child elements.] In this case, the types of the child elements may be constrained, but not their order or their number of occurrences

Is there any way to work around this to make this xml valid:

<parent>
   A text node
   <child/>
</parent>

but this xml invalid:

<parent>
   A text node
   <child/>
   <child/>
</parent>

Also, do you know any reason for why this is not allowed?

<!ELEMENT parent (#PCDATA,child)>
A: 

Alderath,

Up front, I don't know, but I don't think so, not in a DTD... and, if I may say so, you really should be using XML Schema's (i.e. XSD's) instead of DTD's (they're just so 90's;-).

XSD's allow you much tighter control over document content, and I'm pretty sure that restricting the number of occurrences of a particular child element in a "mixed type" complex element is possible (even easy) using an XSD; though I've never had need (touch wood) to do so myself... so treat this as heresay.

Good luck with that.

Cheers. Keith.

corlettk
I am not in the position to decide which schema language that is to be used. I am processing xml files whose grammar is defined by a DTD over which I have no control. The reason for why I asked this was just that I came across this issue while temporarily modifying a local copy of the dtd for the purpose of testing various cases on my xml processing code.
Alderath
A: 

DEFINE: (a,b,c) is the way to express sequenced list of ALLOWED CHILD ELEMENTS. You are not supposed to sequence text-content and some elements.

SOLVE: If you want that text to be at any exact place among some child elements, wrap it in a new specific child element and sequence that specific element among other child elements.

REMEMBER: A = ANY; Allowing A text-content (= PCDATA = non-tagged string from DTD point of view) is necessarily allowing ANY text-content.

I believe i am right. . cheers

donovan