How to define DTD without strict element order?

我的未来我决定 提交于 2019-12-29 06:48:30

问题


As an XML "noob" I have discovered the importance of element order when creating an XML stream/file that is validated against a DTD. Is it possible to define a DTD that is not order dependent on elements ? If, so please provide syntactic example.


回答1:


You use or (a vertical pipe) and repeat (an asterisk:)

<!ELEMENT eltype1 ( eltype2 | eltype3)*>

This means eltype1 can contain any number of repetitions of eltype2 or eltype3.




回答2:


The only issue with the currently accepted answer is that it doesn't force only one of each element in any order. For example, you could have 2 eltype2 elements and no eltype3 elements.

If you need to be sure that both elements are present and that each occurs only one time, this is a more precise element declaration:

<!ELEMENT eltype1 ((eltype2, eltype3)|(eltype3, eltype2))>

Example in an internal subset:

<!DOCTYPE eltype1 [
<!ELEMENT eltype1 ((eltype2, eltype3)|(eltype3, eltype2))>
<!ELEMENT eltype2 (#PCDATA)>
<!ELEMENT eltype3 (#PCDATA)>
]>
<eltype1>
  <eltype3>element three</eltype3>
  <eltype2>element two</eltype2>
</eltype1>


来源:https://stackoverflow.com/questions/5643738/how-to-define-dtd-without-strict-element-order

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!