[xsl] Convert text nodes to a single string

Subject: [xsl] Convert text nodes to a single string
From: "rick@xxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 9 Mar 2022 13:25:53 -0000
Hi All,

 

I am trying to flatten a messy XHTML file by unwrapping any elements that
don't have non-whitespace text nodes. I am trying to return a single string
from all of the text nodes in an element and see if its normalized value is
an empty string. Here is my sample input:

 

<?xml version="1.0" encoding="UTF-8"?>

<root>This has <b>some </b>text.</root>

 

Here is my stylesheet:

 

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";

    xmlns:xs="http://www.w3.org/2001/XMLSchema";

    xmlns:math="http://www.w3.org/2005/xpath-functions/math";

    xmlns:rq="http://www.frameexpert.com/functions";

    exclude-result-prefixes="xs math rq" 

    version="3.0" expand-text="yes">

    

    <xsl:output indent="yes"/>

    

    <xsl:strip-space elements="td"/>

    

    <xsl:template match="/root">

        <xsl:message>{text()}</xsl:message>

        <xsl:message>{count(text())}</xsl:message>

        <xsl:message>{count(rq:getStringFromText(text()))}</xsl:message>

        <xsl:apply-templates/>

    </xsl:template>

    

    <xsl:function name="rq:getStringFromText" as="xs:string*">

        <xsl:param name="text-nodes"/>

        <xsl:for-each select="$text-nodes">

            <xsl:value-of select="normalize-space(.)"/>

        </xsl:for-each>

    </xsl:function>

    

</xsl:stylesheet>

 

I expected that this line

 

<xsl:message>{count(rq:getStringFromText(text()))}</xsl:message>

 

would return 1, but instead I get 2. Perhaps I need recursion in my
function. Thank you in advance.

 

Rick

Current Thread