diff --git a/contrib/xml2/xpath.c b/contrib/xml2/xpath.c
index 3f733405ec6d..11216b9b7f9a 100644
--- a/contrib/xml2/xpath.c
+++ b/contrib/xml2/xpath.c
@@ -209,7 +209,7 @@ pgxmlNodeSetToText(xmlNodeSetPtr nodeset,
 			xmlBufferWriteChar(buf, ">");
 		}
 
-		result = xmlStrdup(buf->content);
+		result = xmlStrdup(xmlBufferContent(buf));
 		if (result == NULL || pg_xml_error_occurred(xmlerrcxt))
 			xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
 						"could not allocate result");
