In these examples, $xml can be either a string or an object. $selector should be a string, e.g. "child". We wrap $xml in xml_field_xml() to make sure that we have an xml object.

Iterating over attributes

  if (xml_field_has($xml, $selector)) {
    $attributes = xml_field_xml($xml)->{$selector}->attributes();
    foreach (array_keys($attributes) as $name) {
      ...
    }
  }

Iterating over children

  if (xml_field_has($xml, $selector)) {
    $attributes = xml_field_xml($xml)->{$selector}->children();
    foreach (array_keys($attributes) as $name) {
      ...
    }
  }