xsl-list
[Top] [All Lists]

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

2022-03-09 07:33:32

On 09.03.2022 14:30, Martin Honnen martin(_dot_)honnen(_at_)gmx(_dot_)de wrote:


On 09.03.2022 14:26, rick(_at_)rickquatro(_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.


I would suggest to use

  string-join(.//text())

(XPath 3 string-join I think defaults to the second argument of
string-join as the empty string, I think) or

  string-join(.//text(), '')


Then you can test whether e.g. `normalize-space(string-join(.//text(),
'') = ''`


On the other hand, if you access the string value of an element node
with e.g. data() or directly normalize-space() you don't even need
string-join and can just check `normalize-space() = ''`.
--~----------------------------------------------------------------
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>