0

I am not very experienced with bash scripting. But I am trying to validate individual xml files with corresponding xsd files in a directory. The beginning name does not change but the dates changes.

For example:

  • File1.xsd
  • File2.xsd
  • File3.xsd

  • File1_random_date.xml (random date time can be 2016_06_12_10_38_13)
  • File2_random_date.xml
  • File2_random_date.xml
  • File3_random_date.xml


I would like validate all File2*.xml files against File2.xsd and all File1*.xml against File1.xsd etc etc


something like:

xmllint --noout --schema File2.xsd File2_*.xml
xmllint --noout --schema File1.xsd File1_*.xml

But i am not sure how to have regex string maybe for dates and say if File2_*.xml exist, validates each file against File2.xsd.

Any help please?

  • The shell doesn't (usually) use regular expressions. It uses shell globs, where * represents zero of more characters and ? represents a single character. Just like you're using them in your Question. – roaima Sep 20 '16 at 14:35
0

With zsh:

list=(file*_*.xml)
for prefix (${(u)list%%_*})
  xmllint --noout --schema $prefix.xsd ${prefix}_*.xml
  • You should explain more about how your commands work, rather than just posting commands. – Centimane Sep 20 '16 at 20:26
0

Something like this may help (using bash):

# Iterate across the XSD files
for xsdfile in *.xsd
do
    test -f "$xsdfile" || continue

    # Strip the ".xsd" suffix and search for XML files matching this prefix
    prefix="${xsdfile%.xsd}"
    for xmlfile in "$xsdfile"_*.xml
    do
        test -f "$xmlfile" || continue
        xmllint --noout --schema "$xsdfile" "$xmlfile"
    done
done

If you want to check all the matching XML files in a single operation, this would work:

# Iterate across the XSD files
for xsdfile in *.xsd
do
    test -f "$xsdfile" || continue

    # Strip the ".xsd" suffix and search for XML files matching this prefix
    prefix="${xsdfile%.xsd}"
    for xmlfile in "$xsdfile"_*.xml
    do
        # Skip if no files, else do it just once
        test -f "$xmlfile" || continue
        xmllint --noout --schema "$xsdfile" "$xsdfile"_*.xml
        break
    done       
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.