Search code examples
xmlxpathxml-namespacesw3cebnf

Understanding NCName and QName in XPath/XML


What is a basic explanation of what a NCName and QName in XML? It is mentioned quite a few times on the XPath page, for example with something like:

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.

But it's hard for me to understand what that is, and when clicking the links, it seems to give a "syntax/grammar" definition but not really one that would 'make sense' to me (i.e., be explainable to me and not just "Oh it represents those characters to a parser"). What would be a definition of these and an example for each?


Solution

  • The need for NCName vs QName arises to support XML Namespaces.

    NCName

    A Non-Colonized NAME can contain any characters allowed in an XML Name except a colon, : :

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

    Example: p is an NCName, where <p> might be a paragraph start tag.

    The motivation for defining a name that cannot contain a : is to reserve : as a separator between XML namespace prefixes and the rest of the name, known as the local part. Which brings us to QName...

    QName

    A Qualified NAME may (but need not) have a namespace prefix separated from a local part by a ::

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

    Example: w:p is a QName, and <w:p> is a paragraph tag in OOXML, where w is a namespace prefix declared as xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main".

    See also