Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 937
Nigel Alderton Avatar asked Jun 20 '13 23:06

Nigel Alderton


2 Answers

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.

like image 169
C. M. Sperberg-McQueen Avatar answered Oct 20 '22 21:10

C. M. Sperberg-McQueen


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>
like image 38
hek2mgl Avatar answered Oct 20 '22 21:10

hek2mgl