可选元素类型DTD

时间:2012-07-11 09:40:49

标签: xml dtd xml-validation dtd-parsing

我有这样的XML文档:

<?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

我想用{/ p>等可选元素类型定义<note>元素

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

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

任何帮助? thankz

1 个答案:

答案 0 :(得分:2)

您可以在ANY的元素声明中使用note。这将允许任何元素成为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>