Welcome to WebmasterWorld Guest from

Forum Moderators: httpwebwitch

Message Too Old, No Replies

XSL question from someone with no experience

4:44 pm on May 20, 2008 (gmt 0)

5+ Year Member


We have an XSL file on the dev server but it is writing urls with the production url. It was suggested I use substring-before in this line of code:

<xsl:attribute name="href"><xsl:value-of select="link"/></xsl:attribute>

Is there a good beginner's resource out there that might help me understand this logic and the syntax a bit better?

For example, right now our dev url is:


production url is:

I want to use the absolute to root only: /news/may-2008.aspx.

Hope that makes some sense.


5:00 pm on May 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

That makes perfect sense. Welcome to WebmasterWorld!

This is a fairly simple thing to do. If you want to use XSLT 1 (you may actually have access to XSLT 2 if you are on a Microsoft server), then substring-after [w3schools.com] is what you want (For the record, many of the functions on that page are XSLT 2-only, but are much more sensible, in many cases).

I'd try this (untested):

 <xsl:attribute name="href"><xsl:value-of select="substring-after('/',link)"/></xsl:attribute>

NOTE: This will only work if the href does not have the "http://" protocol specifier. If it does, then you'll need to do something more like this:

 <xsl:attribute name="href"><xsl:value-of select="concat('http://', substring-after('/',substring-after('//',link)))"/></xsl:attribute>
7:49 pm on May 20, 2008 (gmt 0)

5+ Year Member

Thanks for the quick response! It takes me to the index page rather than the specific page though. Why would that be?
8:14 pm on May 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

Thanks for the quick response! It takes me to the index page rather than the specific page though. Why would that be?

I have no idea. It may be a server issue, and not an XSLT issue.

One thing I just realized I did wrong:

<xsl:attribute name="href"><xsl:value-of select="substring-after('/',link)"/></xsl:attribute>

Should probably actually read:

<xsl:attribute name="href"><xsl:value-of select="[red][b]concat('./',[/b][/red] substring-after('/',link)[red][b])[/b][/red]"/></xsl:attribute>

But I don't know enough about your system to know if that would make any difference.

Remember that the root directory "/" is always the first directory on the server after the domain. For example, in [server.example.com...] , "/" is not [server.example.com...] . It is [server.example.com...] (unless you've played with the .htaccess file). If you want to get to [server.example.com...] from the root, you need to specify "/root_dir/".

9:01 pm on May 20, 2008 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member

May I suggest...
When you're setting up dev and staging and QA and production environments (etc.), it's better to change the XML, not the XSLT. Forking for different servers should happen much farther upstream.

I presume you are generating XML data, which gets transformed into HTML pages, right? In production, you need your XSLT layer to be fast, streamlined, elegant, maintainable - free of unnecessary string manipulation, logic, and clutter.

Put the effort in on the back end where your XML is generated so it uses the right domain or subdomain, not in the middle layer which you would ideally like to keep finely tuned for performance.

If you're on the dev server, your XML should deliver links to the dev server. If on production, your XML should contain links to production. It's easier - keep your codebase identical on all servers, but use a global config file which you do not deploy to the production machine.

On dev, your config says:
$localserver = "http://dev.example.com"

On production, your config says:
$localserver = "http://www.example.com"

Then your XML engine should use that setting to construct all links. The XML rendering is identical, the XSLT is identical, and everything (except that one magic file) can be tested and deployed from one machine to another without causing any grief.

The string "http://dev.example.com" should never appear anywhere in your XML building code, the XSLT, CSS, Javascript, HTML, or anywhere. It's only in one place - the global config. From there, the setting flows downstream.

The global config file is also where you store any database connection strings, should you want to separate your databases into dev/staging/production as well.

See what I mean?

12:50 pm on Jul 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

httpwebwitch is (always) right...

A/ Check if you are on local or host server

$array_test_ip = array(""); // IP FOR TESTING PURPOSE
if(in_array($ip, $array_test_ip)) {$switch="0";} else {$switch="1";}

B/ Then make a configuration file

if ($switch=="0") {
$localserver = "http://dev.example.com"
} else {
$localserver = "http://www.example.com"

C/ Then, pass the variable into your XSLT Processor using the setParameter (for PHP5 - different with PHP4)

$xp = new XsltProcessor();
$xsl = new DomDocument;
$xml_doc = new DomDocument;

$xp->setParameter('', 'abs_path', $localserver);
if ($html = $xp->transformToXML($xml_doc)) {echo $html;}


Featured Threads

Hot Threads This Week

Hot Threads This Month