开发者

Can DTD's be written to allow one or more attributes of any name?

开发者 https://www.devze.com 2023-02-19 09:13 出处:网络
I\'m writing a DTD for an XML structure that allows for flexible attribute names, like this: <ro开发者_JS百科ot_element>

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

<ro开发者_JS百科ot_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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消