XML
Beginner
1 min read
External DTDs and Entity Declarations
Example
<!-- bookstore.dtd — external DTD file -->
<!-- (this content would live in bookstore.dtd) -->
<!--
<!ELEMENT bookstore (book+)>
<!ELEMENT book (title, author, year, isbn)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT year (#PCDATA)>
<!ELEMENT isbn (#PCDATA)>
<!ATTLIST book id ID #REQUIRED lang NMTOKEN "en">
<!ENTITY publisher "Acme Press">
<!ENTITY addr "123 Book Lane, Reading, RG1 1AA">
-->
<!-- ——— XML instance file referencing external DTD ——— -->
<?xml version="1.0" encoding="UTF-8"?>
<!-- PUBLIC identifier + SYSTEM fallback -->
<!DOCTYPE bookstore
PUBLIC "-//AcmePress//DTD Bookstore 1.0//EN"
"http://example.com/dtd/bookstore.dtd">
<!--
Inline internal subset that adds/overrides entities
and can coexist with external DTD
-->
<!DOCTYPE bookstore SYSTEM "bookstore.dtd" [
<!ENTITY publisher "Acme Press">
<!ENTITY addr "123 Book Lane">
<!-- Parameter entity: reuse in content models -->
<!ENTITY % textContent "(#PCDATA)">
<!ELEMENT note %textContent;>
]>
<bookstore>
<book id="b001" lang="en">
<title>XML Essentials</title>
<author>Jane Author</author>
<year>2024</year>
<isbn>978-0-000-00000-0</isbn>
</book>
<!-- Using the general entity -->
<note>Published by &publisher; at &addr;.</note>
</bookstore>