XML仅允许使用DTD的元素中的某些输入

时间:2012-04-30 05:57:24

标签: xml dtd

好吧,我试图在类别元素中只允许以下输入(电子产品,杂货,家庭),但是我无法弄清楚这样做的正确方法。我正在使用内部DTD,并有两个完整的示例,说明我希望它如何处理文档。这是代码:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE manufacturer[
    <!ELEMENT manufacturer (companyName, address, phone, contactPerson?, products)>
    <!ATTLIST manufacturer identifier CDATA #REQUIRED>
    <!ELEMENT companyName (#PCDATA)>
    <!ELEMENT address (#PCDATA)>
    <!ELEMENT phone (#PCDATA)>
    <!ELEMENT contactPerson (#PCDATA)>
    <!ELEMENT products (product+)>
    <!ELEMENT product (model, price, description, category)>
    <!ATTLIST product code CDATA #REQUIRED>
    <!ELEMENT model (#PCDATA)>
    <!ELEMENT price (#PCDATA)>
    <!ELEMENT description (#PCDATA)>
    <!ELEMENT category (household|groceries|electronics)
    ]>

<manufacturer identifier = "m1">
    <companyName>Kitchen Manufacturers International</companyName>
    <address>1256 Factory Lane, Cleveland, OH 44111</address>
    <phone>216-333-4444</phone>
    <contactPerson>Fred Smith</contactPerson>
    <products>
        <product code = "X345">
            <model>Sun Beam Mixer</model>
            <price>14.75</price>
            <description>Popular handheld mixer</description>
            <category>household</category>
        </product>
        <product code = "MW30">
            <model>GE30 Microwave</model>
            <price>99.95</price>
            <description>A popular model</description>
            <category>household</category>
        </product>
    </products>
</manufacturer>

<manufacturer identifier = "m3">
    <companyName>Kraft Foods Incorporated</companyName>
    <address>1515 Kraft Avenue, Chicago, IL 37897</address>
    <phone>222-333-4444</phone>
    <products>
        <product code = "345DR">
            <model>Jiffy Peanut Butter</model>
            <price>4.95</price>
            <description>America’s favorite, made from real peanuts</description>
            <category>groceries</category>
        </product>
        <product code = "321SD">
            <model>Mac N' Cheese</model>
            <price>1.99</price>
            <description>Every child’s favorite dinner</description>
            <category>groceries</category>
        </product>
        <product code = "123DD">
            <model>Rice Krispies</model>
            <price>2.99</price>
            <description>Snap Crackle and Pop</description>
            <category>groceries</category>
        </product>
    </products>
</manufacturer>

1 个答案:

答案 0 :(得分:1)

您不能使用DTD指定对XML元素内容的限制。您指定的是category下唯一的子元素可以是household ......等等。