Re: [xsl] Testing if first child is text or an element?

Subject: Re: [xsl] Testing if first child is text or an element?
From: "Martin Honnen martin.honnen@xxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 15 Apr 2024 19:35:42 -0000
On 15/04/2024 21:29, dvint@xxxxxxxxx wrote:
I've got some markup that allows mixed content in an element. When I
process this content I need to do something different if the element
starts with text vs an element. My content can be like this:

<?xml version="1.0" encoding="UTF-8"?>
<doc>
B  <step >
B B B  <info>
B B B B B  1 The following collection shows examples of non-checklist
B B B B B  lists with nested checklists.
B B B B B  <ul>
B B B B B B B B  <li>This is an unordered list item with a child
B B B B B B B B B B B  checklist.
B B B B B B B B  </li>
B B B B B  </ul>
B B B  </info>
B  </step>
B  <step >
B B B  <info>
B B B B B B  <p>2 The following collection shows examples of non-checklist
B B B B B B B  lists with nested checklists.</p>
B B B B B B  <ul>
B B B B B B B B B  <li>This is an unordered list item with a child
B B B B B B B B B B  checklist.
B B B B B B B B B  </li>
B B B B B B  </ul>
B B  </info>
B  </step>
</doc>

I want it to produce:

+
1 The following collection shows examples of non-checklist
lists with nested checklists.
* This is an unordered list item with a child
checklist.

2 The following collection shows examples of non-checklist
lists with nested checklists.</p>
* This is an unordered list item with a child

So if the <info> starts with text it should add the '+' and if it
starets with an element, add nothing.



B B B B <xsl:template name="list-block-start">

B B B B B B B  <xsl:if test="
B B B B B B B B B B B  ancestor::step ">

B B B B B B B B B B B  <xsl:choose>
B B B B B B B B B B B B B B B  <!-- collapsed tables in
kyt1659720640909.dita -->
B B B B B B B B B B B B B B B <xsl:when
test="ancestor::*[contains(@outputclass,
'collapse')]"/>
B B B B B B B B B B B B B B B  <xsl:otherwise>
B B B B B B B B B B B B B B B B B B B  <xsl:value-of select="$RETURN"/>
B B B B B B B B B B B B B B B B B B B  <xsl:choose>
B B B B B B B B B B B B B B B B B B B B B B B  <xsl:when
test="local-name()='info'">
B B B B B B B B B B B B B B B B B B B B B B B B B B B  <xsl:choose>
B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B  <xsl:when
test="child::*[1] instance
of element()">
B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B  <!--
do nothing for info element
with content -->
B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B  </xsl:when>
B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B  <xsl:when
test="child::*[1] instance
of text()">
<xsl:text>+</xsl:text>
B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B
<xsl:value-of select="$RETURN"/>
B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B  </xsl:when>
B B B B B B B B B B B B B B B B B B B B B B B B B B B  </xsl:choose>


child::* selects element nodesB only I think.


But do realize that the second info element also starts with a pure
whitespace text node child.


So, unless you strip-space, both your case have a text node child as the first child, you might want to check

B B info[node()[1][normalize-space()] instance of text()]]

or

B B info[node()[1][normalize-space()][self::text()]]

to match the info element that starts with a text node that has more
than whitespace.

Current Thread