3

I'm writing a DTD for an XML structure that allows for flexible attribute names, like this:

<root_element>
    <child_element>
        <element_with_attrib iCouldbeAnything="value" defined="true" />
    </child_element>
</root_element>

So here's a simple DTD for this:

<!ELEMENT root_element (child_element)*>
<!ELEMENT child_element (element_with_attrib)+>
<!ELEMENT element_with_attrib EMPTY>
<!ATTLIST element_with_attrib
    defined CDATA #IMPLIED
    iCouldbeAnything CDATA #IMPLIED
  >

The problem is, a requirement for the XML is that "iCouldBeAnything" has to be anything. Is it possible to describe this using DTD or would I need to rely on some other kind of XML validation?

danieltalsky
  • 7,752
  • 5
  • 39
  • 60
  • Um, why a dtd and not a schema? – bmargulies Mar 24 '11 at 23:06
  • Well, a DTD is a kind of schema for XML. I assume you're talking about XSD? What are the advantages of XSD? DTD seems very widely accepted. – danieltalsky Mar 24 '11 at 23:10
  • DTDs are much less flexible then W3C XML Schema and RelaxNG, let alone schematron. For 'document'-type stuff, relaxng is becoming very popular, and for 'data'-type stuff it's W3C XML Schema. Either can express what you want easily. – bmargulies Mar 24 '11 at 23:51
  • Appreciated... but you're not able to answer my initial question? "Is it possible to do it with a DTD?" Answer that question as an real answer and I'll accept it if you can show. – danieltalsky Mar 25 '11 at 00:47
  • If I had an answer to that I would have put it in an answer. I haven't written a DTD in several years. Thus the comment traffic in case one of the other things was useful to you. – bmargulies Mar 25 '11 at 00:53

1 Answers1

2

AFAIK this cannot be done with DTD. You need to use some other schema language, for example W3C XML Schema allows flexible attribute names when <xs:anyAttribute> is used.

To be more precise: DTD attribute list declarations require a name for an attribute and there is no general name wildcard. Strangely though, the attribute definition in the attribute declaration list is optional, so theoretically you could have an anonymous and untyped attribute list declaration.

AttlistDecl  ::=  '<!ATTLIST' S Name AttDef* S? '>'
AttDef       ::=  S Name S AttType S DefaultDecl

So the specification allows attribute declarations like <!ATTLIST element-name > but it seems that the spec doesn't specify how such declaration should be interpreted, does it have any effect at all or could it allow any attribute name with any type. Whichever the case is, I don't believe such structure is well supported because of the rarity and eccentricity of that feature.

jasso
  • 13,736
  • 2
  • 36
  • 50
  • Good catch. The SGML spec from which this is derived requires the equivalent of AttDef+. In theory it should have no effect because the spec allow multiple ATTLISTs for an element, with the rule that only the first definition of any specific attribute counts (and the remaining do not constitute errors). So this ATTLIST declaration would simply contribute nothing to the list of attributes. – arayq2 Dec 31 '12 at 02:01