1

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 
3
  • 1
    I suggest to check the XML against a Schema definition, see stackoverflow.com/a/129401/10622916 for available tools and stackoverflow.com/q/28987004/10622916 for a schema that checks for an empty value.
    – Bodo
    CommentedSep 11, 2020 at 9:11
  • 1
    Assuming consistency in the input, a missing value appears always to be short-form (not requiring a separate </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.CommentedSep 11, 2020 at 9:18
  • I don't do git. You mentioned a "git hook" so I assumed you knew how to glue my snippet in place. I would assume git commit would pass you the names so you could iterate over them in a script, or pass you the content so you could read it through a pipe, and you could return an exit code that would abort the commit. Or you just run the snippet manually first, for each file you want to commit.CommentedSep 12, 2020 at 9:16

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.