1
votes

I have the following XML

$data = "<data>
  <element name='A'>value1</element>
  <element name='B'>value2</element>
  <element name='C'>value3</element>
  <element name='D'>value4</element>
  <element name='E'>value5</element>
</data>";

It's loaded as SimpleXML object in PHP.

Using the following line of code I can get all values as an array

$values = array_map( create_function( '$value', 'return (string) $value;' ), $data->xpath("data/element") );

print_r($values) gives

Array (
  [0] => value1
  [1] => value2
  [2] => value3
  [3] => value4
  [4] => value5
)

Is it possible to use the same method with a different xpath query to return values of all name attributes, without looping through array created with $data->xpath("data/element")?

1

1 Answers

0
votes

Use this XPath expression:

data/element/@name