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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With