Menu

Getting the value of “grandchild” element

Help
Moo Nam Ko
2018-11-07
2018-11-09
  • Moo Nam Ko

    Moo Nam Ko - 2018-11-07

    My service provider gave me data using XML and XSD files and I'm trying to parse this data using PYXB library in Python.

    I can access the root and child elements and attributes except for the grandchild element. I'd like to know how I can access the value of grandchild element(GenderValue).

    Here is a part of xml file:

    <Person id="10441" recordaction="add" date="10-Jul-2018">
      <GenderDetails>
        <Gender>
          <ReferenceGroup ReferenceGroupCode="DJ"/>
          <GenderValue>Male</GenderValue>
        </Gender>
      </GenderDetails>
    

    Here is my python test code and result:

    >>> for person in dj.Records.Person:
    ...     if person.GenderDetails is not None:
    ...         if person.GenderDetails.Gender is not None:
    ...             print(person.GenderDetails.Gender)
    ...             print(person.GenderDetails.Gender.GenderValue)
    ... 
    [<schema_DJ.CTD_ANON_14 object at 0x11b97afd0>]
    Traceback (most recent call last):
      File "<stdin>", line 5, in <module>
    AttributeError: '_PluralBinding' object has no attribute 'GenderValue'
    

    The result shows there are Gender object is in the memory and AttributeError. I don't know why it searched the attribute instead of the element. Thank you for your help in advance.

     

    Last edit: Moo Nam Ko 2018-11-07
  • Peter A. Bigot

    Peter A. Bigot - 2018-11-07

    A couple things are odd. First, you're referencing GenderDetails but the XML has it spelled genderdetails. Probably one or the other is not what's actually being used. The bigger clue is the reference to PluralBinding which suggests that the Gender element can appear multiple times (which is also a little odd), in which case it's like a list/array and you have to pick which of the elements you want to look at. Or you're spelling the element name wrong.

    At any rate there isn't enough context here to figure out what's going on. Look at the schema more closely, especially for maxOccurrences greater than one. If you have to you can open a ticket on github but I don't provide much free support for PyXB anymore so if it isn't clear that it's a bug or obvious what the problem is I won't be able to help you.

     
  • Moo Nam Ko

    Moo Nam Ko - 2018-11-07

    Hello Peter,

    Thank you for reply. I reivsed the code using code tag. I first time use the sourceforge, so I did the mistake when I posted the code. I hope the revised one is much better to read.

    I validated the the xml file using xsd, so the xml file is ok with the schema. The max occurance is 1.

    I have two questions.

    1. Is my code the rigth way to access the grandchild elemenet if the xml and xsd are correct? Is there other way to access it?
    person.GenderDetails.Gender.GenderValue
    
    1. Could you explain more detail about the _PluralBinding? Any reference documents?

    Thank you for your help again!

     
  • Peter A. Bigot

    Peter A. Bigot - 2018-11-07

    If the schema shows GenderValue as a non-plural element within whatever type Gender is the code you show should work. Without seeing the schema I can't tell.

    The _PluralBinding material is internal to PyXB; some of its visible markers are described here.

     
    • Moo Nam Ko

      Moo Nam Ko - 2018-11-08

      GenderValue part of the schema is

      <xs:element name="Person">
              <xs:complexType>
                  <xs:sequence>
                      <xs:element ref="GenderDetails" minOccurs="0"/>
                      <xs:element ref="ActiveStatusDetails" minOccurs="0"/>
                      <xs:element ref="DeceasedDetails" minOccurs="0"/>
                      <xs:element ref="PersonNameDetails" minOccurs="0"/>
                      <xs:element ref="Descriptions"/>
                      <xs:element ref="DateDetails" minOccurs="0"/>
                      <xs:element ref="BirthPlaceDetails" minOccurs="0"/>
                      <xs:element ref="SanctionsReferences" minOccurs="0"/>
                      <xs:element ref="AddressDetails" minOccurs="0"/>
                      <xs:element ref="CountryDetails" minOccurs="0"/>
                      <xs:element ref="IdentificationDetails" minOccurs="0"/>
                      <xs:element ref="SourceDetails" minOccurs="0"/>
                      <xs:element ref="ProfileNotes" minOccurs="0"/>
                  </xs:sequence>
                  <xs:attribute name="id" type="xs:string" use="required"/>
                  <xs:attribute name="date" use="required">
                      <xs:simpleType>
                          <xs:restriction base="xs:string">
                              <xs:maxLength value="12"/>
                          </xs:restriction>
                      </xs:simpleType>
                  </xs:attribute>
                  <xs:attribute name="recordaction" use="required">
                      <xs:simpleType>
                          <xs:restriction base="xs:NMTOKEN">
                              <xs:maxLength value="9"/>
                              <xs:enumeration value="add"/>
                              <xs:enumeration value="chg"/>
                              <xs:enumeration value="del"/>
                              <xs:enumeration value="rev"/>
                              <xs:enumeration value="unchg"/>
                          </xs:restriction>
                      </xs:simpleType>
                  </xs:attribute>
              </xs:complexType>
          </xs:element>
          <xs:element name="GenderDetails">
              <xs:complexType>
                  <xs:sequence>
                      <xs:element ref="Gender" minOccurs="1" maxOccurs="unbounded"/>
                  </xs:sequence>
              </xs:complexType>
          </xs:element>
          <xs:element name="Gender">
              <xs:complexType>
                  <xs:sequence>
                      <xs:element ref="ReferenceGroup" minOccurs="1" maxOccurs="unbounded"/>
                      <xs:element ref="GenderValue" minOccurs="1" maxOccurs="1"/>
                  </xs:sequence>
              </xs:complexType>
          </xs:element>
          <xs:element name="GenderValue">
              <xs:complexType>
                  <xs:simpleContent>
                      <xs:extension base="GenderContentRestriction">
                          <xs:attribute ref="fieldaction" use="optional"/>
                      </xs:extension>
                  </xs:simpleContent>
              </xs:complexType>
      
       
  • Peter A. Bigot

    Peter A. Bigot - 2018-11-08

    The schema shows there are an unbounded number of Gender elements within the containing GenderDetails element; i.e. Gender is plural, as I originally suggested. So you probably need person.GenderDetails.Gender[0].GenderValue.

     
  • Moo Nam Ko

    Moo Nam Ko - 2018-11-09

    Hello Peter,

    Thank you so much! It works. Now, I understand how to handles the unbounded
    number of elements. Thank you for your great help and time again!!!

     

Log in to post a comment.

MongoDB Logo MongoDB