针对DTD的XML验证 [英] XML validation against DTD
问题描述
我具有以下XML和嵌入式DTD:
I have this XML along with an embedded DTD:
<?xml version="1.0" ?>
<!DOCTYPE customers [
<!ELEMENT customers (name,age,roll,sex)>
<!ELEMENT name (#CDATA)>
<!ELEMENT age (#CDATA)>
<!ELEMENT roll (#CDATA)>
<!ELEMENT sex (#CDATA)>
]>
<customers>
<name>XYZ</name>
<age>19</age>
<roll>23</roll>
<sex>M</sex>
</customers>
当我尝试验证XML时,它向我显示了一个错误。但是,如果我将#CDATA更改为#PCDATA,则验证成功。
When i try to validate the XML, its showing me an error. But if i change the #CDATA to #PCDATA then the validation is successful.
问题1)我没有适当的解释,为什么会这样。两者之间的唯一区别是:#CDATA将不进行解析,而#PCDATA将进行解析。在这种情况下。两次验证都应该成功吧?请说明我错在哪里,因为此验证的输出与我的概念背道而驰。
Question 1) I don't have a proper explanation why is this happening. The only difference between the two is: #CDATA will not parse whereas #PCDATA will parse. In such a case. both the validations should succeed right? Please explain where am i wrong since the output of this validation is going against my concept.
问候,
推荐答案
在XML DTD中没有像 #CDATA
这样的东西。这是一个未知的关键字。这就是为什么会出现错误。
There is no such thing as #CDATA
available for use in XML DTDs. It is an unknown keyword. That's why you get an error.
CDATA
是在声明属性时使用的关键字在DTD中。您不能声明元素的类型为 CDATA
(或 #CDATA
)。
CDATA
is a keyword that is used when declaring attributes in a DTD. You cannot declare an element to be of type CDATA
(or #CDATA
).
在CDATA部分中也找到了字符串 CDATA
(<![CDATA [ ...]]>
),这是完全不同的东西。可以在XML文档中使用它们来转义字符(如&
),否则这些字符将被解释为标记。在DTD中未声明CDATA节;
The string CDATA
is also found in CDATA sections (<![CDATA[ ... ]]>
), which are entirely different things. They can be used in an XML document to escape characters (such as &
) that would otherwise be interpreted as markup. CDATA sections are not declared in the DTD; they are simply used when needed.
如果您有标记,例如< name> L& T< / name>
(未包含在CDATA部分中),那么它将被解析器拒绝。在DTD中声明 name
元素并不重要。
If you have markup such as <name>L&T</name>
(that is not enclosed in a CDATA section), then it will be rejected by the parser. It does not matter how the name
element is declared in the DTD.
这篇关于针对DTD的XML验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!