问题
I need to validate xml files against a DTD.
Sometimes the child elements can be more and I dont know what child elements will come up next time, I tried the following:
<!ELEMENT Story (StoryPara+ , ANY+)+>
but this didn't work, how to validate for any child elements using DTD?
回答1:
You can't mix "ANY" with anything else in your content model. (You would use it just like you would use "EMPTY".)
What your current element declaration is saying is: at least one occurrence of: one or more "StoryPara" element(s) followed by one or more "ANY" element(s)
Change your element declaration to this:
<!ELEMENT Story ANY>
Do note that any child of "Story" must also be defined.
For example, this is valid:
<?xml version="1.0"?>
<!DOCTYPE test [
<!ELEMENT test ANY>
<!ELEMENT foo (#PCDATA)>
]>
<test>
<foo>This element is defined.</foo>
</test>
but this is not because "bar" is not defined:
<?xml version="1.0"?>
<!DOCTYPE test [
<!ELEMENT test ANY>
<!ELEMENT foo (#PCDATA)>
]>
<test>
<foo>This element is defined.</foo>
<bar>This element is not defined.</bar>
</test>
来源:https://stackoverflow.com/questions/6489366/how-to-add-dtd-element-if-i-dont-know-the-child-elements