XQuery/All Paths

Motivation
You want to generate a list of all unique path expressions to a document.

This process is very useful to quickly get familiar with a new data set. It is also important to make sure that your document-style transforms are accessing all the elements. This process can also be used as a basis for generating index files for a new data set.

Example Output
Paths the list of unique paths for a sample file from the Shakespeare Demos on the eXist demo system at /db/shakespeare/plays/hamlet.xml would generate the following results.

Note that these path expressions are sorted in document order, that is the order that the path first appeared in a document. So you can see that the cast list in the PERSONAE appear before the ACT/SCENE elements. The output can also be sorted in alphabetical order.

Method
We will use the functx libraries.

In particular the function:

functx:distinct-element-paths($nodes)

takes as its input a node and returns a sequence of strings of the path expressions.

See Documentation on xqueryfunctions.com

distinct-element-paths function
The heart of this query is the single expression:

ancestor-or-self::*/name(.)

Which says in effect "get me the element names of all the nodes in the document". The next step is to turn this list into a list of distinct element paths. This is done by the function functx:distinct-element-paths

Working with a single test document
use the document

Working with a document collection
use collection function

Acknowledgments
David Elwell posted this suggestion on the open-exist list on July 22 of 2010