How to read an Array of Element Names from XMLSchema XML File in PHP? -


i wanted list of schema elements xml. example

<xs:schema id="spponlinexml" xmlns="" xmlns:xs="http://www.w3.org/2001/xmlschema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">  <xs:element name="spponlinexml" msdata:isdataset="true" msdata:maindatatable="lev_settings" msdata:usecurrentlocale="true">   <xs:complextype>     <xs:choice minoccurs="0" maxoccurs="unbounded">       <xs:element name="lev_settings">         <xs:complextype>           <xs:sequence>             <xs:element name="levid" type="xs:int" minoccurs="0" />             <xs:element name="attheadid" type="xs:int" minoccurs="0" />             <xs:element name="mindatetype" type="xs:unsignedbyte" minoccurs="0" />             <xs:element name="minmonth" type="xs:unsignedbyte" minoccurs="0" />           </xs:sequence>         </xs:complextype>       </xs:element>     </xs:choice>   </xs:complextype>  </xs:element> </xs:schema>  

from above schema wanted create array of elements "levid","atthead","mindatetype","minmonth"

to obtain xs:element name-attribute values array suggest pick simplexml , make use of xpath. once you've got xml in string (or file) straight forward if know xpath expressions:

# obtain element names incl. complextypes:  //xs:element/@name  # obtain element names excl. complextypes , #  contain incl. comments, text etc.:  //xs:element[not(node())]/@name 

and per php code exmaple looks like:

$xml = simplexml_load_string($string);  echo "obtain element names incl. complextypes:\n";  $elementnames = array_map('strval', $xml->xpath('//xs:element/@name')); print_r($elementnames);  echo "\nobtain element names excl. complextypes ,   contain incl. comments, text etc.:\n";  $elementnames = array_map('strval', $xml->xpath('//xs:element[not(node())]/@name')); print_r($elementnames); 

see online demo.


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -