什么是QName

看代码时经常碰到QName,当时对这个东东具体什么意思也是不太明白;今天在看SOAP消息的时候,想到这个东东,就去仔细看了下。QName其实就是Qualified Name的简称,在“NameSpaces in XML1.0(Third Edition)”(http://www.w3.org/TR/REC-xml-names/#ns-qualnames)中有如下这段话:

URI references can contain characters not allowed in names, and are often inconveniently long, so expanded names are not used directly to name elements and attributes in XML documents. Instead qualified names are used. [Definition: A qualified name is a name subject to namespace interpretation. ] In documents conforming to this specification, element and attribute names appear as qualified names. Syntactically, they are either prefixed names or unprefixed names.

同时文中对QName的定义如下:

In XML documents conforming to this specification, some names (constructs corresponding to the nonterminal Name) MUST be given as qualified names,defined as follows:

Qualified Name

 

[7]    QName    ::=    PrefixedName
      UnprefixedName
[8]    PrefixedName    ::=    Prefix ':' LocalPart
[9]    UnprefixedName    ::=    LocalPart
[10]    Prefix    ::=    NCName
[11]    LocalPart    ::=    NCName

 

The Prefix provides the namespace prefix part of the qualified name, and MUST be associated with a namespace URI reference in a namespace declaration. [Definition: The LocalPart provides the local part of the qualified name.]

Note that the prefix functions only as a placeholder for a namespace name. Applications SHOULD use the namespace name, not the prefix, in constructing names whose scope extends beyond the containing document.

 

In XML documents conforming to this specification, element names are given as qualified names;

Attributes are either namespace declarations or their names are given as qualified names.

 

也就是说QName分为两种,一种是带前缀的名字,另一种是不带前缀的。若是带前缀,则前缀必须和命名空间的URI引用相关联,如

<namespace-prefix:envelope xmlns:namespace-prefix="namespaceURI"> ... </namespace-prefix:envelope>,其中<namespace-prefix:envelope>就是QName;不带前缀,也就是说xml文件使用的默认命名空间,如<rootElement xmlns="namespaceURI"> ... </rootElement>,这里<rootElement>就是QName;在xml文件中,元素和属性都是用QName表示的;

 

如果应用程序在构造QName的时候,同时这个QName的范围超过了其被包含的xml文档,比如应用程序是Java或C#程序,那么在构造QName的时候就需要使用命名空间,而不是前缀,QName可以看做是一个包含了命名空间和local part的二元组,如(namespace, local name).

posted @ 2016-11-23 10:19  余***龙  阅读(3117)  评论(0编辑  收藏  举报