Using Both Internal and External DTDs

In fact, you can use both internal and external DTDs at the same time, using these forms of the <!DOCTYPE> element: <!DOCTYPE rootname SYSTEM URL [DTD]> for private external DTDs and <!DOCTYPE rootname PUBLIC FPI URL [DTD]> for public external DTDs. In this case, the external DTD is specified by URL and the internal one by DTD.

Here's an example where I've removed the <PRODUCT> element from the external DTD order.dtd:

<!ELEMENT DOCUMENT (CUSTOMER)*>
<!ELEMENT CUSTOMER (NAME,DATE,ORDERS)>
<!ELEMENT NAME (LAST_NAME,FIRST_NAME)>
<!ELEMENT LAST_NAME (#PCDATA)>
<!ELEMENT FIRST_NAME (#PCDATA)>
<!ELEMENT DATE (#PCDATA)>
<!ELEMENT ORDERS (ITEM)*>
<!ELEMENT ITEM (PRODUCT,NUMBER,PRICE)>
<!ELEMENT NUMBER (#PCDATA)>
<!ELEMENT PRICE (#PCDATA)>

Now I'll specify that I want to use this external DTD in the document's <!DOCTYPE> element—but also add square brackets, [ and ], to enclose an internal DTD as well:

<?xml version = "1.0" standalone="no"?>
<!DOCTYPE DOCUMENT SYSTEM "order.dtd" [
    .
    .
    .
]>
<DOCUMENT>
    <CUSTOMER>
        <NAME>
            <LAST_NAME>Smith</LAST_NAME>
            <FIRST_NAME>Sam</FIRST_NAME>
        </NAME>
        <DATE>October 15, 2001</DATE>
        <ORDERS>
            <ITEM>
                <PRODUCT>
                    <PRODUCT_ID>
                        198348209
                    </PRODUCT_ID>
                </PRODUCT>
                <NUMBER>8</NUMBER>
                <PRICE>$1.25</PRICE>
            </ITEM>
            .
            .
            .
            <ITEM>
                <PRODUCT>
                    <PRODUCT_ID>
                        198348206
                    </PRODUCT_ID>
                </PRODUCT>
                <NUMBER>6</NUMBER>
                <PRICE>$11.50</PRICE>
            </ITEM>
        </ORDERS>
    </CUSTOMER>
</DOCUMENT>

Next, I add the declaration of the <PRODUCT> element to the internal part of the DTD, like this:

<?xml version = "1.0" standalone="no"?>
<!DOCTYPE DOCUMENT SYSTEM "order.dtd" [
<!ELEMENT PRODUCT (PRODUCT_ID)>
<!ELEMENT PRODUCT_ID (#PCDATA)>
]>
<DOCUMENT>
    <CUSTOMER>
        <NAME>
            <LAST_NAME>Smith</LAST_NAME>
            <FIRST_NAME>Sam</FIRST_NAME>
        </NAME>
        <DATE>October 15, 2001</DATE>
        <ORDERS>
            <ITEM>
                <PRODUCT>
                    <PRODUCT_ID>
                        198348209
                    </PRODUCT_ID>
                </PRODUCT>
                <NUMBER>8</NUMBER>
                <PRICE>$1.25</PRICE>
            </ITEM>
            .
            .
            .
            <ITEM>
                <PRODUCT>
                    <PRODUCT_ID>
                        198348206
                    </PRODUCT_ID>
                </PRODUCT>
                <NUMBER>6</NUMBER>
                <PRICE>$11.50</PRICE>
            </ITEM>
        </ORDERS>
    </CUSTOMER>
</DOCUMENT>

And that's all it takes; now this DTD uses both internal and external parts.

If It's Both Internal and External, Which Takes Precedence?

Theoretically, if an element or attribute is defined in both an internal and external DTD, the definition in the internal DTD is supposed to take precedence, overwriting the external definition. Things were arranged that way to let you customize external DTDs as you like. However, my experience is that most XML processors simply consider it an error if there is an element or attribute conflict between internal and external DTDs, and they usually just halt.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.190.182