Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Extracting Domain from URL in XSLT

Posted on 2004-11-10
Medium Priority
Last Modified: 2012-05-05
I'd like to extract the domain out of a url such that the XSLT will convert a url like: http://www.multimap.com/map/browse.cgi?X=-8550000.33029827&Y=2025000.0309924&scale=1000000&coordsys=mercator 

Any ideas on a solid method to get this to look right?  I guess I could use javascript after displaying the page and getting the domain out of the location object, but I'd prefer to do it on the XSLT side.

Question by:yleviel
  • 2

Expert Comment

ID: 12551135
use substring-before() and substring-after() function in xslt:

for example, a xml is this:

then your xsl:

<xsl:variable name="url_ini" select="root/url"/>
<xsl:variable name="url_m" select="substring-before($url_ini,'/')"/>
<xsl:value-of select="substring-after($url_m,'//')"/>

LVL 15

Accepted Solution

dualsoul earned 500 total points
ID: 12554145
the best will be switched to XSLT 2.0 and use built-in regular expressions support :)
but if you don't want to do it:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
      <xsl:template match="/">
        <xsl:variable name="url_ini" select="/root/url"/>        
        <xsl:variable name="url_minus_http" select="substring-after($url_ini,'http://')"/>
        <xsl:value-of select="substring-before($url_minus_http,'/')"/>             

, sample xml is here (i used ChenChen sample):
<?xml version="1.0" encoding="UTF-8"?>

, please note this method doen't respect cases, where you have no http:// , you can add this logic if you have such cases.

Author Comment

ID: 12587458
ChenChen & dualsoul,

ChenChen: I have been unable to get your answer to work.  It simply returns nothing for the url's... can you check your xsl and be sure that it works?

dualsoul: your answer works wonderfully, however is limited to http:// only.  I do not worry about having no prefix... but I do have cases of
1.) file://
2.) https://
3.) ftp://

Is there a solution that will handle all of these if I can guarantee that one of the following will be used?

Will I have to force the script to check for the prefix of each one?  I'll mess around with it and see if I can produce one for all and post it.

Author Comment

ID: 12587658
Using the above examples, I modified the XSL to read:

<xsl:variable name="url_ini" select="/root/url"/>
<!-- removed http from the second param -->
<xsl:variable name="url_minus_http" select="substring-after($url_ini,'://')"/>
<xsl:value-of select="substring-before($url_minus_http,'/')"/>

Works like a charm!


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Integration Management Part 2
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question