tags:

views:

67

answers:

2

Hi there I have the following XML code:

<?xml version="1.0" encoding="UTF-8"?>
<connector_ret>
   <function name="search">
     <row id="1">
         <col id="1">AAA</col>
         <col id="2">243168</col>
         <col id="3">090828-000300</col>
         <col id="4">Subject</col>
     </row>
     <row id="2">
         <col id="1">BBB</col>
         <col id="2">243515</col>
         <col id="3">090831-000116</col>
         <col id="4">Subject</col>
     </row>
     <row id="3">
         <col id="1">BBB</col>
         <col id="2">244913</col>
         <col id="3">090905-000022</col>
         <col id="4">Subject</col>
     </row>
     <row id="4">
         <col id="1">CCC</col>
         <col id="2">245323</col>
         <col id="3">090907-000253</col>
         <col id="4">Subject</col>
     </row>
     <row id="5">
         <col id="1">CCC</col>
         <col id="2">245323</col>
         <col id="3">090907-000253</col>
         <col id="4">Subject</col>
     </row>
   </function>
</connector_ret>

I was wondering if it was possible to loop through this in PHP4 and only display certain rows when given a variable.

For example if the variable given is BBB then only display rows where the first column id is also equal to BBB.

So the output would be:

BBB - 243515 - 090831-000116 - Subject BBB - 244913 - 090905-000022 - Subject

and the other rows are just ignored.

If the variable given is AAA the output would be:

AAA - 243168 - 090828-000300 - Subject

You get the idea :)

If you can help that'd be fantastic. Thanks so much.

A: 

You might look at XSL-T as your solution to this. It's easy enough to select the nodes you want and transform the results.

Bernard Chen
Problem is the XML is generated through a third-party API and as such I'm only able to work with what the XML output I have.
Pete
@Bernard: XSL-T is available out of the box only in PHP5 and for doing such things plain `Xpath` is enough IMHO
RageZ
+2  A: 

you should use xpath to do such processing

$dom    =domxml_open_mem($xml); 
$calcX = &$dom->xpath_new_context();
$xml_parsed["match"]= node_content(
   $calcX->xpath_eval("//row/col[@id=1][text()='BBB']")
);
RageZ
Are there any prerequisites to this? I have a feeling IT have disabled the modules needed for this as PHP conks out as it runs the domxml_open_mem function.
Pete
yeah the domxml is a PECL extension, but on most modern distro (Ubuntu,debian,centos) you can install has a package, see http://www.php.net/manual/en/domxml.installation.php for PHP install
RageZ
Thanks for that. Is there any solution for this out of the box? I'm afraid trying to get IT to add any addition extensions might have issues. If not I guess that's the only way I can go about it.Thanks again!
Pete
that's not the only way but after doing some reg exp parsing of XML/HTML is evil, so you better to get your sysroot moving.
RageZ
Thanks Rage. I'll definitely try the push to move forward. Thanks for all your help!
Pete