Jump to content

Filter XML using a PHP variable


animus

Recommended Posts

Hello,

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

[b]Given:[/b]

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

Link to comment
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....

[code]<?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 />";
    }
   }
?>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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