diff --git a/contrib/xml2/expected/xml2.out b/contrib/xml2/expected/xml2.out
index 3d97b14c3a1..1906fcf33e2 100644
--- a/contrib/xml2/expected/xml2.out
+++ b/contrib/xml2/expected/xml2.out
@@ -261,3 +261,13 @@ $$
$$);
ERROR: failed to apply stylesheet
+-- empty output
+select xslt_process('',
+$$
+$$);
+ xslt_process
+--------------
+
+(1 row)
+
diff --git a/contrib/xml2/expected/xml2_1.out b/contrib/xml2/expected/xml2_1.out
index 31700040a60..9a2144d58f5 100644
--- a/contrib/xml2/expected/xml2_1.out
+++ b/contrib/xml2/expected/xml2_1.out
@@ -205,3 +205,9 @@ $$
$$);
ERROR: xslt_process() is not available without libxslt
+-- empty output
+select xslt_process('',
+$$
+$$);
+ERROR: xslt_process() is not available without libxslt
diff --git a/contrib/xml2/sql/xml2.sql b/contrib/xml2/sql/xml2.sql
index ef99d164f27..510d18a3679 100644
--- a/contrib/xml2/sql/xml2.sql
+++ b/contrib/xml2/sql/xml2.sql
@@ -153,3 +153,9 @@ $$
$$);
+
+-- empty output
+select xslt_process('',
+$$
+$$);
diff --git a/contrib/xml2/xslt_proc.c b/contrib/xml2/xslt_proc.c
index b720d89f754..8ff7c7f1bbf 100644
--- a/contrib/xml2/xslt_proc.c
+++ b/contrib/xml2/xslt_proc.c
@@ -176,7 +176,14 @@ xslt_process(PG_FUNCTION_ARGS)
if (resstat < 0)
PG_RETURN_NULL();
- result = cstring_to_text_with_len((char *) resstr, reslen);
+ /*
+ * If an empty string has been returned, resstr would be NULL. In
+ * this case, assume that the result is an empty string.
+ */
+ if (reslen == 0)
+ result = cstring_to_text("");
+ else
+ result = cstring_to_text_with_len((char *) resstr, reslen);
if (resstr)
xmlFree(resstr);