For instance, I have an XML file with the below content and the "NAME" Author and Assignee(s) have empty "VALUE". The main aim is to implement a check that can be applied to find XML files that have empty VALUE's. So is there a way I can create a script which can be used as a git hook(pre-commit) so that input to the script are the files that are being called by git commit (all files in that repo) or git commit (filename). And then the script should not allow to commit if the XML file has empty VALUEs.
<?xml version="1.0" encoding="utf-8"?> <PACKAGE format-rev="7" prog-version="2020.2.98572" xmlns=https..." xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <INFORMATION format-rev="2" xsi:type="packageInfo"> <ATTRIBUTES> <ATTRIBUTE xsi:type="attributeInst"> <NAME xsi:type="string">Type</NAME> <VALUE xsi:type="string">MANUAL</VALUE> </ATTRIBUTE> <ATTRIBUTE xsi:type="attributeInst"> <NAME xsi:type="string">Severity</NAME> <VALUE xsi:type="string">Normal</VALUE> <ATTRIBUTE xsi:type="attributeInst"> <NAME xsi:type="string">Author</NAME> <VALUE xsi:type="string"/> </ATTRIBUTE> <ATTRIBUTE xsi:type="attributeInst"> <NAME xsi:type="string">Assignee(s)</NAME> <VALUE xsi:type="string"/> </ATTRIBUTES>
Is there a better approach than the one shown below? TIA
def xmlvalidator(): dirlist = [] dirlist.append(os.getcwd().replace("\\","/")) filelists = filelist(dirlist) Flagvallists = [] for filename in filelists: xtree = et.parse(filename) root = xtree.getroot() xroot = root[0][0] flag = 0 flagval = [] for i in range(len(xroot)): #xroot here should ATTRIBUTES (ATTRIBUTE IN ATTRIBUTES) s_name = xroot[i][0].text s_value = xroot[i][1].text if s_value == None: flagval.append(s_name) flag = 1 if flag==1: flagval.append(filename) Flagvallists.append(flagval) #sys.exit(1) #return True return Flagvallists
EDIT: Thanks to @Paul_Pedant Seems like a simple and great solution, but how can the input be the same as the one called by git commit? and not the filename(A.xml)
if grep "<VALUE[^>]*/>" A.xml; then echo errors found else echo not found fi
</VALUE>
. So a RE of<VALUE[^>]*/>
matches an empty value. A script that silently greps for that and returns zero status (to mean at least one missing value) is a one-liner.