python - Is it possible to get the type of an XML node as it was defined in XSD? -


i'm parsing xml in python. i've xsd schema validate xml. can type of particular node of xml defined in xsd?

for example, xml (small part) is

<devicedescription>   <wakeupnote>     <lang xml:lang="ru">Русский</lang>     <lang xml:lang="en">english</lang>   </wakeupnote>  </devicedescription> 

my xsd (once again small part of it):

<xsd:element name="devicedescription" type="zwv:devicedescription" minoccurs="0"/>  <xsd:complextype name="devicedescription">   <xsd:sequence>     <xsd:element name="wakeupnote" type="zwv:description" minoccurs="0">       <xsd:unique name="langdescrunique">         <xsd:selector xpath="zwv:lang"/>          <xsd:field xpath="@xml:lang"/>         </xsd:unique>     </xsd:element>    </xsd:sequence> </xsd:complextype>  <xsd:complextype name="description">   <xsd:sequence>     <xsd:element name="lang" maxoccurs="unbounded">       <xsd:complextype>         <xsd:simplecontent>           <xsd:extension base="xsd:string">             <xsd:attribute ref="xml:lang" use="required"/>           </xsd:extension>         </xsd:simplecontent>       </xsd:complextype>     </xsd:element>   </xsd:sequence>  </xsd:complextype> 

during parse want know tag wakeupnote defined in xsd complextype zwv:description. how (in python)?

what need for? suppose have lot of these xmls , want check of them have fields english language filled. easy check <lang xml:lang="en"></lang> empty, allowed not specify tag @ all.

so idea tags may have language descriptions , check <lang> tag present , has non-empty content en.

upd

since during validation xml checked against xsd, validation engine knows types of nodes. had similar question 7 month ago still no answer. related, imho. validating , filling default values in xml based on xsd in python

if question is: how find name of type given xml node? answer use xpath in python up. xpath run on xsd

//element[@name='wakeupnote']/@type 

this should return zwv:description. if returns 2 types, you'll have walk root

/root/foo/wakeupnote (type a) /root/bar/wakeupnote (type b) 

this tedious walking down root. you'll have both anonomous , named types.

if question is: how find xml nodes of given type? if schema change frequently, test type of every node parse above method.

if schema known, fixed, , nodes looking findable xpath test each node.

//@xml:lang='en' 

then use python check length of each.

in stable-schema case, write second xsd enforces criteria looking for.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -