Suresh
Suresh

Reputation: 1091

xsl regex variable which has a url

my xsl variable has the value as below

<xsl:variable name="url">
        <xsl:value-of select="'http://1.2.34.4:70/Anything/uri'"/>
      </xsl:variable>

I need to replace the ip address and port combination part with a name - abcd.com, then store it in another variable. So the variable will have the value 'http://abcd.com/Anything/uri'. How can I do this. Do I have to use regex. The variable url can start with https as well, plus the uri can have any number os slashes. ie instead of /Anything/uri it can have /uri or /Anything/other/uri as well

Upvotes: 1

Views: 857

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243469

This XSLT 1.0 transformation:

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

 <xsl:variable name="vReplacement" select="'abcd.com'"/>

 <xsl:template match="/">
     <xsl:variable name="vUrl" select="'http://1.2.34.4:70/Anything/uri'"/>

     <xsl:variable name="vReplaced">
       <xsl:value-of select="concat(substring-before($vUrl,'//'), '//')"/>
       <xsl:value-of select="concat($vReplacement, '/')"/>
       <xsl:value-of select="substring-after(substring-after($vUrl,'//'),'/')"/>
     </xsl:variable>

     "<xsl:copy-of select="$vReplaced"/>"
 </xsl:template>
</xsl:stylesheet>

when applied on any XML document (not used), sets the variable $vReplaced to the wanted, correct value and copies its content to the output:

"http://abcd.com/Anything/uri"

Explanation: Proper use of substring-before(), substring-after() and concat().

Upvotes: 1

Related Questions