Question

  1. How to find if a node does not exist ? i am using

    if ( $item->branch()->siblings($tagNames['desc'])->text())

is there a better way ?

  1. is there a way to execute an OR query? if tag A exists get its text(), otherwise get B's text() ?

I am using the following :

 $desc1 = (  $item->branch()->siblings($tagNames['desc'])->text()  ?
$item->branch()->siblings($tagNames['desc'])   :
$item->branch()->siblings($tagNames['descAlternative']) ) ;

which doesn't look like the most efficient way of doing things.

Thanks

Was it helpful?

Solution

Regarding the first:

When QueryPath does not find a matching, it's size is 0. So you can do:

if (count($item) > 0) {
  // do whatever with $item
}

So for the second example, you could do:

if (count($item->branch()->siblings($tagNames['desc']))) {
    $item->branch()->siblings($tagNames['desc']);
    $item->branch()->siblings($tagNames['descAlternative']) ) ;
}

But there's another way: You can also pass two selectors in at once.

$item->branch()->siblings('desc, descAlternative')->text();

This will select both. However, text() will only return the text of the first matched item. So it has the effect of doing an OR.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top