0

I have one XML file:-

<products>
   <product_id value="1">
       <tab_id value="251">
            <dist_region value="5" />
            <dist_region value="10066" />
            <dist_region value="10069" />
       </tab_id>
   </product_id>
</products>

I am use this xpath:-

list($prid) = $product->xpath("//dist_region[@value]/@value");
    $prid=(string)$prid;
    if(strlen(trim($prid))==0)
    {
        echo ' Is not Match In Product';
        exit;
    }
    echo $prid;

and get the output:-

5

I want to output like:- all dist_region like:-

5,10066,10069

how can I get this?

Michael Irigoyen
  • 22,513
  • 17
  • 89
  • 131
Jack Php
  • 577
  • 2
  • 7
  • 25
  • 1
    You don't need the predicate, `//dist_region/@value` will yield the exactly same result. Path steps just don't return anything if there is no result. You've been writing something like "select all `` elements _which have an attribute `@value`_, and select all attributes `@value` if there are any" which is redundant. – Jens Erat May 20 '13 at 10:09
  • Please ask a question. What is the programming problem you run into. Yes it all works with programming and PHP has array and string functions however it's not clear why you ask and what your concrete quesiton is. Same for the other, every similar questions you ask. – hakre May 20 '13 at 14:41

2 Answers2

3

You have to loop through your results:

$result = $product->xpath('//dist_region[@value]/@value');
$result_arr = array();

if(!empty($result)) { 
    foreach($result as $prid) {
        $prid = (string) $prid;
        if(strlen(trim($prid)) > 0) {
            $result_arr[] = $prid;
        }
    }
}

echo implode(',', $result_arr);
stefandoorn
  • 1,032
  • 8
  • 12
2

According to the answer you had accepted, it looks like you're looking for all non-empty value-attributes. But different to what was answered, you can actually spare the loop by formulating the xpath expression differently:

$expression = '//dist_region/@value[normalize-space(.)]';
$values     = $product->xpath($expression);

echo implode(',', $values);

That xpath expression means: Query all value-attribute-nodes of any dist_region-element-nodes which have a non-empty (non-zero-length) normalized string value (that is similar to trim() in PHP and perfectly fitting here if you would have needed trim).

As this example shows, there is no need to actually loop through the results, in SimpleXML you can return attribute-nodes from an xpath query in form of an array and easily use them in string context (cast them to string), for example by using the implode() function.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • if i want only tag name than what i do liek `Product_id,tab_id,dist_region,dist_region,dist_region` – Jack Php May 21 '13 at 10:41
  • You need to find out what qualifies these and then formulate a more abstract goal. Getting the tagname from an element in a simplexml xpath result array is http://php.net/simplexmlelement.getName - IIRC I outlined this already on a similar question of yours in form of a comment. Just look around a bit, it's nothing magic. – hakre May 21 '13 at 10:43
  • i am try this nothing display on output. `$products = $xml->xpath("//product_id"); foreach ($products as $product) { print($product->children()); }` – Jack Php May 21 '13 at 11:18
  • @JackPhp: Before you continue to guess for too long (and wondering), check this important notice on PHP errors: [How to get useful error messages in PHP?](http://stackoverflow.com/q/845021/367456) - And also please understand that this website here is not good for individual problems - better abstract your problem into a good programming questions, that way you will learn better & more and then actually use this website much better. – hakre May 21 '13 at 11:21