def matchEDIElements(self, EDISegment, specElement): #Make sure the EDISegment matches the rules laid out in the specElement. if EDISegment.getSegmentID() == specElement.getAttribute("id"): match = 1 else: match = 0 return match
def writeDataSegment(self, specElement, EDISegment): #Before writing the EDISegment, we need to compare this segments lineage to the previous segment so that we #know whether we need to extend the current XML data branch, or go back and create a new one somewhere. #Let's get the lineage of this EDISegment. thisMatchLineage = self.getSpecLineage(specElement) #Determine if there is a straight up lineage match, i.e. this EDI segment and the last EDI segment are "leafs on the same branch." if thisMatchLineage == self.lastMatchLineage: #print('Lineages match!') lineageMismatch = 'false' else: #print('Lineages do not match!') lineageMismatch = 'true' #Determine where the lineage of this EDISegment and the last EDISegment diverge. stopindex = min(len(thisMatchLineage), len(self.lastMatchLineage)) - 1 i = 0 while (i <= stopindex): if (not thisMatchLineage[i].isSameNode(self.lastMatchLineage[i])): lineageMismatch = 'true' break i = i + 1 if (len(thisMatchLineage) < len(self.lastMatchLineage) and EDISegment.getSegmentID() != 'SE'): i = i - 1 #For lineages that don't match, we need to do some work to the XML data file's current branch. if lineageMismatch =='true': thisMatchDataLineage = [] #Build the new data lineage first using elements that match between this EDISegment lineage and the last. j = 0 while (j < i) : thisMatchDataLineage.append(self.lastMatchDataLineage[j]) j = j + 1 k = j while (k < len(thisMatchLineage)): newElementID = thisMatchLineage[k].nodeName newNode = self.dataDocument.createElement(newElementID) newNode.setAttribute('id', thisMatchLineage[k].getAttribute('id')) newNode.setAttribute('name', thisMatchLineage[k].getAttribute('name')) #if (thisMatchDataLineage[k - 1]): if (k - 1 > 0 and thisMatchDataLineage[k - 1]): thisMatchDataLineage[k - 1].appendChild(newNode) else: if (newNode.tagName != 'edifilespec'): self.dataDocumentElement.appendChild(newNode); thisMatchDataLineage.append(newNode) k = k + 1 #Set the class variables for the next go round. self.lastMatchLineage = thisMatchLineage if (lineageMismatch == 'true'): self.lastMatchDataLineage = thisMatchDataLineage #Build and the current EDISegment as a <segment> node in the XML data file. newEDISegmentNode = self.dataDocument.createElement('segment') newEDISegmentNode.setAttribute('id', specElement.getAttribute('id')) newEDISegmentNode.setAttribute('name', specElement.getAttribute('name')) #Add the EDISegment elements as <element> nodes hanging off the <segment> node in the XML data file. m = 1 while (m < len(EDISegment.elements)): newEDIElementNode = self.dataDocument.createElement('element') newEDIElementNode.setAttribute('id', specElement.getAttribute('id') + format(m, '#02')) newEDIElementNode.value = EDISegment.getElementByIndex(m) newEDIElementNode.setAttribute('value', EDISegment.getElementByIndex(m)) newEDISegmentNode.appendChild(newEDIElementNode) m = m + 1 #Set the class variables that point to XML data file locations for the next go round. try: self.lastMatchDataLineage[len(self.lastMatchDataLineage) - 1].appendChild(newEDISegmentNode) except: print('Error appending child to EDITSParser.lastMatchDataLineage') self.dataDocumentElement.appendChild(newEDISegmentNode) return 0