Tintin81
Tintin81

Reputation: 10215

How to replace string in XSLT?

I have this XML:

<path>/en/products/bike</path>

Is there an XSLT function that will return

/products/bike

?

Thanks for any help.

Upvotes: 1

Views: 601

Answers (2)

Mads Hansen
Mads Hansen

Reputation: 66781

With XSLT 2.0 you can also use the replace() function, which accepts a regex pattern as the second parameter and an optional 4th parameter for regex flags.

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" />
    <xsl:template match="path">
        <xsl:sequence select="replace(., '^/en','')"/>
    </xsl:template>
</xsl:stylesheet>

Upvotes: 0

Ben L
Ben L

Reputation: 1312

You can use the substring-after function.

So, for example, the following XSLT stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>

  <xsl:template match="path">
    <xsl:value-of select="substring-after(., '/en')"/>
  </xsl:template>
</xsl:stylesheet>

when applied to your XML:

<path>/en/products/bike</path>

will give:

/products/bike

Upvotes: 3

Related Questions