homepage Welcome to WebmasterWorld Guest from 54.196.18.51
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / XML Development
Forum Library, Charter, Moderators: httpwebwitch

XML Development Forum

    
XSL question from someone with no experience
simonbeave




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

Hello,

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:

ourdev/news/may-2008.aspx

production url is:
oursite/news/may-2008.aspx

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

Hope that makes some sense.

Thanks!

 

cmarshall




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

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>

simonbeave




msg:3654756
 7:49 pm on May 20, 2008 (gmt 0)

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

cmarshall




msg:3654785
 8:14 pm on May 20, 2008 (gmt 0)

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/".

httpwebwitch




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

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?

tomda




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

httpwebwitch is (always) right...

A/ Check if you are on local or host server
$ip=trim($_SERVER['REMOTE_ADDR']);
$array_test_ip = array("127.0.0.1"); // 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;
$xsl->load($URL_VDM_XSL);
$xp->importStylesheet($xsl);
$xml_doc = new DomDocument;
$xml_doc->load($URL_VDM_XML);

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


Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / XML Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved