0

我有一个为大部分文件指定的 xml 文件。只有一个元素没有规范,可以包含任意 xml 元素。

这是此 xml 的示例:

<root_element>
  <specified_element>
     <empty_child_node/> 
  </specified_element>
  <element_with_free_form>
    <what><ever><xml><which><is><valid><xml>
    </xml></valid></is></which></xml></ever></what>
  </element_with_free_form>
</root_element>

我对 DTD 的理解是,这应该是此类 xml 的有效规范:

<?xml version="1.0" encoding="utf-8"?>
<!ELEMENT root_element (specified_element, element_with_free_form)>
<!ELEMENT specified_element (empty_child_node)>
<!ELEMENT empty_child_node EMPTY>
<!ELEMENT element_with_free_form ANY>

这是嵌入了 DTD 的完整 xml 文档:

<!DOCTYPE root_element [
<!ELEMENT root_element (specified_element, element_with_free_form)>
<!ELEMENT specified_element (empty_child_node)>
<!ELEMENT empty_child_node EMPTY>
<!ELEMENT element_with_free_form ANY>
]>
<root_element>
  <specified_element>
     <empty_child_node/> 
  </specified_element>
  <element_with_free_form>
    <what><ever><xml><which><valid><xml>
    </xml></valid></which></xml></ever></what>
  </element_with_free_form>
</root_element>

我究竟做错了什么 ?

4

2 回答 2

0

As DevNull has already said, the ANY keyword means the element being declared can contain a mixture of #PCDATA and any element declared in the DTD.

If you want to declare that element_with_free_form can contain any well-balanced XML, then DTDs are not quite the right schema language for you. XSD can declare such elements, as well as supporting some other variations like "I don't care what the structure is, but the children must all have been declared" (which is the same functionality as ANY) and "If you have a declaration for it, validate it, and if you don't have a declaration for it, let it ride" (which is a good first approximation to the "ignore what you don't understand" principle of HTML).

于 2012-08-22T01:07:07.873 回答
0

即使element_with_free_form被声明为ANY,其中的任何元素也必须被声明。这意味着您还需要声明what, ever, xml,whichvalid.

于 2012-04-11T14:13:11.867 回答