In trattoria scoprii che c'era an...">

How to "collapse" but not "normalize" spaces in xlst

I have xml / tei like

 <p> In trattoria scoprii che c'era <del rend="tratto a matita">anche</del> Mirella,
                non la non vedevo da almeno sei anni. 
                La spianata dava infatti l'impressione di fango secco, <del rend="matita">divorato
                    dalle rughe</del><add place="margine sinistro" rend="matita">attraversato da
                    lunghe ferite nere</add>. Lontano si vedeva una montagna di creta dello
                stesso colore della mota. </p>

I use this style sheet to remove spaces, both between elements and inside text nodes.

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

<xsl:template match="/">
    <xsl:apply-templates />
</xsl:template>

<xsl:template match="*">
    <xsl:copy>
        <xsl:for-each select="@*">
            <xsl:attribute name="{name()}">
                <xsl:value-of select="normalize-space()"/>
            </xsl:attribute>
        </xsl:for-each>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>
<xsl:template match="text()">
    <xsl:value-of select="normalize-space()"/>
</xsl:template>

Everything is fine for the fact that normalize-space () also removes leading and distorting spaces, so I have some non-obvious behavior like

c'era<del rend="tratto a matita">anche</del>Mirella

I cannot exclude the contents of mixed mode for deletion, because my first need is to collapse spaces such as returns, tabs, INSIDE authentication, for example, an element <p>.

Is there a way / function / trick to collapse multiple spaces in one space without removing the start and end spaces?

+3
1

, , , ( , XPath 2) replace(), , . ( xml.com).

, , , :

select="normalize-space()"

select="replace(., '(\s\s+)', ' ')"

.

: , Mycol .

+1

Source: https://habr.com/ru/post/1749222/


All Articles