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

Subject: Re: [xsl] Testing if first child is text or an element?
From: "Piez, Wendell A. (Fed) wendell.piez@xxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 16 Apr 2024 00:22:29 -0000
Hi,

Instead of a conditional on the container, how about handling only the node we
want to amend?

<xsl:template match="info/child::text()[1][matches(.,'\S')]">
   <xsl:text>+</xsl:text>
  <xsl:next-match/>
</xsl:template>

Much simpler, I think, probably also performs better.

Apologies if I misconstrue the question!

Cheers, Wendell

From: dvint dvint@xxxxxxxxx <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Monday, April 15, 2024 6:08 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Testing if first child is text or an element?

That did the trick. Figured I needed a normalize-space in there somewhere,
just not sure how to add it.



Sent from my Verizon, Samsung Galaxy smartphone


-------- Original message --------
From: "Martin Honnen martin.honnen@xxxxxx<mailto:martin.honnen@xxxxxx>"
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx<mailto:xsl-list-service@xxxxxxxxxxxx
rytech.com>>
Date: 4/15/24 12:35 PM (GMT-08:00)
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx<mailto:xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Subject: Re: [xsl] Testing if first child is text or an element?


On 15/04/2024 21:29, dvint@xxxxxxxxx<mailto: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>
>   <step >
>     <info>
>       1 The following collection shows examples of non-checklist
>       lists with nested checklists.
>       <ul>
>          <li>This is an unordered list item with a child
>             checklist.
>          </li>
>       </ul>
>     </info>
>   </step>
>   <step >
>     <info>
>        <p>2 The following collection shows examples of non-checklist
>         lists with nested checklists.</p>
>        <ul>
>           <li>This is an unordered list item with a child
>            checklist.
>           </li>
>        </ul>
>    </info>
>   </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.
>
>
>
>     <xsl:template name="list-block-start">
>
>         <xsl:if test="
>             ancestor::step ">
>
>             <xsl:choose>
>                 <!-- collapsed tables in kyt1659720640909.dita -->
>                 <xsl:when test="ancestor::*[contains(@outputclass,
> 'collapse')]"/>
>                 <xsl:otherwise>
>                     <xsl:value-of select="$RETURN"/>
>                     <xsl:choose>
>                         <xsl:when test="local-name()='info'">
>                             <xsl:choose>
>                                 <xsl:when test="child::*[1] instance
> of element()">
>                                     <!-- do nothing for info element
> with content -->
>                                 </xsl:when>
>                                 <xsl:when test="child::*[1] instance
> of text()">
> <xsl:text>+</xsl:text>
>                                     <xsl:value-of select="$RETURN"/>
>                                 </xsl:when>
>                             </xsl:choose>
>

child::* selects element nodes  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

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

or

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

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


XSL-List info and archive<http://www.mulberrytech.com/xsl/xsl-list>
EasyUnsubscribe<http://lists.mulberrytech.com/unsub/xsl-list/3302254> (by
email<>)

Current Thread