1

I have a long web url in my xsl variable. eg. @url = "http://stackoverflow.com/questions/ask/2434/35454"

I need a substring from this based on 3rd index of "/". i.e I want to display only http://stackoverflow.com

There is a substring(string, start, length) function in xsl but how do i find the length portion. I could not find any indexof function.

<xsl:value-of select="substring(url,1,length)"/> 

My url is suppose - "http://stackoverflow.com/questions/ask/2434/35454" Output I want is http://stackoverflow.com

Please suggest some solutions.

1 Answer 1

1

Use:

   concat(substring-before(.,'//'),
          '//',
          substring-before(substring-after(., '//'),
                           '/'
                           )
         )

Complete code example:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

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

when this transformation is applied on this XML document:

<t>http://stackoverflow.com/questions/ask/2434/35454</t>

the wanted, correct result is produced:

http://stackoverflow.com
Sign up to request clarification or add additional context in comments.

2 Comments

So if my url is stored in $url I have to use like this. concat(substring-before(url,'//'), '//', substring-before(substring-after(url, '//'), '/' )
@AbhiRoczz... Yes, but don't forget the $ character before the name of the variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.