Browse Source

Dealing with unexpected errors after collecting multiple variable values

Andrej 3 weeks ago
parent
commit
896929f6c1
1 changed files with 23 additions and 15 deletions
  1. 23 15
      pythonScripts/importXML.py

+ 23 - 15
pythonScripts/importXML.py

@@ -31,27 +31,30 @@ def countElements(xmlRoot,xPath):
       i=i+1
    return i-1
 
+def mergePaths(corePath,subPath):
+   if len(corePath)>0:
+      if len(subPath)>0:
+         return '/'.join([corePath,subPath])
+      return corePath
+   return subPath
 
 def getVal(xmlRoot,aliasVal,xPath=''):
+
    q=aliasVal.split(':')
    xName=q[0]
-    
-   if len(xPath)>0:
-      if len(xName)>0:
-         elementPath='/'.join([xPath,xName])
-      else:
-         elementPath=xPath
-   else:
-      elementPath=xName
-
+   elementPath=mergePaths(xPath,xName)
    attributeName=q[1]
 
-#find number of elements with this path
-   n=countElements(xmlRoot,elementPath) 
+   eList=[elementPath]
+   if xPath[-1]!=']':
+      n=countElements(xmlRoot,elementPath) 
+      eList=[f'{elementPath}[{i+1}]' for i in range(n)]
+   
+   print(eList)
    try:
-      return [xmlRoot.find(f'{elementPath}[{i+1}]').get(attributeName) for i in range(n)]
+      return [xmlRoot.find(e).get(attributeName) for e in eList]
    except AttributeError:
-      return [None]
+      return None
 
 def updateTxt(txt,replacePatterns):
    for x in replacePatterns:
@@ -142,9 +145,14 @@ def importXML(importXLSX,pars,xmlRoot,dryRun=True):
    vals={}
    for f in fields:
       try:
-         vals[f]=getVal(xmlRoot,alias[f],xPath)
+         x=getVal(xmlRoot,alias[f],xPath)
+         if not x:
+            continue
          if not allowMultiple:
-            vals[f]=vals[f][0:1]
+            x=x[0:1]
+         n=len(x)
+         print('{}[{}]: {}'.format(f,n,x))
+         vals[f]=x
       except KeyError:
          print(f'Alias for field {f} not found')
          continue