代码之家  ›  专栏  ›  技术社区  ›  Ross

将doctype与XML一起使用

  •  3
  • Ross  · 技术社区  · 17 年前

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE name SYSTEM "names.dtd">
    <names>
        <name>
            <text>Pep&eacute;</text>
            <creator>&lost;</creator>
            <history>&lost;</history>
        </name>
        <name>
            <text>Charles</text>
            <creator>James</creator>
            <history>&lost;</history>
        </name>
    </names>
    

    names.dtd

    <!ELEMENT name (text, creator+, history)>
    <!ELEMENT text (#PCDATA)>
    <!ELEMENT creator (#PCDATA)>
    <!ELEMENT history (#PCDATA)>
    
    <!-- Placeholder/unknown history or creator name -->
    <!ENTITY lost "Lost in the depths of time.">
    <!ENTITY eacute "é">
    

    但是,在尝试访问names.xml时,我遇到以下错误:

    XML分析错误:未定义的实体 http://localhost/.../names.xml 线 第5号,第18栏:

    <text>Pep&eacute;</text>
    ---------^
    

    http://localhost/.../names.dtd 也不行。

    这似乎在放置 <!ENTITY <!DOCTYPE 在里面 names.xml 然而有人能对此提出建议吗?

    2 回复  |  直到 17 年前
        1
  •  2
  •   Justin Niessner    17 年前

    如果您要在Firefox中打开文档,试图确定dtd是否正确,请不要这样做。Firefox没有通过适当的xml解析器传递xml和dtd。在IE中打开xml文档,这将使您的文档通过MSXML解析器。

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE NAME SYSTEM "names.dtd">
    <names>
        <name>
            <text>Pep&eacute;</text>
            <creator>&lost;</creator>
            <history>&lost;</history>
        </name>
        <name>
            <text>Charles</text>
            <creator>James</creator>
            <history>&lost;</history>
       </name>
    </names>
    

    <!ELEMENT name (text, creator+, history)>
    <!ELEMENT text (#PCDATA)>
    <!ELEMENT creator (#PCDATA)>
    <!ELEMENT history (#PCDATA)>
    
    <!ENTITY lost "Lost in the depths of time.">
    <!ENTITY eacute "&#233;">
    
        2
  •  2
  •   Brian Campbell Dennis Williamson    17 年前

    火狐 does not load external DTDs (野生动物园也不例外;它 looks like

    $ xmllint --loaddtd names.xml 
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE names SYSTEM "names.dtd">
    <names>
        <name>
            <text>Pep&eacute;</text>
            <creator>&lost;</creator>
            <history>&lost;</history>
        </name>
        <name>
            <text>Charles</text>
            <creator>James</creator>
            <history>&lost;</history>
        </name>
    </names>
    

    编辑 bad idea RELAX NG 建议用于此目的),而不是嵌入在文档本身中的DTD。

    推荐文章