XML实体 - 我的代码出了什么问题?

时间:2016-03-01 23:34:02

标签: xml entity dtd

我正在上XML课程,我必须阅读这本名为“INeasysteps”的书。我正在尝试按照其中一个关于“添加注释和实体”的教程,但当我将它放入浏览器时,我会不断收到回复:

  

Blockquote XML解析错误:未定义的实体   第11行,第13栏:   块引用

这是我的代码:请让我知道我做错了什么。仅供参考,Jedit解析器中没有错误,但是当我将xml代码放入浏览器时,我得到上述错误:

var build = {
    execute: function (type, data) {
        switch (type) {

            case  "fullName":
                return build.fullName(data);
        }
    },
    fullName: function(data){
        return data[0] + data[1]
    }
};

// assume this is the actual inbound data after parsing
var sample_event = {
    customer: {
        firstName: "bob",
        lastName: "smith",
        email: "a@example.com"
    }
};


function cleanStuff( event) {

    // potential inbound data

    var prospects = [
        {parent: "customer", type: build.fullName, newField: 'fullName',data: [event.customer.firstName, event.customer.middleName, event.customer.lastName]},
        {parent: "customer", type: build.fullName, newField:'fullName', data: [event.idontexist.firstName, event.seller.middleName, event.seller.lastName]}
    ];

    function calc(type, newField, calculationData) {
        var calculated = calculated || {};
        return calculated[newField] = build.execute(type, calculationData);
    }

    var filteredList = prospects.filter(function (x) {
        if (event[x.parent] !== undefined) {
            return x
        }
    });

    filteredList.forEach(function (item) {
        var type = item.type,
            fieldName = item.fieldName,
            data = item.data;

        calc(type, fieldName, data, event);

    });
}

cleanStuff(sample_event);

这是history.dtd文件

<?xml version="1.0" encoding="UTF-8" ?>

<!-- XML in easy steps - Page 40. -->

<?xml-stylesheet 
type = "text/css" href = "history.css" ?>

<!DOCTYPE doc SYSTEM "history.dtd" >

<doc>
<para>Both &html; and &xml; are derived from &sgml; which is, in turn, a descendant of the &gml; that was developed in the 1960s by IBM. </para>
</doc>

1 个答案:

答案 0 :(得分:1)

大多数浏览器都不会加载外部实体。如果要在浏览器中打开XML文件并正确解析实体,请将实体声明添加到内部子集(doctype声明中的[]之间):

<?xml version="1.0" encoding="UTF-8" ?>
<!-- XML in easy steps - Page 40. -->
<?xml-stylesheet type="text/css" href = "history.css" ?>
<!DOCTYPE doc SYSTEM "history.dtd" [
<!ENTITY html "HyperText Markup Language (HTML)" >
<!ENTITY xml "eXtensible Markup Language (XML)" >
<!ENTITY sgml "Standard Generalized Markup Language (SGML)" >
<!ENTITY gml "Generalized Markup Language (GML)" >
]>
<doc>
    <para>Both &html; and &xml; are derived from &sgml; which is, in turn, a 
        descendant of the &gml; that was developed in the 1960s by IBM. </para>
</doc>