Using a PHP variable to filter XML

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
animus
Forum Newbie
Posts: 2
Joined: Wed Mar 01, 2006 10:18 am

Using a PHP variable to filter XML

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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>";
?>
animus
Forum Newbie
Posts: 2
Joined: Wed Mar 01, 2006 10:18 am

thanks!

Post 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
vietboy505
Forum Commoner
Posts: 53
Joined: Wed Feb 22, 2006 9:30 am

Post 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.
Post Reply