Using a DTD, can an element be declared that allows any XML content?
Asked Answered
H

2

7

I'm trying to create a DTD for an XML document. The document has two children - one contains structured data and the other unstructured data eg;

<doc>
  <structured>
    <foo x="9"/>
    <foo x="4"/>
  </structured>
  <unstructured>
    <hello/>
    <world x="2">
      <hi msg="something"/>
      <anything/>
    </world>
  </unstructured>
</doc>

I want to create a DTD for the above XML that allows the <unstructured> element to contain any valid XML. I tried this DTD;

<!ELEMENT doc (structured,unstructured)
<!ELEMENT structured (foo*)
<!ELEMENT foo EMPTY>
<!ATTLIST foo x CDATA #REQUIRED>
<!ELEMENT unstructured ANY>

But errors are generated like this;

No declaration for element hello
No declaration for element world

..etc

I want to allow <unstructured> to contain any valid XML. Is there a way in a DTD to allow a specified element to contain any parsable XML?

I'm using PHP 5.3 DOMDocument::validate.

Helvetic answered 20/6, 2013 at 23:58 Comment(0)
C
6

No, there is not.

You came as close as a DTD can come, by using the ANY keyword. But ANY matches a mixture of #PCDATA and every element declared in the DTD. It doesn't accept undeclared elements; DTDs don't have much notion of partial validity.

That was one of the motivating use cases for the introduction of wildcards in XSD with options to ask for strict, lax, or skip processing of the matching elements.

Cataplexy answered 21/6, 2013 at 0:16 Comment(0)
C
1

Just well formed isn't valid that's the simple answer. If you want to validate a document using DTD's you'll have to declare every element (unlike xml schema).. The working example would look like this:

<?xml version="1.0"?>
<!DOCTYPE doc [
<!ELEMENT doc (structured,unstructured)>
<!ELEMENT structured (foo*)>
<!ELEMENT hello EMPTY>
<!ELEMENT world (hi, anything)>
<!ATTLIST world x CDATA #REQUIRED>
<!ELEMENT hi EMPTY>
<!ATTLIST hi msg CDATA #REQUIRED>
<!ELEMENT anything EMPTY>
<!ELEMENT foo EMPTY>
<!ATTLIST foo x CDATA #REQUIRED>
<!ELEMENT unstructured ANY>
]>
<doc>
  <structured>
    <foo x="9"/>
    <foo x="4"/>
  </structured>
  <unstructured>
    <hello/>
    <world x="2">
      <hi msg="something"/>
      <anything/>
    </world>
  </unstructured>
</doc>
Catechin answered 21/6, 2013 at 0:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.