1

I'm trying to make a script (bash or python ideally, so I learn and don't just use it dumbly) that will parse a XML file that looks like this :

<?xml version="1.0" encoding="UTF-8"?>
<fruits xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://whatever/fruits.xsd" timestamp="1521126010" merchantId="xxxx">
<fruit id="1" name="Orange" color="orange"/>
<fruit id="2" name="Mandarine" color="Orange"/>
<fruit id="3" name="Raisin" color="Green" variety="4"/>
<fruit id="4" name="Raspberrry" color="red" variety="2"/>
<fruit id="5" name="Kiwi" color="brown"/>
<fruit id="6" name="I am a fruit" variety="7">
</fruits>

I'm trying to make a script that can return me the differents attributes. For example :

./script Raisin -c
Orange
./script Kiwi -v
No variety defined
./script "I am a fruit" -i
6

And so on. I've read a lot on XML parsing, but didn't found anything yet with that kind of XML file. Any help would be greatly appreciated.

5
  • and what does mean your -i option? Describe all your options Commented Mar 16, 2018 at 14:19
  • In terms of python there is a module called etree that is included in lxml. This will allow you to easily parse your XML data in the python runtime. Commented Mar 16, 2018 at 14:22
  • @RomanPerekhrest : -i would be for id, -c for color, -v for variety Commented Mar 16, 2018 at 14:28
  • @Kyle : Will check it, thanks ! Commented Mar 16, 2018 at 14:28
  • " didn't found anything yet with that kind of XML file" => the point of formats like XML is that they are generic. IOW you can use just any available Python XML parser to do the job. Commented Mar 16, 2018 at 14:43

2 Answers 2

1

Complete bash + xmlstarlet solution:

get_attr.sh script:

#!/bin/bash

name=$1
declare -A attr_map
attr_map=(["-c"]=color ["-i"]=id ["-v"]=variety)

if [[ -z "$2" ]]; then
    echo "Additional attribute missing!"
    exit 1
fi

if [[ -z "${attr_map[$2]}" ]]; then
    echo "Unsupported attribute prefix. Allowed are: ${!attr_map[@]}"
    exit 1
fi

attr="${attr_map[$2]}"
result=$(xmlstarlet sel -t -m "//fruit[@name='$name' and @$attr]" -v "./@$attr" input.xml)
if [[ -n "$result" ]]; then
    echo "$result"
else
    echo "No $attr attribute defined"
fi

Test cases:

$ bash get_attr.sh "Orange" -c
orange
$ bash get_attr.sh "Raisin" -v
4
$ bash get_attr.sh "Raisin" -d
Unsupported attribute prefix. Allowed are: -v -c -i
$ bash get_attr.sh "I am a fruit" -i
6
$ bash get_attr.sh "I am a fruit" -c
No color attribute defined
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much ! Will definitely check the xmlstartlet thinggy for my next scripts !
0

Check out the xmlstarlet tool.

http://xmlstar.sourceforge.net/doc/UG/xmlstarlet-ug.html

Using xmlstarlet you can execute XPath queries from the command line.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.