Skip to content

Commit

Permalink
added xmlstarlet demos
Browse files Browse the repository at this point in the history
  • Loading branch information
sumeetsarkar committed Nov 16, 2017
1 parent a8a143f commit 0dae3f1
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 52 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
output
test.sh
33 changes: 23 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
# xmllint demo
# xmllint & xmlstarlet demo

> A simple bash script to explore xmllint usage
> A simple bash script to explore xmllint & xmlstarlet usage
[xmllint Documentation](http://www.xmlsoft.org/xmllint.html)<br />
[xmlstarlet Documentation](http://xmlstar.sourceforge.net/docs.php)<br />

### Installation
Ubuntu, run apt-get install libxml2-utils (if not installed already)<br />
Mac OS X (Yosemite), installed by default
### Installation for xmllint
Mac OS X (Yosemite) installed by default<br/>

Ubuntu: sudo apt-get install libxml2-utils


### Installation for xmlstarlet
For Mac: brew install xmlstarlet
For Ubuntu: sudo apt-get install xmlstarlet

### xmllint usage demos in this project
1. --xpath
2. --repeat
3. --timing

### xmlstarlet usage demos in this project
1. -ed -d
2. -ed -u

### Running the demo

Project includes below sample xml files<br/>
Project includes below sample xml file<br/>
[books.xml](data/books.xml)<br/>
[food.xml](data/food.xml)<br/>

### Make app.sh executable and run the script
./app.sh data/food.xml
./app.sh data/books.xml
### Make xmllint-demo.sh executable and run the script
./xmllint.sh data/books.xml


### Make xmlstarlet-demo.sh executable and run the script
./xmlstarlet.sh data/books.xml
33 changes: 0 additions & 33 deletions data/food.xml

This file was deleted.

10 changes: 1 addition & 9 deletions app.sh → xmllint-demo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,11 @@

function main() {
# check if file is food.xml
if [ $1 == 'data/food.xml' ]; then
processBreakfastMenu $1
# check if file is books.xml
elif [ $1 == 'data/books.xml' ]; then
if [ $1 == 'data/books.xml' ]; then
processBookstore $1
fi
}

function processBreakfastMenu() {
echo $'usage of --path /breakfast_menu/food[1]\n'
xmllint --xpath '/breakfast_menu/food[1]' $1
}

function processBookstore() {
echo $'usage of --path /bookstore/book[@category="cooking"]/author/text()\n'
xmllint --xpath '/bookstore/book[@category="cooking"]/author/text()' $1
Expand Down
21 changes: 21 additions & 0 deletions xmlstarlet-demo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

function main() {
# check if file is food.xml
if [ $1 == 'data/books.xml' ]; then
processBookstore $1
fi
}

function processBookstore() {
xmlstarlet ed -d '/bookstore/book[@category="cooking"]' $1 > output/books-d.xml
xmlstarlet ed -u '/bookstore/book[@category="cooking"]/@category' -v 'baking' $1 > output/books-u.xml
xmlstarlet ed -u '/bookstore/book[@category="cooking"]/title' -v 'Everyday baking' $1 > output/books-uv.xml
xmlstarlet ed -u '/bookstore/book[@category="cooking"]/title[@lang="en"]/@lang' -v 'es' $1 > output/books-uv1.xml
xmlstarlet ed -u '/bookstore/book/@category' -v 'food' $1 > output/books-uv2.xml
}

# create output DIR if not presetn
mkdir -p output
echo 'File being read -' $1
main $1

0 comments on commit 0dae3f1

Please sign in to comment.