Solprovider Lenya Kongregate Registration for Free Flash Games and Chat with solprovider

Sitemaps & QueryStrings

This article is mostly here for historical interest (meaning I hate discarding possibly useful code.) It provides a good example using QueryString parameters in an XMAP (Sitemap). The intended result is to have a document displayed using different stylesheets. The process in Multiple Stylesheets is cleaner, more functional without custom coding, and easier to maintain.

Usable Solution


Make the change in "{pub}/publication-sitemap.xmap":
<map:match pattern="lenyabody-*/*/*/*/**">
...
<map:transform src="xslt/page2xhtml-homepage.xsl">
<map:parameter name="root"
value="{page-envelope:context-prefix}/{2}/{3}"/>
<map:parameter name="url" value="{5}"/>
<map:parameter name="document-id"
value="{page-envelope:document-id}"/>
<map:parameter name="document-type"
value="{page-envelope:document-type}"/>
<map:parameter name="language"
value="{page-envelope:document-language}"/>
</map:transform>


Change the transform to:
<map:select type="parameter">
<map:parameter name="parameter-selector-test"
value="{request-param:style}"/>
<map:when test="myStyleName">
<map:transform src="xslt/page2xhtml-MyStylesheet.xsl">
<map:parameter name="root"
value="{page-envelope:context-prefix}/{2}/{3}"/>
<map:parameter name="url" value="{5}"/>
<map:parameter name="document-id"
value="{page-envelope:document-id}"/>
<map:parameter name="document-type"
value="{page-envelope:document-type}"/>
<map:parameter name="language"
value="{page-envelope:document-language}"/>
</map:transform>
</map:when>
<map:otherwise>
<map:transform src="xslt/page2xhtml-{4}.xsl">
<map:parameter name="root"
value="{page-envelope:context-prefix}/{2}/{3}"/>
<map:parameter name="url" value="{5}"/>
<map:parameter name="document-id"
value="{page-envelope:document-id}"/>
<map:parameter name="document-type"
value="{page-envelope:document-type}"/>
<map:parameter name="language"
value="{page-envelope:document-language}"/>
</map:transform>
</map:otherwise>
</map:select>


Add your extra XML as filename (must match:
{pub}/xslt/page2xhtml-MyStylesheet.xsl

Add ?style=1 to the URL
http://127.0.0.1:8888/default/live/news.html?style=MyStyleName

You can add more map:when sections with different parameter-names and filenames.

A solution that does not work


The above solution overrides the XSL used for display. A more Lenya-oriented solution is to change the doctype of the document. Unfortunately, Lenya caches the doctype for each target document so it is only decided once. That means this method of overriding the doctype cannot be used for dynamically changing the display. It is here for historical purposes (meaning I hate deleting anything).

I will update this article if a method for turning off the caching of doctypes is discovered.

FILE: parameter-doctype.xmap
Change the <map:match pattern="*/**.html"> section to:

<map:match pattern="*/**.html">
<map:select type="parameter">
<map:parameter name="parameter-selector-test"
value="{request-param:style}"/>
<!--
<map:select type="request-parameter">
<map:parameter name="parameter-name" value="style"/>
-->
<map:when test="1">
<map:generate type="serverpages"
src="../../config/parameters/default.xsp">
<map:parameter name="value" value="zzz"/>
</map:generate>
</map:when>
<map:otherwise>
<map:act type="sourcetype"
src="content/{1}/{page-envelope:document-path}">
<map:generate type="serverpages"
src="../../config/parameters/default.xsp">
<map:parameter name="value" value="{sourcetype}"/>
</map:generate>
</map:act>
</map:otherwise>
</map:select>
<map:serialize type="xml"/>
</map:match>


If you are only using this for testing, you can hit different pages
with different style=x, and each will appear with the specified stylesheet.

Comparing the two solutions


Ignoring the "caching of doctypes" issue, the two solutions are functionally identical. They each add a select around a function call. Both require the same amount of processing. The first has a few more lines of code, because the format suggests each parameter to the function call be placed on a separate line, and the transform function has more parameters than the generateConfig function; most other programming languages would do it with one line of code..

The goal is to rewrite the line:
<map:transform src="xslt/page2xhtml-{4}.xsl">
so {4} is configurable.
- The first solution does it by copying the line and hardcoding a value.
- The second solution accepts {4} is already configurable, and adds the test where it is decided.

* First Solution *
doctype =getDoctype() ;
if(style=="1"){
transform("zzz");
}else{
transform(doctype);
}
function getDoctype{
return this.doctype;
}


* Second Solution *
doctype =getDoctype() ;
transform(doctype);

function getDoctype{
if(style=="1"){
return "zzz";
}else{
return this.doctype;
}


The first overrides doctype where doctype is used.
The second decides doctype where doctype is decided.

MAINTENANCE CONSIDERATION: If doctype was used anywhere else, and you want it to change for everything, it would be much cleaner to set it in getDoctype() rather than every place it is used.

PERFORMANCE CONSIDERATION: Since doctype is only used once, it is possible to optimize version 2 by moving the "doctype =getDoctype()" inside the if. For this code, that requires changing other functions to stop passing it as a parameter. That means changing even more code (chancing more bugs) and changing the API (breaking other calls to the function).

There is a function for getDoctype(). The goal is to change the doctype. That is the logical place to do it -- unless doctype needs to be set according to the usual rules, and only the display is overridden. Then the first solution is the only one that meets the goals.

Which solution to use should depend on what value doctype should have in other functions, but since the second version has a bug avoided in the first version, use the first.


<< Multiple StylesheetsSecurity >>

Contact Solprovider
Paul Ercolino