xsl-list
[Top] [All Lists]

Re: [xsl] Convert text nodes to a single string

2022-03-09 07:34:55
Hey Rick,

Perhaps another way to accomplish your goal is simply a test on whether
such a text node child exists:

exists(child::text()[matches(.,'\S')])

returns true when a child text node has any non-whitespace content.

Cheers, Wendell

On Wed, Mar 9, 2022 at 8:26 AM rick(_at_)rickquatro(_dot_)com <
xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

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


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



-- 
...Wendell Piez... ...wendell -at- nist -dot- gov...
...wendellpiez.com... ...pellucidliterature.org... ...pausepress.org...
...github.com/wendellpiez... ...gitlab.coko.foundation/wendell...
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--
<Prev in Thread] Current Thread [Next in Thread>