简体   繁体   中英

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

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

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, eg:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM