Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing variables to XSLT
    text
    copied!<p>I am new to XML/XSL. I want to be able to pass a var in a rule string and have that return the correct data.</p> <p>Right now I have have this PHP:</p> <pre><code>&lt;?php $params = array('id' =&gt; $_GET['id']); $xslDoc = new DOMDocument(); $xslDoc-&gt;load("test.xsl"); $xmlDoc = new DOMDocument(); $xmlDoc-&gt;load("test.xml"); $xsltProcessor = new XSLTProcessor(); $xsltProcessor-&gt;registerPHPFunctions(); $xsltProcessor-&gt;importStyleSheet($xslDoc); foreach ($params as $key =&gt; $val) $xsltProcessor-&gt;setParameter('', $key, $val); echo $xsltProcessor-&gt;transformToXML($xmlDoc); ?&gt; </code></pre> <p>My xml file looks like this:</p> <pre><code>&lt;Profiles&gt; &lt;Profile&gt; &lt;id&gt;1&lt;/id&gt; &lt;name&gt;john doe&lt;/name&gt; &lt;dob&gt;188677800&lt;/dob&gt; &lt;/Profile&gt; &lt;Profile&gt; &lt;id&gt;2&lt;/id&gt; &lt;name&gt;mark antony&lt;/name&gt; &lt;dob&gt;79900200&lt;/dob&gt; &lt;/Profile&gt; &lt;Profile&gt; &lt;id&gt;3&lt;/id&gt; &lt;name&gt;neo anderson&lt;/name&gt; &lt;dob&gt;240431400&lt;/dob&gt; &lt;/Profile&gt; &lt;Profile&gt; &lt;id&gt;4&lt;/id&gt; &lt;name&gt;mark twain&lt;/name&gt; &lt;dob&gt;340431400&lt;/dob&gt; &lt;/Profile&gt; &lt;Profile&gt; &lt;id&gt;5&lt;/id&gt; &lt;name&gt;frank hardy&lt;/name&gt; &lt;dob&gt;390431400&lt;/dob&gt; &lt;/Profile&gt; &lt;/Profiles&gt; </code></pre> <p>And my xsl looks like this</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:param name="id" /&gt; &lt;xsl:template match="*"&gt; &lt;html&gt;&lt;body&gt; &lt;h2&gt;Profile&lt;/h2&gt; &lt;table cellspacing="1" cellpadding="5" border="1"&gt; &lt;caption&gt;User Profiles&lt;/caption&gt; &lt;tr&gt;&lt;th&gt;ID&lt;/th&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Date of Birth&lt;/th&gt;&lt;/tr&gt; &lt;xsl:for-each select="/Profiles/Profile[id='$id']"&gt; &lt;tr&gt; &lt;td&gt;&lt;xsl:value-of select="id"/&gt;&lt;/td&gt; &lt;td&gt;&lt;xsl:value-of select="php:function('ucwords', string(name))"/&gt;&lt;/td&gt; &lt;td&gt;&lt;xsl:value-of select="php:function('date', 'jS M, Y', number(dob))"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/xsl:for-each&gt; &lt;/table&gt; &lt;/body&gt;&lt;/html&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>When I test the url like this:</p> <pre>http://foo.com/sanbox/index.php?id=2</pre> <p>I only get:</p> <pre> Profile User Profiles ID Name Date of Birth. </pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload