XSLT - regular expression replace character

I have an xsl example like this,

<doc>
  <para>text . . .text</para>
  <para>text . . .text. . . . . .text</para>
</doc>

As you can see, there are several templates in xml, for example, . . . I need to replace the space between points with *. Therefore, the output should look like this:

<doc>
  <para>text .*.*.text</para>
  <para>text .*.*.text.*.*.*.*.*.text</para>
</doc>

I wrote the following xslt for this,

<xsl:template match="text()">
        <xsl:analyze-string select="." regex="(\.)(&#x0020;)(\.)">
            <xsl:matching-substring>
                <xsl:value-of select="replace(.,regex-group(2),'*')"/>
            </xsl:matching-substring>
            <xsl:non-matching-substring>
                <xsl:value-of select="."/>
            </xsl:non-matching-substring>
        </xsl:analyze-string>
    </xsl:template>

but it eliminates all other spaces and gives me the following result,

<doc>
  <para>text .*. .text</para>
  <para>text .*. .text.*. .*. .*.text</para>
</doc>

How can I change my XSLT to get the correct output.

+4
source share
1 answer

I think,

<xsl:template match="text()">
    <xsl:analyze-string select="." regex="(\.)( )(\.)( \.)*">
        <xsl:matching-substring>
            <xsl:value-of select="replace(., ' ','*')"/>
        </xsl:matching-substring>
        <xsl:non-matching-substring>
            <xsl:value-of select="."/>
        </xsl:non-matching-substring>
    </xsl:analyze-string>
</xsl:template>

performs this work. As LukStorms notes, this can be simplified to

<xsl:template match="text()">
    <xsl:analyze-string select="." regex="\.( \.)+">
        <xsl:matching-substring>
            <xsl:value-of select="replace(., ' ','*')"/>
        </xsl:matching-substring>
        <xsl:non-matching-substring>
            <xsl:value-of select="."/>
        </xsl:non-matching-substring>
    </xsl:analyze-string>
</xsl:template>
+3
source

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


All Articles