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).
>>> 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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
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
Could you explain more detail about the _PluralBinding? Any reference documents?
Thank you for your help again!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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:
Here is my python test code and result:
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
A couple things are odd. First, you're referencing
GenderDetailsbut the XML has it spelledgenderdetails. Probably one or the other is not what's actually being used. The bigger clue is the reference to PluralBinding which suggests that theGenderelement 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
maxOccurrencesgreater 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.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.
Thank you for your help again!
If the schema shows
GenderValueas a non-plural element within whatever typeGenderis the code you show should work. Without seeing the schema I can't tell.The
_PluralBindingmaterial is internal to PyXB; some of its visible markers are described here.GenderValue part of the schema is
The schema shows there are an unbounded number of
Genderelements within the containingGenderDetailselement; i.e.Genderis plural, as I originally suggested. So you probably needperson.GenderDetails.Gender[0].GenderValue.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!!!