I have a page which has URL structure : http://www.abc.com/xyz?parama=1¶mb=2
Is is possible to create a generic method for getting the values of an
This might be possible using EXSLT, or registering a function callback with your particular xslt processor, but not with plain old XSLT, at least not to my knowledge. With plain XSLT, if it isn't in the XML, then it doesn't exist.
If it is client side, there isn't any way of doing this. Unfortunately, xsl is very limited in the browser. I'm afraid you will need to move the functionality to the webapp or to javascript.
If the transform is server side, there might be something you can do.
I workaround this by using javascript in mi XSLT file. take a look.
on The XML I have a tag element named tag! yes, very original...
<tag url="http://www.demo.net/share.php?u=param1 />
Mi Sample transform
<div class="HelloDiv">
<xsl:for-each select="tag">
<a href="{@url}">
This is my custom URL
</a>
</xsl:for-each>
</div>
Now inside the template of the transform, in going to load my custom value in Param1 in this case i'm going to use the document.title.. by using a jquery function.
<script type="text/javascript">
jQuery(function(){
jQuery('div.HelloDiv a').each(function(){
var parameter = jQuery(this).attr('href');
parameter = currentUrl.replace('param1',escape(document.title));
jQuery(this).attr('href',parameter);
})
});
</script>
Is is possible to get the URL of the page in XSL similar to javascript's location.href ?
Not exactly in the same way, but yes, the query string can be passed as a parameter.
Is is possible to create a generic method for getting the values of any additional params the URL maybe (parama=1¶mb=2)
Yes, one can perform tokenization (with the tokenize()
function in XSLT 2.0 or in XSLT 1.0 using the str-split-to-words
template of **FXSL 1.x or a self-written recursive tokenization template.)
XSLT 1.0 solution:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
>
<xsl:import href="strSplit-to-Words.xsl"/>
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:param name="pQString" select=
"'?parama=1&paramb=2&anyParamName=AnyValue'"
/>
<xsl:template match="/">
<xsl:variable name="vwordNodes">
<xsl:call-template name="str-split-to-words">
<xsl:with-param name="pStr" select="$pQString"/>
<xsl:with-param name="pDelimiters"
select="'?&'"/>
</xsl:call-template>
</xsl:variable>
<xsl:apply-templates select=
"ext:node-set($vwordNodes)/*
"/>
</xsl:template>
<xsl:template match="word">
<xsl:value-of select="."/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
when the above transformation is applied on any XML document (will not be used), the wanted result is produced:
parama=1
paramb=2
anyParamName=AnyValue
Do note the use of the FXSL 1.x str-split-to-words
template and the use of the EXSLT ext:node-set()
extension function.
XSLT 2.0 solution:
<xsl:param name="pQString" as="xs:string" select=
"'?parama=1&paramb=2&anyParamName=AnyValue'"
/>
<xsl:template match="/">
<xsl:value-of separator="
" select=
"tokenize($pQString, '\?|&')
"/>
</xsl:template>
When the above XSLT 2.0 transformation is performed, it produces the correct result:
parama=1
paramb=2
anyParamName=AnyValue