Re: [xml] How to select a node based on attribute values?



John Dennis wrote:

On Tue, 2006-11-28 at 16:57 -0800, Yan Seiner wrote:

Fist off, I'm a total newbie when it comes to libxml. I've been able to work my way through some examples, and produce working code, but I'm not entirely happy with the result, and I would like some advice.

I have an XML tree that looks like this:

<root>
   <plc id="1">
      <some stuff/>
   </plc>
   <plc id="2">
      <some other stuff/>
   </plc>
</root>

I have a relatively small number (< 50) of the plc nodes, but the whole tree is pretty big.

What is the best (or simplest way) to retrieve the information for plc 2?
xpath is the simplest and most powerful

Should I pull all of the plc elements using xmlXPathEvalExpression, and then iterate over them with xmlGetProp looking for an id attribute with a value of "2"?

no need to iterate, just create an xpath expression which specifies id=2

off the top of my head the expression would be "/plc[ id="2"]"

just google xpath syntax or xpath examples, you'll turn up a ton

OK, thanks a lot for the above!

I'm working with an embedded platform, so it took me a bit to get all of the required libxml stuff together and rebuild PHP with libxml support....

Now I have xpath working with PHP.   Yaay!

Ok, now for the PHP + xml question:

I am trying to get the base node of a schedule; I've attached a small portion of my XML file below. What I would like to do is to get the root of a schedule, and then iterate with foreach through all of the children. This is the function I came up with and of course it doesn't work. I don't understand how I am supposed to work with the object that the xpath_eval[_expression] returns. I've also tried the child_nodes method and that fails as well.

Hopefully there is a PHP expert on the list who can shed some light on this.

Thanks,

--Yan

   function loadSchedule($pivotID, $schedName, $schedNum) {
   $dom = domxml_open_file("/tmp/test.conf");
   $xpath = xpath_new_context($dom);
   // find base node for our schedule
   $pat = '//root/pivot[ id="' . $pivotID . '"]/'
       . $schedName . '[ id="' . $schedNum . '"]';
   print $pat;
   $baseNode = xpath_eval($xpath,$pat);
   var_dump($baseNode);
   $lines = xpath_eval_expression($xpath,'/line',$baseNode);
   var_dump($lines);
   print "\nOK\n";
   foreach($lines->nodeset as $line) {
       var_dump($line);
       }
   }

<root>
   <pivot id="1">
       <water id="1">
           <line id="1">
               <bearingTo>120.0</bearingTo>
               <WetDry>1</WetDry>
               <FwdRev>0</FwdRev>
               <event>0</event>
               <speed>10</speed>
           </line>
           <line id="2">
               <bearingTo>180.0</bearingTo>
               <WetDry>1</WetDry>
               <FwdRev>1</FwdRev>
               <event>0</event>
               <speed>45</speed>
           </line>
       </water>
   <pivot>
</root>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]