Hi,
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.
The sample xsd and xml is given below.
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:
- 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?
- 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.
- Or is there some other better way.
Thanks in advance.
XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="P_ROOT">
<xs:annotation>
<xs:documentation>...</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:element name="P_B" minOccurs="0" maxOccurs="1500">
<xs:annotation>
<xs:documentation>....</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element name="P_B.1" type="xs:int" maxOccurs="5" use="required">
<xs:annotation>
<xs:documentation>...</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:int">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="5"/>
</xs:restriction>
</xs:element>
<xs:element name="P_B.2" type="xs:int" maxOccurs="5" use="required">
<xs:annotation>
<xs:documentation>...</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:int">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="5"/>
</xs:restriction>
</xs:element>
</xs:sequence>
<xs:attribute name="P_B.A1" default="1" use="optional">
<xs:annotation>
<xs:documentation>...</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:int">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="5"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:complexType>
</xs:element>
</xs:schema>
XML:
<?xml version="1.0" encoding="UTF-8"?>
<P_ROOT>
<P_B>
<P_B.1>1</P_B.1>
<P_B.2>2</P_B.2>
</P_B>
</P_ROOT>