5.8 Persistent Node Identifiers (NIDs)

If an XML document has an XML Schema or DTD, certain attributes can be marked to be ID or IDREF attributes. Such attributes can be used in the fn:id() function to look up nodes by their ID. This way, the XML Data Model provides some support for graph data structures.

Internally, each node in MonetDB/XQuery (not only element nodes, but nodes of all kinds, except attribute nodes) have a unique node identifier (NID). This NID can be compared with ROW-IDs in relational database system. Moreover, the NID is tightly coupled to the physical location of the node's information in the database storage system. This also implies that given a NID, the database system knows exactly where it is located, so it can read the node's information without even having to use an index structure.

Such high-performance lookups may be useful to applications. For this reason we exposed an built-in extension function:

pf:nid($n as node()) as xs:string

It returns a string that contains a number. Note that in the XML Data Model, ID attributes must be non-numerical strings.

So the second extension was to modify the behavior of fn:id() to accept numeric identifiers, and to interpret them as NIDs. Thus you can use the normal fn:id() function to lookup your NIDs.

Warning: while NIDs are stable under updates, a Database Restore (see Backup/Restore) of an updatable XML Collection may change the NID values of your XML. So, in updatable XML data that should live across Restore points, you cannot reliably use NIDs. Note that this is not an issue for read-only XML Collections (see Read-only versus Updatable).