diff --git a/src/main/java/org/apache/ibatis/scripting/xmltags/XMLScriptBuilder.java b/src/main/java/org/apache/ibatis/scripting/xmltags/XMLScriptBuilder.java
index 6e9cafc1fb4..b8102a87dad 100644
--- a/src/main/java/org/apache/ibatis/scripting/xmltags/XMLScriptBuilder.java
+++ b/src/main/java/org/apache/ibatis/scripting/xmltags/XMLScriptBuilder.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2009-2023 the original author or authors.
+ * Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -80,6 +80,9 @@ protected MixedSqlNode parseDynamicTags(XNode node) {
XNode child = node.newXNode(children.item(i));
if (child.getNode().getNodeType() == Node.CDATA_SECTION_NODE || child.getNode().getNodeType() == Node.TEXT_NODE) {
String data = child.getStringBody("");
+ if (data.trim().isEmpty()) {
+ continue;
+ }
TextSqlNode textSqlNode = new TextSqlNode(data);
if (textSqlNode.isDynamic()) {
contents.add(textSqlNode);
diff --git a/src/test/java/org/apache/ibatis/scripting/xmltags/XMLScriptBuilderTest.java b/src/test/java/org/apache/ibatis/scripting/xmltags/XMLScriptBuilderTest.java
new file mode 100644
index 00000000000..7d1ec46c8c6
--- /dev/null
+++ b/src/test/java/org/apache/ibatis/scripting/xmltags/XMLScriptBuilderTest.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2009-2024 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ibatis.scripting.xmltags;
+
+import static org.assertj.core.api.Assertions.*;
+import static org.junit.Assert.assertEquals;
+
+import java.lang.reflect.Field;
+import java.util.List;
+
+import org.apache.ibatis.mapping.SqlSource;
+import org.apache.ibatis.parsing.XPathParser;
+import org.apache.ibatis.session.Configuration;
+import org.junit.jupiter.api.Test;
+
+class XMLScriptBuilderTest {
+
+ @Test
+ void shouldEmptyTextNodesRemoved() throws Exception {
+ String xml = """
+
+ """;
+ SqlSource sqlSource = new XMLScriptBuilder(new Configuration(), new XPathParser(xml).evalNode("/script"))
+ .parseScriptNode();
+
+ Field rootSqlNodeFld = DynamicSqlSource.class.getDeclaredField("rootSqlNode");
+ rootSqlNodeFld.setAccessible(true);
+ MixedSqlNode sqlNode = (MixedSqlNode) rootSqlNodeFld.get(sqlSource);
+
+ Field contentsFld = MixedSqlNode.class.getDeclaredField("contents");
+ contentsFld.setAccessible(true);
+ @SuppressWarnings("unchecked")
+ List contents = (List) contentsFld.get(sqlNode);
+
+ assertEquals(3, contents.size());
+ assertThat(contents.get(0)).isInstanceOf(StaticTextSqlNode.class);
+ assertThat(contents.get(1)).isInstanceOf(IfSqlNode.class);
+ assertThat(contents.get(2)).isInstanceOf(IfSqlNode.class);
+ }
+
+}