Jump to content

GET Arrays using XML, XSL, PHP


kvnmck18

Recommended Posts

Below is my PHP for parsing the XSL and the XML, works fine... but I cannot get a $_GET array work. Within the XSL I have checkboxes that become checked if the plan param is equal to the current node.Using the URL below should check the checkboxes for "plan 42", "plan 933", and "plan 37"?id=00314&plan[]=42&plan[]=933&plan[]=37^^^^^^^^^^^^^^^^^^^^^^^^^^^^^But this does not work, I receive this error "Warning: Wrong parameter count for XSLTProcessor::setParameter() in..."?id=00314&plan=37 <-- this works for one check box... but making it an array does not.How can I fix this?

	<?php$id= $_REQUEST['id'];///////////////////////////$plan = $_GET['plan'];//$plan = array("42","933","37");//print_r($plan);///////////////////////////				$xsltFile = "xsl.xsl";				$xmlFile = "xml.xml";				$xml = new DomDocument;				$xml->load($xmlFile);				$xsl = new DomDocument;				$xsl->load($xsltFile);				$xslt = new Xsltprocessor;				$xslt->importStylesheet($xsl);				$xslt->setParameter(NULL, 'id', $id);								//////////////////////////////////////////				$xslt->setParameter(NULL, 'plan', $plan);				//////////////////////////////////////////				$transformation = $xslt->transformToXml($xml);				echo $transformation;	?>

Link to comment
Share on other sites

XSLT parameters can only be strings. XSLT doesn't have the notion of arrays.What you can do instead is to pass a DOMDocument object. But it's not exactly as easy as with plain XSLT parameters.First, you need to convert your array into a DOMDocument object, and actually do that from a function like:

function array2dom($of) {$planDom = new DOMDocument;$planDomRoot = $planDom->loadXML('<params/>')->documentElement; //Create the root node, and select it./* Loop over all items in the array named like the $of variable and turn them into an element we're then going to append to the root node. */foreach(${$of} as $n) {$planDomRoot->appendChild($planDom->createElement('param', $n));}return $planDom;}

the callarray2dom('plan');should return (based on the above example) a DOMDocument object representing the XML:

<params><param>42</param><param>933</param><param>37</param></params>

In your PHP, you also need to make the call

$xslt->registerPHPFunctions('array2dom');

and then, in XSLT you need to use that PHP function with something like (the following is only an example of how it works):

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" 	 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"	 xmlns:php="http://php.net/xsl"><!-- notice the PHP namespace. It must be present for the function below to work --><xsl:template match="/"><xsl:value-of select="php:function('array2dom','plan')/params/param[1]"/><!-- With your above array, this should be the answer to life, the universe and everything: 42 --></xsl:template></xsl:stylesheet>

Not that you'll care, but this approach descreases portability, as its only available in PHP 5. No possibility to go to PHP4's Salbotron, or ASP, or any other S3L, or use PHP with any other XSLT processor.

Link to comment
Share on other sites

I can't seem to get this work.

$plan = array("27113210059000000","23533210059000000","27613210059000000");function array2dom($n) {$planDom = new DOMDocument;$planDomRoot = $planDom->loadXML('<params/>')->documentElement;foreach ($plan as $n) {			$planDomRoot->appendChild($planDom->createElement('param', $n));		}		return $planDom;}

I don't get your use of ${$of}?? The array should be the first part of the foreach....

Link to comment
Share on other sites

The ${$*} syntax is the variable variable syntax. It allows you to call any variable by a name that is otherwise specified as a string.In the function that I've made you, the callarray2dom('plan');would mean the script finds a variable called "plan" and uses its contents as the value for the foreach loop.The alterations you've made are totally unacceptable. They completely eliminate the logic behind the script.I've made the function like I did for two reasons:1. Make it reusable. Instead of creating a new function for each future array you'd like to pass, you'll just pass the name of array to this function, and get its DOM representaion.2. As said, XSLT doesn't have arrays. This function needs to be usable from XSLT, so to do that, it can't use PHP variable references, it can't use arrays... It can only use scalar values (strings, numbers, booleans). Since a variable name usually contains letters, it's only logical to use a string.Just, try this PHP out:

<?php$myArray = array('a','b','c');function array2dom($of) {$planDom = new DOMDocument;$planDom->loadXML('<params/>');$planDomRoot = $planDom->documentElement;foreach(${$of} as $n) {$planDomRoot->appendChild($planDom->createElement('param', $n));}return $planDom;}var_dump(array2dom('myArray')->saveXML());?>

The output of this should be:

<params><param>a</param><param>b</param><param>c</param></params>

Oddly enough, this does NOT work for my PHP 5.2.5 setup, though according to the documentation, it should.Oh well, in the worst case scenario, you could take the variables directly from the GET, and do the sanitizing in XSLT, or in the array2dom function. So, if the above doesn't work, try this one:

$_GET['plan'] = array('a', 'b', 'c');function array2dom($of) {$planDom = new DOMDocument;$planDom->loadXML('<params/>');$planDomRoot = $planDom->documentElement;foreach($_GET[$of] as $n) {$planDomRoot->appendChild($planDom->createElement('param', $n));}return $planDom;}var_dump(array2dom('plan')->saveXML());

At least that one worked for me.Once this function works, go to the next steps from my previous post.

Link to comment
Share on other sites

Bottom one works. That's odd.... because foreach shouldn't be the problem, but I guess it's a bug of some-sort.Nice solution!!! You are the god.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...