1. A computer-implemented method executed on a processor for processing a query, comprising: receiving the query, wherein the query is formed by one or more paths, and wherein each path includes one or more steps; receiving a hierarchical document including one or more document nodes; and while processing the query and traversing the hierarchical document constructing a LookingFor structure, wherein the LookingFor structure includes LookingFor nodes, wherein each LookingFor node includes a next step in a path of the query, at least one of a minimum level and an absolute level for a next step instance, a level of an end event, a parent step instance identifier of a next step instance, and a matched step instance identifier when a match is found, wherein a document node in the hierarchical document that is described by a step in the query is called a step instance candidate, and wherein a step instance candidate is assigned a unique identifier called a step instance identifier; constructing one or more extraction entries constructed from the LookingFor structure, wherein each extraction entry includes a step instance match candidate identifying a document node and a step instance ancestor path for the document node; and constructing one or more tuples using the one or more extraction entries by associating the step instance match candidate from one of the one or more extraction entries with the step instance match candidate from at least one of the one or more other extraction entries wherein the constructing of the one or more tuples includes determining which step instance match candidates are to be paired, in which order the step instance match candidates are to be paired to form tuples, and which ancestors are to be compared for each pair of step instance match candidates.