Extracting Domain from URL in XSLT

Posted on 2004-11-10
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:

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
    LVL 4

    Expert Comment

    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

    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="">
          <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.
    LVL 2

    Author Comment

    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.
    LVL 2

    Author Comment

    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

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
    The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API ( has made its way into the popular lexicon of the English language.  A few years ago, …
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now