Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

retrieving which choice has been filled in in xml document?

Help
2013-09-05
2013-09-06
  • Paul Fennema
    Paul Fennema
    2013-09-05

    Goodmorning,

    I am new to pyXB and the format please accept my apologies if I ask a 'stupid' question, but here goes:
    When I have a xml document which containts a 'choice' I'd like to know in my corresponding python object which is actually filled in, the python object is filled in by means of:

    xmlObject = xmlObject.CreateFromDocument(file('XML_Example.xml').read() )
    

    And the xml document contains this choice:

    <xs:element name="reportingOperations">
    <xs:complexType>
    <xs:choice maxOccurs="unbounded">
      <xs:element name="reportingOperation" type="reportingOperation" />
      <xs:element name="reportingFullTermination" type="reportingFullTermination" />
                <xs:element name="reportingTradeTermination" type="reportingTradeTermination" />
                <xs:element name="reportingBackloading" type="reportingBackloading" />
                <xs:element name="reportingModification" type="reportingModification" />
                <xs:element name="reportingValuationUpdate" type="reportingValuationUpdate" />
                <xs:element name="reportingCollateralUpdate" type="reportingCollateralUpdate" />
            </xs:choice>
        </xs:complexType>
    </xs:element>
    

    What I'd like know how to check which choice of reportingOperations is actually filled in, something like:

    if xmlObject.reportingOperations == reportingOperation:
        # Do something with the variables filled in when it is a reportingOperation
    else if xmlObject.reportingOperations == reportingFullTermination:
        # Do something with the variables filled in when it is a reportingFullTermination
    else if ...
    

    How can I check this?

    Thanks Paul

     
  • Peter A. Bigot
    Peter A. Bigot
    2013-09-05

    The instance object will have Python attributes (properties, actually) for each of the candidate fileselements. They will all be None except the one that has been assigned. So your check would be:

    if xmlObject.reportingOperation is not None:
        # Do something with the variables filled in when it is a reportingOperation
    else if xmlObject.reportingFullTermination is not None:
        # Do something with the variables filled in when it is a reportingFullTermination
    else if ...
    

    As you manipulate the others you are free to assign to them, but the resulting object will not validate until exactly one of them has been assigned a non-None value.

    The information may also be retrieved from the ordered content method though that's more complex.

     
    Last edit: Peter A. Bigot 2013-09-05
    • Paul Fennema
      Paul Fennema
      2013-09-06

      Hi Peter,

      Thanks a lot! That is indeed how I solve it right now, but thought there would be some sort of attribute in the reportingOperations which gave me the part what was filled in. This works fine however.

      Best regards, Paul

       
  • Peter A. Bigot
    Peter A. Bigot
    2013-09-06

    As I look more closely, my recommended check for None is wrong, since the choice model group in your schema allows unbounded occurrences and each of the contained elements is actually represented as a list, and the correct check is whether the list is empty.

    This also shows why there's no way in general to specify which part was filled in: with multiple occurrences any or all of them could be filled in, multiple times, in arbitrary order. In that case you need orderedContent to reconstruct the order; PyXB normally doesn't care since the content model doesn't restrict the order.