Page 1 of 1

Using a PHP variable to filter XML

Posted: Wed Mar 01, 2006 11:59 am
by animus
Hello,

I have an issue that I cannot seem to resolve using XML and PHP.

Given:

The XML
http://www.aagrating.com/19space.xml

The XSL
http://www.aagrating.com/format.xsl

The CSS
http://www.aagrating.com/table.css


Given this setup I would like to be able to filter my data given a PHP variable (i.e. form submits, loads XML and filters based off of user input). I can use xpath to output the format to a table correctly, but when I attempt to set a variable for XPath it locks up.

the php script:
http://www.aagrating.com/xmlTest.txt

I have attempted to use "$myVar", '$myVar' " '+myVar+' ", but I cannot find the correct syntax. Is this even possible? Any thoughts?

Thanks in advance!

-thom

Posted: Wed Mar 01, 2006 1:48 pm
by feyd
the original php

Code: Select all

<?php
echo "<html><head>";
echo "<title>Untitled Document</title>";
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"table.css\" />";
echo "</head>";
  $s = simplexml_load_file('19space.xml');
  $myVar="19W4";   
   $bar = $s->xpath('/Root/product[type="$myVar"]');
   print $bar;
   foreach($bar as $type) {
   	echo "Found {$type->type} <br />";
	}
   echo "<body></body></html>";
?>
try

Code: Select all

<?php
echo "<html><head>";
echo "<title>Untitled Document</title>";
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"table.css\" />";
echo "</head>";
  $s = simplexml_load_file('19space.xml');
  $myVar="19W4";   
   $bar = $s->xpath('/Root/product[type="'.$myVar.'"]');
   print $bar;
   foreach($bar as $type) {
   	echo "Found {$type->type} <br />";
	}
   echo "<body></body></html>";
?>

thanks!

Posted: Wed Mar 01, 2006 2:31 pm
by animus
Thanks! that worked, I knew it had to be a syntax issue, I couldn't find the correct one for the life of me!


Thanks again!

-t

Posted: Thu Mar 02, 2006 2:23 pm
by vietboy505
Since this on the topic with XML & PHP.

I was wondering how can I parse XML into PHP so it will output a nice table format.

If it can parse in, and let user modify and output back as a XML file.