using php, how do I access XML element's attributes declared by XML Schema declaration?

332 views Asked by At

I have a an xml file located at the URL specified, which includes a probability-of-precipitation node, which has several "value" elements whose schema-instance declaration(s) allows them to be nillable. However, the attributes() function in php does not show the XSI declarations of this element.

$feedURL= "http://forecast.weather.gov/MapClick.php?lat=32.78520&lon=-79.99400&FcstType=dwml";

// read feed into SimpleXML object
$wxml = simplexml_load_file($feedURL);

echo $wxml->data->parameters->{'probability-of-precipitation'}->value[0]->attributes();

Is it possible to print 'XSI attributes'?? Thanks

1

There are 1 answers

1
user377241 On

In the case of this $feedURL variable, the XML file passed includes an XML namespace directive which specifies that the namespace of the "XSI" prefix is referred to by the URI "http://www.w3.org/2001/XMLSchema-instance."

So, in order to access the attributes (@attributes) collection corresponding to the value[0] element, you need to specify this URI within the attributes function parameter value, e.g.:

print_r($wxml->data->parameters->{'probability-of-precipitation'}->value[0]->attributes('the_XSI_prefix's_URI'));

The above will output in your browser:

Nil,

if the xsi:nil="true" directive is passed in the current version of the http://forecast.weather.gov/MapClick.php?lat=32.78520&lon=-79.99400&FcstType=dwml XML file.