自定义实体分为:一般实体和参数实体
1、自定义一般实体
demo8中的代码:
<?xml version="1.0" encoding="UTF-8"?> <!ELEMENT book (detail+)> <!ELEMENT detail (name,author,publisher,price,url)> <!ELEMENT name (#PCDATA)> <!ELEMENT author (#PCDATA)> <!ELEMENT publisher (#PCDATA)> <!ELEMENT price (#PCDATA)> <!ELEMENT url (#PCDATA)> <!ENTITY bj "北京大学出版社">
对应的xml代码:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE book SYSTEM "demo8.dtd"> <book> <detail> <name>XML</name> <author>李白</author> <publisher>&bj;</publisher> <price>22.5</price> <url> <![CDATA[ <a href="#">链接1</a> ]]> </url> </detail> <detail> <name>PHP</name> <author>杜甫</author> <publisher>&bj;</publisher> <price>22.5</price> <url> <![CDATA[ <a href="#">链接2</a> ]]> </url> </detail> </book>
xml内部定义一般实体:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE book SYSTEM "demo8.dtd"[ <!ENTITY bj "北京大学出版社"> ]> <book> <detail> <name>XML</name> <author>李白</author> <publisher>&bj;</publisher> <price>22.5</price> <url> <![CDATA[ <a href="#">链接1</a> ]]> </url> </detail> <detail> <name>PHP</name> <author>杜甫</author> <publisher>&bj;</publisher> <price>22.5</price> <url> <![CDATA[ <a href="#">链接2</a> ]]> </url> </detail> </book>
2、自定义参数实体
参数实体就是在普通实体名字前面加上“%”。
dtd中的代码:
<?xml version="1.0" encoding="UTF-8"?> <!ENTITY % p "test"> <!-- 先声明后使用 --> <!ELEMENT roster ((%p;)+)> <!ELEMENT %p; (name,sex,birthday,score?,skill+)> <!ELEMENT name (#PCDATA)> <!ELEMENT sex (#PCDATA)> <!ELEMENT birthday (#PCDATA)> <!ELEMENT score (#PCDATA)> <!ELEMENT skill (#PCDATA)> <!ATTLIST %p; id ID #required>
对应的xml代码:
demo1.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE roster SYSTEM "demo11.dtd"[ <!ENTITY % p "teacher"> <!-- 给参数赋值 --> ]> <roster> <teacher id="s"> <name></name> <sex></sex> <birthday></birthday> <skill></skill> </teacher> </roster>
demo2.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE roster SYSTEM "demo11.dtd"[ <!ENTITY % p "student"> <!-- 给参数赋值 --> ]> <roster> <student id="s"> <name></name> <sex></sex> <birthday></birthday> <skill></skill> </student> </roster>原文链接:https://www.f2er.com/xml/293993.html