I want to get the URL parameter from the current page URL in my XSL and use it to test a condition in template xpath . Eg:
<xsl:apply-templates select="//search-results/orders/order[@id = URL Parameter]" mode="tableContent"/>
I do not have control over the XML as its generated by the tool which am using.
I tried using JavaScript to get the parameter from URL and saving it in js variable in the XSL , but am not able to proceed on getting this variable value in that above xpath condition. Not sure if i have adopted the right approach .
@dariom : I am storing the URL parameter in an input field on the same page and then getting that value in the below js code
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8"/>
<xsl:template match="/">
<script language = "javascript">
$( document ).ready(function() {
var trxid = document.getElementById("tempField").value;
console.log("trxid");
console.log(trxid);
});
</script>
<textarea><xsl:copy-of select="*"/></textarea>
<input type="hidden" id="countValue" class="countValue" value="{search-results/size}"/>
<xsl:apply-templates select="//search-results/orders/order[@id = trxid ]"/>
</xsl:template>
</xsl:stylesheet>
Update
I am using a closed source portlet based CMS Tool. This portlet generates the XML as output based on it's GUI configuration. And am using XSL to display the output.I do not have any further control over the portlet.
XSLT itself doesn't know that it's executing within the context of a web page, so you don't have direct access to a URL.
You can, however, pass parameters to an XSLT which is what it sounds like you're doing. Can you add examples of your JavaScript code and XSLT? From what you've described it sounds like you need to use an <xsl:param /> element instead of an <xsl:variable /> element.
Update
Based on the code you've added it looks like XSLT is generating an HTML page with JavaScript built into it. The thing is that your JavaScript runs after the XSLT has created it's output. You can't use the value of the trxid JavaScript variable in XSLT because it hasn't been defined yet (and you can't mix XSLT and JavaScript like that).
To achieve what you need to do you'll need to pass in the URL parameter value into your XSLT processor (assuming it supports this), create an <xsl:param> element to contain the value of your parameter and then you can use the parameter in your XPath expressions.
Something like:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
...
<!-- defintion of your XSLT parameter. Note this has a global scope -->
<xsl:param name="trxid" />
...
<!-- usage of your XSLT parameter. Note the '$' prefix -->
<xsl:apply-templates select="//search-results/orders/order[@id = $trxid ]"/>
</xsl:template>
</xsl:stylesheet>
The key thing will be figuring out how to invoke your XSLT processor and supply it with values for your parameters. I'd suggest updating your answer with details of the language you're coding in and the XSLT processor you're using.
Update
I've searched for a bit more information about Unify. I have no idea whether the information I've found is correct for your context (it seems that there are many modules in Unify) but this seems to be what you're after: request parameters in XSL.
So, my updated example would become (assuming that your query string parameter is named 'trxid'):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
...
<xsl:param name="request_trxid" />
...
<!-- usage of your XSLT parameter. Note the '$' prefix -->
<xsl:apply-templates select="//search-results/orders/order[@id = $request_trxid]"/>
</xsl:template>
</xsl:stylesheet>
If this works, you don't have to add JavaScript code and your hidden field.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With