33559

Pass URL as a parameter to XSL

Question:

I'd like to pass current page URL as an attribute to XSL template. As far as I understood it should be passed as a parameter, and then used as an attribute.

I use PHP to load XML & XSL files:

<?php $xml = new DOMDocument; $xml->load('main.xml'); $xsl = new DOMDocument; $xsl->load('blocks/common.xsl'); $proc = new XSLTProcessor; $proc->importStyleSheet($xsl); echo $proc->transformToXML($xml); ?>

How should this code be altered to pass URL as a parameter named "current-url", for example?

I've seen a lot of similar questions here with different solutions, but none has worked for me so far. Thank you in advance.

Answer1:

Maybe you already tried this approach, but in case if not:

<?php $params = array('current-url' => $_SERVER['REQUEST_URI']); $xml = new DOMDocument; $xml->load('main.xml'); $xsl = new DOMDocument; $xsl->load('blocks/common.xsl'); $proc = new XSLTProcessor; $proc -> registerPHPFunctions(); $proc->importStyleSheet($xsl); foreach ($params as $key => $val) $proc->setParameter('', $key, $val); echo $proc->transformToXML($xml); ?>

In the xsl, add above the templates

<xsl:param name="current-url" />

In the templates, you can get the value using

<xsl:value-of select="$current-url" />

If not already there, you have to add xmlns:php="http://php.net/xsl" into the xsl:stylesheet declaration.<br /> For reference: <a href="http://php.net/manual/en/xsltprocessor.registerphpfunctions.php" rel="nofollow">registerPHPFunctions()</a> and a solution you maybe already checked on SO: <a href="https://stackoverflow.com/questions/865085/passing-variables-to-xslt" rel="nofollow">Passing variables to XSLT</a>

Recommend

  • php echo first divs
  • How to remove html part of a text in PHP
  • How to add nodes to a multi-level XML from an array?
  • finding maximum depth of chapter
  • PHP XPath. How to return string with html tags?
  • Duplicate Element x number of times with XSLT
  • File random access in J2ME
  • How to generate an asynchronous reset verilog always blocks with chisel
  • how to display   in Mozilla using XSL.
  • Python function to read variable length blocks of data from file while open
  • Less Conflicting Session Manager for Zope 2
  • XSLT foreach repeating nodes to flat
  • How to synchronize jQuery dialog box to act like alert() of Javascript
  • Does Mobilefirst provide a provision to access web services directly?
  • Play WS (2.2.1): post/put large request
  • Installing Hadoop, Java Exception about illegal characters at index 7?
  • Different response to non-authenticated users and AJAX calls
  • output of program is not same as passed argument
  • Deserializing XML into class C#
  • Upload files with Ajax and Jquery
  • Do I've to free mysql result after storing it?
  • Akka Routing: Reply's send to router ends up as dead letters
  • Is there a mandatory requirement to switch app.yaml?
  • Transpose CSV data with awk (pivot transformation)
  • Use group_by to filter specific cases while keeping NAs
  • A cron job substitute?
  • json Serialization in asp
  • log4net write single file for each call to log.info
  • Benchmarking RAM performance - UWP and C#
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • unknown Exception android
  • Checking variable from a different class in C#
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal