Jump to content


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


Filter XML using a PHP variable

Recommended Posts


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


[a href=\"http://www.aagrating.com/19space.xml\" target=\"_blank\"]19space.xml[/a]

[a href=\"http://www.aagrating.com/format.xsl\" target=\"_blank\"]http://www.aagrating.com/format.xsl[/a]

[a href=\"http://www..agrating.com/table.css\" target=\"_blank\"]http://www..agrating.com/table.css[/a]

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:[a href=\"http://www.aagrating.com/xmlTest.txt\" target=\"_blank\"]xmlTest.php[/a]

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

Thanks in advance!


Share this post

Link to post
Share on other sites
As far as I know you cannot use variables in Xpath expressions (or at least I have never gotten it to work). A nice tutorial on expath expressions: [a href=\"http://www.zvon.org/xxl/XPathTutorial/General/examples.html\" target=\"_blank\"]http://www.zvon.org/xxl/XPathTutorial/General/examples.html[/a]

What you can do is look for all elements with the "type" tag, and go through them all and only show those that have the value of "$myVar".

I don't have any experience with simpleXml stuff, but I hope you get the idea if the syntax is not really correct...

Hope it helps....

  $s = simplexml_load_file('19space.xml');
   $products = $s->xpath('//product'); //search for all "product" elements
   foreach($products as $product) //go through list of all products
           //if the type of $product matches user input (ie $myVar)
       if ($product->type == $myVar)
           echo "Found {$product->type} <br />";

Share this post

Link to post
Share on other sites

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.