文档类型声明和名称空间 [英] Document type declaration and namespaces

查看:68
本文介绍了文档类型声明和名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我是XML的新手,目前正在学习文档类型声明。无论如何,在DTD主体中声明元素时,元素名称必须与XML文档中的名称完全一样,包括任何名称空间前缀,这意味着用户无法定义自己的名称空间,但必须使用DTD中定义的前缀。

I’m new to XML and am currently learning about Document Type Declaration. Anyways, when declaring elements in the body of DTD, the element name must appear exactly as it will within the XML document, including any namespace prefix, which means users can’t define their own namespace, but must use the prefix defined within DTD.

a)我假设即使必须使用DTD中定义的前缀,我们仍然能够选择这些前缀指向的URI?

a) I assume that even though we must use prefixes defined within DTD, we are still able to choose to which URIs these prefixes point to?


b)假设我们在DTD中声明了一个元素,其中pers是一个命名空间前缀,是否意味着在XML文档中此元素的所有出现都需要包含前缀 pers?如果真是这样,那就意味着使用DTD不能使用默认的名称空间功能?!

b) Assuming we declare ( in DTD ) an element , where pers is a namespace prefix, does that mean all occurrences of this element within XML document will need to include a prefix "pers"? If that is the case, then that would mean that with DTDs we can’t use a default namespace feature?!


谢谢


编辑:


a)


确实,除非有特别好的理由或您对语法的要求很简单并且不需要名称空间,否则应考虑使用XML模式。

Really, unless there is a particularly good reason to or you have simple syntactical requirements and no need for namespaces, you should consider using XML Schemas instead.

我确实打算使用XML Schema,但是我也想学习DTD的基础。

I do plan to use XML Schema instead, but I’d also like to learn the basics of DTDs.


b)


...没有名称空间URI的概念(也没有默认名称空间)。

…there is no notion of namespace URIs (nor a default namespace).

如果我们在DTD中声明名为 xmlns的属性,则:

If we declare attribute named "xmlns" within DTD:

<!ATTLIST contact xmlns CDATA #REQUIRED> 


然后是XML文档可以使用默认名称空间功能(此处子元素< name> 在默认名称空间中):

then XML document could use the default namespace feature( here child element <name> is in the default namespace):

       ...
<contact xmlns="www.somewhere.com" … > 
   <name></name>
</contact>       
       ...


thanx

推荐答案

DTD完全没有名称空间的概念。

DTDs have no notion of namespaces whatsoever. The namespace concept was introduced after their conception.

对于XML命名空间,重要的是命名空间URI,而不是前缀。另一方面,前缀可以由用户自由更改。

For XML namespaces, the important part is the namespace URI and not the prefix; the prefix may be altered freely by the user.

另一方面,当在DTD中给定名称空间前缀时,前缀部分仅被视为元素的一部分。名称(因为DTD没有名称空间概念)。因此,不能更改前缀,也没有名称空间URI的概念(也没有默认名称空间)。

When given namespace "prefixes" in a DTD, on the other hand, the prefix part is simply considered part of the element name (since DTD has no namespace concept). Therefore, the "prefix" canNOT be altered and there is no notion of namespace URIs (nor a default namespace).

真的,除非有特别好的理由要如果您对语法有简单的要求,并且不需要名称空间,则应该考虑使用XML模式。

Really, unless there is a particularly good reason to or you have simple syntactical requirements and no need for namespaces, you should consider using XML Schemas instead.

完整的模式规范可能令人望而生畏,但我发现有人学到了一定的知识。足够复杂的子集。 W3Schools 的人对基础知识有很好的入门。

The full Schema spec can be daunting, but I find that one learns a certain adequate subset that is not that complicated. The folks at W3Schools have a good primer for the basics.

这篇关于文档类型声明和名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆