Link to home
Start Free TrialLog in
Avatar of Dinesh Bali
Dinesh Bali

asked on

Adding condition in href string in XSLT

Hi,

I need help in XSLT.

In my XSLT, I have many links in href previously.
For sme reason I need to move entire web app to virtual folder.

Now, I have to place another variable before all href link with
{$virtualFolderPath}
so, previously, link was
{//web/menu[@title='Contact Us']/@url}

Now after modifying link become as below
{$virtualFolderPath}{//web/menu[@title='Contact Us']/@url}


Sample XSLT code below:
<xsl:if test="//web/menu[@title = 'Contact Us']">
      <a href="{$virtualFolderPath}{//web/menu[@title='Contact Us']/@url}" class="dropdown-toggle main-links">Contact Us</a>
</xsl:if>

Problem Statement:

If link is full link like:
http://www.somewebapp.com/somepage
then href link become
/b2chttp://www.somewebapp.com/somepage
as path in
{$virtualFolderPath} is /b2c

How should I fix so that if URL starts from
http:// 
then do not add my path:
{$virtualFolderPath}
and URL should be direct

Please guide.
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

There a lot of different options, here is the one that works in XSLT1.0 also

create the attribute with an xsl:attribute and have the conditional inside

Note that I made a choose, so you can have more conditions such as https:// etc...

        <xsl:if test="//web/menu[@title = 'Contact Us']">
            <a class="dropdown-toggle main-links">
                <xsl:attribute name="href">
                    <xsl:choose>
                        <xsl:when test="starts-with(//web/menu[@title='Contact Us']/@url, 'http://')"></xsl:when>
                        <xsl:when test="starts-with(//web/menu[@title='Contact Us']/@url, 'https://')"></xsl:when>
                        <xsl:when test="starts-with(//web/menu[@title='Contact Us']/@url, 'ftp://')"></xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="$virtualFolderPath"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:attribute>
                <xsl:value-of select="//web/menu[@title='Contact Us']/@url"/>
                <xsl:text>Contact Us</xsl:text>
            </a>
        </xsl:if>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That is a lot more robust
Avatar of Dinesh Bali
Dinesh Bali

ASKER

Many Many Thanks for your help.