code: private String getRecursiveString() {
          final StringBuilder output = new StringBuilder();
          // append headers e.g. namespaces and functions
          output.append(String.join("\n", getHeaders()));
  
          if (!qvarMapVariable.isEmpty() && addQvarMap) {
              output.append(qvarMapVariable).append("\n");
          }
  
          output.append("\ndeclare function local:compareApply($rootApply, $depth, $x ) {\n")
                  .append("(for $child in $x/* return local:compareApply(\n")
                  .append("if (empty($rootApply) and $child/name() = \"apply\") then $child else $rootApply,\n")
                  .append("if (empty($rootApply) and $child/name() = \"apply\") then 0 else $depth+1, $child),\n")
                  .append("if ($x/name() = \"apply\"\n")
                  .append(" and $x").append(getExactMatchXQuery()).append("\n");
          if (!getLengthConstraint().isEmpty()) {
              output.append(" and ").append(getLengthConstraint()).append("\n");
          }
          if (!qvarConstraint.isEmpty()) {
              output.append(" and ").append(qvarConstraint).append("\n");
          }
          output.append(" ) then\n")
                  .append(getReturnFormat()).append("\n")
                  .append("else ()\n")
                  .append(")};\n\n")
                  .append("for $m in ").append(getPathToRoot()).append(" return\n")
                  .append("local:compareApply((), 0, $m)");
  
          return output.toString();
      }
docs: Builds the XQuery as a string. Uses the recursive format of recursively looping through the documents.
  This enables the $depth and the $rootApply variables.
  
  @return XQuery as string