了解 XPath/XML 中的 NCName 和 QName

Understanding NCName and QName in XPath/XML

什么是 NCName and QName in XML? It is mentioned quite a few times on the XPath page 的基本解释,例如:

The name function returns a string containing a QName representing the expanded-name of the node in the argument node-set that is first in document order. The QName must represent the expanded-name with respect to the namespace declarations in effect on the node whose expanded-name is being represented. Typically, this will be the QName that occurred in the XML source. This need not be the case if there are namespace declarations in effect on the node that associate multiple prefixes with the same namespace. However, an implementation may include information about the original prefix in its representation of nodes; in this case, an implementation can ensure that the returned string is always the same as the QName used in the XML source. If the argument node-set is empty or the first node has no expanded-name, an empty string is returned. If the argument it omitted, it defaults to a node-set with the context node as its only member.

但我很难理解那是什么,当点击链接时,它似乎给出了一个“syntax/grammar”的定义,但对我来说并不是 'make sense' 的真正定义(即,对我来说是可以解释的,而不仅仅是“哦,它代表解析器的那些字符”)。这些的定义和每个的示例是什么?

需要NCName vs QName来支持XML Namespaces

NCName

A Non-Colonized NAME 可以包含 XML Name 中允许的任何字符,但冒号 : 除外:

NCName         ::= Name - (Char* ':' Char*)

示例: p 是一个 NCName,其中 <p> 可能是段落开始标记。

定义不能包含 : 的名称的动机是保留 : 作为 XML 名称空间前缀和名称其余部分之间的分隔符,称为本地部分。这将我们带到 QName...

QName

A Qqualified NAME 可能(但不需要)有一个命名空间前缀分隔来自本地 ::

QName          ::= PrefixedName | UnprefixedName
PrefixedName   ::= Prefix ':' LocalPart
UnprefixedName ::= LocalPart
Prefix         ::= NCName
LocalPart      ::= NCName

示例: w:p是一个QName,而<w:p>是OOXML中的一个段落标签,其中w 是声明为 xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main".

的名称空间前缀

另见