views:

36

answers:

1
<wsdl:definitions targetNamespace="http://www.webserviceX.NET/"&gt;
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://www.webserviceX.NET/"&gt;
<s:element name="ConversionRate">
<s:complexType>
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="FromCurrency" type="tns:Currency"/>
<s:element minOccurs="1" maxOccurs="1" name="ToCurrency" type="tns:Currency"/>
</s:sequence>
</s:complexType>
</s:element>
<s:simpleType name="Currency">
<s:restriction base="s:string">
<s:enumeration value="AFA"/>
<s:enumeration value="ALL"/>
<s:enumeration value="DZD"/>
<s:enumeration value="ARS"/>

I am trying to get at all of the elements in enumeration but can't seem to get it right. This is homework so please no full solutions, just guidance if possible.

$feed = simplexml_load_file('http://www.webservicex.net/CurrencyConvertor.asmx?WSDL');

foreach($feed->simpleType as $val){
    $ns s = $val->children('http://www.webserviceX.NET/');
    echo $ns_s -> enumeration;
}

What am I doing wrong?

thanks

+2  A: 

SimpleXML is very bad at namespace. The following works with PHP5.3:

$feedUrl = 'http://www.webservicex.net/CurrencyConvertor.asmx?WSDL';
$feed = simplexml_load_file($feedUrl);
$feed->registerXPathNamespace('s', 'http://www.w3.org/2001/XMLSchema');
foreach( $feed->xpath('//s:enumeration/@value') as $enumNode) {
    echo $enumNode, PHP_EOL;
}

print_r( $feed->getDocNamespaces() );

Another approach with less quirks would be to use the DOM extension:

$feed = new DomDocument;
$feed->load('http://www.webservicex.net/CurrencyConvertor.asmx?WSDL');
$nodes = $feed->getElementsByTagNameNS(
    'http://www.w3.org/2001/XMLSchema', 
    'enumeration');

foreach( $nodes as $node ) {
    echo $node->getAttribute('value'), PHP_EOL;
}
Gordon
Would it be possible to have a hint in how to do it my way as altugh posting working code is uesful for me to see whats going on, being a student means i dont know any other way of doing it really as i lack the experience, and obviously using the above would be classed as cheating - and rightfully so :D
csU
@csU well, the first thing would be to use the correct namespace. The s:enumaration elements are inside the XMLSchema namespace. So when iterating over the Feed Children, you want to limit yourself to those.
Gordon
XML is superior at namespace because it uses a lambda inheritance model, which is not compatible with the object oriented scope used in class based languages: C++, PHP, Java, and so forth.
so $ns_s = $val->children('http://www.w3.org/2001/XMLSchema');is this the correct namespace?
csU
@Csu Yes. if you do $feed->getDocNamespaces() you will see what prefixes are registered to what namespace. But like I said, using SimpleXML with namespace is a pain. Xpath or DOM is **much** easier and reliable.
Gordon
thanks for the help, i really appreciate itmaybe if i use the code you have posted, change to my needs and properly reference it in my code where i got it from - it should be ok?
csU
@csU well, you can reference if you want, but for a simple snippet like the above, I don't see the point of doing so? Just take it :) SO requires attribution though: http://blog.stackoverflow.com/2009/06/attribution-required/ but I guess that applies to the entire answer then, not just the code.
Gordon