Repository: dita-ot/dita-ot
Issue: `<xsl:message>` of an XML element also recursively prints its descendants

## Expected Behavior
When I use `<xsl:message>` to print an element:

```
    <xsl:template match="*[contains-token(@class, 'topic/p')]">
        <xsl:message>***BEGIN***</xsl:message>
        <xsl:message><xsl:sequence select="."/></xsl:message>
        <xsl:message>***END***</xsl:message>
        <xsl:next-match/>
    </xsl:template>
```

and the element being printed has descendant elements in it:

```
<p>1
  <ph>2
    <ph>3
      <ph>4</ph>
    5</ph>
  6</ph>
7</p>
```

then I expect the element to be printed, serialized as it exists in the document:

```
     [xslt] ***BEGIN***
     [xslt] <p xmlns:dita-ot="http://dita-ot.sourceforge.net/ns/201007/dita-ot" xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/" class="- topic/p ">1
     [xslt]   <ph class="- topic/ph ">2
     [xslt]     <ph class="- topic/ph ">3
     [xslt]       <ph class="- topic/ph ">4</ph>
     [xslt]     5</ph>
     [xslt]   6</ph>
     [xslt] 7</p>
     [xslt] ***END***
```

## Actual Behavior
Somehow the element is printed once normally, then the contents of its descendants are also recursively printed:
```
     [xslt] ***BEGIN***
     [xslt] <p xmlns:dita-ot="http://dita-ot.sourceforge.net/ns/201007/dita-ot"
     [xslt]    xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/"
     [xslt]    class="- topic/p ">1
     [xslt]   <ph class="- topic/ph ">2
     [xslt]     <ph class="- topic/ph ">3
     [xslt]       <ph class="- topic/ph ">4</ph>
     [xslt]     5</ph>
     [xslt]   6</ph>
     [xslt] 7</p>1
     [xslt]   <ph xmlns:dita-ot="http://dita-ot.sourceforge.net/ns/201007/dita-ot"
     [xslt]     xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/"
     [xslt]     class="- topic/ph ">2
     [xslt]     <ph class="- topic/ph ">3
     [xslt]       <ph class="- topic/ph ">4</ph>
     [xslt]     5</ph>
     [xslt]   6</ph>2
     [xslt]     <ph xmlns:dita-ot="http://dita-ot.sourceforge.net/ns/201007/dita-ot"
     [xslt]     xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/"
     [xslt]     class="- topic/ph ">3
     [xslt]       <ph class="- topic/ph ">4</ph>
     [xslt]     5</ph>3
     [xslt]       <ph xmlns:dita-ot="http://dita-ot.sourceforge.net/ns/201007/dita-ot"
     [xslt]     xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/"
     [xslt]     class="- topic/ph ">4</ph>4
     [xslt]     5
     [xslt]   6
     [xslt] 7
     [xslt] ***END***
```

## Possible Solution
I think this is related to the fix for #3662. Note that the "expected behavior" above comes from DITA-OT 3.5.4, before the original bug was introduced.

## Steps to Reproduce
1. Extract the following testcase: [ditaot_message_nested_elements.zip](https://github.com/dita-ot/dita-ot/files/9154617/ditaot_message_nested_elements.zip)
2. Install the plugin:
   `dita install com.debug.message.zip`
4. Run the following command:
   `dita -i topic.dita -f html5 -o out -Dgenerate-debug-attributes=no --verbose`
5. Uninstall the plugin:
   `dita uninstall com.debug.message`

## Environment
* DITA-OT version: 3.7.2
* Operating system and version: Linux (Ubuntu 20.04 LTS)
* How did you run DITA-OT? `dita` command
* Transformation type: any


Changed files:
src/test/java/org/dita/dost/util/XMLUtilsTest.java, src/main/java/org/dita/dost/util/XMLUtils.java

diff --git a/src/main/java/org/dita/dost/util/XMLUtils.java b/src/main/java/org/dita/dost/util/XMLUtils.java
index 088a553c8..e57172dfa 100644
--- a/src/main/java/org/dita/dost/util/XMLUtils.java
+++ b/src/main/java/org/dita/dost/util/XMLUtils.java
@@ -160,7 +160,7 @@ public final class XMLUtils {
                             .map(XdmItem::getStringValue)
                             .orElse("INFO");
                 final String msg = content
-                        .select(descendant(
+                        .select(child(
                                 hasLocalName("level").or(hasLocalName("error-code"))
                                         .and(hasType(ItemType.PROCESSING_INSTRUCTION_NODE))
                                 .negate()))
diff --git a/src/test/java/org/dita/dost/util/XMLUtilsTest.java b/src/test/java/org/dita/dost/util/XMLUtilsTest.java
index a9d1687b3..ae1e12c27 100644
--- a/src/test/java/org/dita/dost/util/XMLUtilsTest.java
+++ b/src/test/java/org/dita/dost/util/XMLUtilsTest.java
@@ -364,6 +364,26 @@ public class XMLUtilsTest {
         assertEquals(1, act.size());
         assertEquals(new Message(Message.Level.ERROR, "message <debug/>", null), act.get(0));
     }
+    
+    @Test
+    public void toMessageListenerProperElemsSerialization() throws SaxonApiException {
+        final CachingLogger logger = new CachingLogger();
+        final MessageListener2 listener = XMLUtils.toMessageListener(logger);
+
+        final XdmNode msg = Saplings.doc().withChild(
+                    Saplings.text("abc "),
+                    Saplings.elem("def").withChild(Saplings.elem("hij"))
+                )
+                .toXdmNode(new XMLUtils().getProcessor());
+
+        listener.message(msg, null, false, null);
+
+        final List<Message> act = logger.getMessages();
+        assertEquals(1, act.size());
+        assertEquals("abc <def>\\n" + 
+                "   <hij/>\\n" + 
+                "</def>", act.get(0).message);
+    }
 
 //    @Test
 //    public void transform() throws Exception {