Skip to content

Instantly share code, notes, and snippets.

@chriswhite199
Created September 19, 2020 19:52
Show Gist options
  • Save chriswhite199/34d83ba2d5141c9afb21b0bc66037f8c to your computer and use it in GitHub Desktop.
Save chriswhite199/34d83ba2d5141c9afb21b0bc66037f8c to your computer and use it in GitHub Desktop.
XSD Offline Download
#!/bin/bash
set -e
command -V xq || (echo "no xq installed - pip install yq" && exit 1)
command -V dos2unix || (echo "no dos2unix installed" && exit 1)
xml=$1
[[ ! -z "$xml" ]] || (echo "No file passed as input" && exit 1)
completed=()
rm -f catalog.cat
function dlSchema() {
local loc=$1
if [[ " ${completed[@]}" =~ " ${1}" ]]; then
echo "Already completed $1"
return
fi
if [[ $loc =~ https?://.* ]]; then
echo "Downloading $loc"
curl -sL $loc -O -w %{filename_effective} > .filename
loc=$(cat .filename)
dos2unix $loc
echo "SYSTEM \"$1\" $loc" >> catalog.cat
else
[[ -f "$loc" ]] || (echo "$loc not found" && exit 1)
fi
completed+=("$1")
ns=$(cat $loc | xq '. | keys[0] | split(":")[0]' -r)
local schemas="$(cat $loc | xq '(."'$ns':schema"."'$ns':import" // []) | if type=="object" then [.] else . end | .[] | ."@schemaLocation"' -r)"
echo $schemas
if [[ ! -z "$schemas" ]]; then
for schema in $(cat $loc | xq '(."'$ns':schema"."'$ns':import" // []) | if type=="object" then [.] else . end | .[] | ."@schemaLocation"' -r)
do
# recurse schema
dlSchema $schema
done
fi
}
dlSchema $xml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment