Jump to content


Photo

Filter XML using a PHP variable


  • Please log in to reply
1 reply to this topic

#1 animus

animus
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 01 March 2006 - 05:37 PM

Hello,

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

Given:

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

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

The CSS
[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!

-thom



#2 Collin

Collin
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 01 March 2006 - 08:45 PM

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

<?php
  $s = simplexml_load_file('19space.xml');
  $myVar="19W4";   
   $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 />";
    }
   }
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users