2012-07-11 89 views
1

我有一個XML文檔是這樣的:可選元素DTD

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE school SYSTEM ""> 
<school> 
    <data> 
     <id> 
      <uid>1</uid> 
     </id> 
     <information> 
      <name>Michael</name> 
      <surename>Julius</surename> 
     </information> 
     <note> 
      <test>hans</test> 

     </note> 
    </data> 
</school> 

和DTD文件

<!ELEMENT school (data)> 
<!ELEMENT data (id,information,note)> 
<!ELEMENT id (uid)> 
<!ELEMENT uid (#PCDATA)> 
<!ELEMENT information (name,surename?)> 
<!ELEMENT name (#PCDATA)> 
<!ELEMENT surename (#PCDATA)> 
<!ELEMENT note (#PCDATA)> <--- unknown element type 

我想與可選元素類型定義<note>元素像

<note> 
    <test2>test2</test2> 
</note> 

<note> 
    <unknown name></unknown name> 
</note> 

有幫助嗎? thankz

回答

1

您可以在note的元素聲明中使用ANY。這將允許任何元素成爲note的子元素,但該元素也必須被定義(具有元素聲明)。你不能有一個未定義的元素。

note聲明:

<!ELEMENT note ANY> 

實施例實例(DTD中可以是外部的,但我用一個內部子集,因爲它是更容易測試):

<!DOCTYPE school [ 
<!ELEMENT school (data)> 
<!ELEMENT data (id,information,note)> 
<!ELEMENT id (uid)> 
<!ELEMENT uid (#PCDATA)> 
<!ELEMENT information (name,surename?)> 
<!ELEMENT name (#PCDATA)> 
<!ELEMENT surename (#PCDATA)> 
<!ELEMENT note ANY> 
<!ELEMENT test2 (#PCDATA)><!--The element "test2" still has to be declared.--> 
]> 
<school> 
    <data> 
     <id> 
      <uid>1</uid> 
     </id> 
     <information> 
      <name>Michael</name> 
      <surename>Julius</surename> 
     </information> 
     <note> 
      <test2>hans</test2>   
     </note> 
    </data> 
</school>