diff --git a/main/config_options.c b/main/config_options.c
index 3c0959ea3cf26f2ee98267086ce6eb99aae3879a..a9a145b96a166947fe33b00a65cb7503982bb192 100644
--- a/main/config_options.c
+++ b/main/config_options.c
@@ -998,11 +998,11 @@ static int xmldoc_update_config_type(const char *module, const char *name, const
 
 	/* If we already have a syntax element, bail. This isn't an error, since we may unload a module which
 	 * has updated the docs and then load it again. */
-	if ((results = ast_xmldoc_query("//configInfo[@name='%s']/*/configObject[@name='%s']/syntax", module, name))) {
+	if ((results = ast_xmldoc_query("/docs/configInfo[@name='%s']/configFile/configObject[@name='%s']/syntax", module, name))) {
 		return 0;
 	}
 
-	if (!(results = ast_xmldoc_query("//configInfo[@name='%s']/*/configObject[@name='%s']", module, name))) {
+	if (!(results = ast_xmldoc_query("/docs/configInfo[@name='%s']/configFile/configObject[@name='%s']", module, name))) {
 		ast_log(LOG_WARNING, "Cannot update type '%s' in module '%s' because it has no existing documentation!\n", name, module);
 		return XMLDOC_STRICT ? -1 : 0;
 	}
@@ -1068,7 +1068,7 @@ static int xmldoc_update_config_option(struct aco_type **types, const char *modu
 		return XMLDOC_STRICT ? -1 : 0;
 	}
 
-	if (!(results = ast_xmldoc_query("//configInfo[@name='%s']/*/configObject[@name='%s']/configOption[@name='%s']", module, object_name, name))) {
+	if (!(results = ast_xmldoc_query("/docs/configInfo[@name='%s']/configFile/configObject[@name='%s']/configOption[@name='%s']", module, object_name, name))) {
 		ast_log(LOG_WARNING, "Could not find option '%s' with type '%s' in module '%s'\n", name, object_name, module);
 		return XMLDOC_STRICT ? -1 : 0;
 	}
diff --git a/main/xml.c b/main/xml.c
index d32a8c5e4f4dcf56954032e793739f0926a6c4f3..d476c1d4f7a643e7647245ebfdf377d5a0a4b6a8 100644
--- a/main/xml.c
+++ b/main/xml.c
@@ -96,6 +96,9 @@ struct ast_xml_doc *ast_xml_open(char *filename)
 	ast_log(LOG_NOTICE, "XSLT support not found. XML documentation may be incomplete.\n");
 #endif /* HAVE_LIBXSLT */
 
+	/* Optimize for XPath */
+	xmlXPathOrderDocElems(doc);
+
 	return (struct ast_xml_doc *) doc;
 }