By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,712 Members | 1,541 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,712 IT Pros & Developers. It's quick & easy.

defaults and range checks from multiple xsd

P: 1

I have a weird requirement and since i have never worked on xml, xsd and python i am not understanding how to go about it.
There will be 2 xsd files containing parameters(structure), (may or maynot have)default values for them and range values for them. The xml file will contain values for these parameters and (may or maynot) have values for optional parameters.

So in the example file below, P_B.1 is an element, which has a value "1" in xml file. I need to get this value and i need to check whether the value fits into the range mentioned in both xsd1 and xsd2(which is the min and max values under restriction). If it fits then i need to put it into my global data structure i maintain to store these parameters. P_B.A1 is an attribute which is optional,so the value for it is not present in xml. In such cases i need to go and first pick up its default value in xsd1, if the value for the attribute is not present then go to xsd2 and pick its default value. Now consider, i picked up the default value from xsd1, after this i need to check whether the value i picked up fits the range(min and max under restriction for the element) in both xsd1 and xsd2 and if it correctly fits the range in both xsd, then fill the value into my global data structure.

I understand that tools like pyxbgen provides me bindings for the xsd files i have. But i am not able to visualize how i will be able to use them. my questions are:
1. is it better to parse both the xsd with simple parsers and then parse the xml content then create my datastructure, which is the brute force or the raw way. if so what are the good xsd parsers available in python?
2. Or is it a better idea to create bindings for both my xsd, then retrieving the parameter values from the xml file using both these xsd bindings. then generate a new xml filling up all the optional parameters avaiable with default values from the xsd. later, validate the new xml again with the bindings previously generated for the xsd files.
3. Or is there some other better way.

thanks in advance.
The sample xsd and xml is given below:

Expand|Select|Wrap|Line Numbers
  1. XSD:
  3. <?xml version="1.0" encoding="UTF-8"?>
  4. <xs:schema xmlns:xs="" elementFormDefault="qualified" attributeFormDefault="unqualified">
  5.     <xs:element name="P_ROOT">
  6.         <xs:annotation>
  7.             <xs:documentation>...</xs:documentation>
  8.         </xs:annotation>
  9.         <xs:complexType>
  10.             <xs:element name="P_B" minOccurs="0" maxOccurs="1500">
  11.                 <xs:annotation>
  12.                     <xs:documentation>....</xs:documentation>
  13.                 </xs:annotation>
  14.                 <xs:complexType>
  15.                     <xs:sequence>
  16.                         <xs:element name="P_B.1" type="xs:int" maxOccurs="5" use="required">
  17.                             <xs:annotation>
  18.                                 <xs:documentation>...</xs:documentation>
  19.                             </xs:annotation>
  20.                             <xs:restriction base="xs:int">
  21.                                 <xs:minInclusive value="1"/>
  22.                                 <xs:maxInclusive value="5"/>
  23.                             </xs:restriction>
  24.                         </xs:element>
  25.                         <xs:element name="P_B.2" type="xs:int" maxOccurs="5" use="required">
  26.                             <xs:annotation>
  27.                                 <xs:documentation>...</xs:documentation>
  28.                             </xs:annotation>
  29.                             <xs:restriction base="xs:int">
  30.                                 <xs:minInclusive value="1"/>
  31.                                 <xs:maxInclusive value="5"/>
  32.                             </xs:restriction>
  33.                         </xs:element>
  34.                     </xs:sequence>
  35.                     <xs:attribute name="P_B.A1" default="1" use="optional">
  36.                         <xs:annotation>
  37.                             <xs:documentation>...</xs:documentation>
  38.                         </xs:annotation>
  39.                         <xs:simpleType>
  40.                             <xs:restriction base="xs:int">
  41.                                 <xs:minInclusive value="1"/>
  42.                                 <xs:maxInclusive value="5"/>
  43.                             </xs:restriction>
  44.                         </xs:simpleType>
  45.                     </xs:attribute>            
  46.                 </xs:complexType>
  47.             </xs:element>
  48.         </xs:complexType>
  49.     </xs:element>
  50. </xs:schema>
  52. XML:
  53. <?xml version="1.0" encoding="UTF-8"?>
  54. <P_ROOT>
  55.     <P_B>
  56.         <P_B.1>1</P_B.1>
  57.         <P_B.2>2</P_B.2>
  58.     </P_B>
  59. </P_ROOT>
Apr 30 '13 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 2.5K+
P: 2,851
The example xsd file you posted appears to be valid xml except there is no opening tag for closing tag "</xs:sequence>". Was this a copy/paste error?

I have been using xml.dom.minidom for xml parsing for a long time, but there are several other modules for parsing xml including ElementTree.
May 1 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.