xmlstarlet example 5
open http://aplawrence.com/rss/index.html curl -s http://feeds2.feedburner.com/SiteNewsForAplawrenceUnixLinuxAndMacOsXResources | \ xmlstarlet fo > aplawrence.xml less aplawrence.xml xmlstarlet val aplawrence.xml xmlstarlet el -a aplawrence.xml | less xmlstarlet el -v aplawrence.xml | less # def=... is default namespace xmlstarlet sel \ -N def="http://purl.org/rss/1.0/" \ -N rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" \ -t -m "//rdf:RDF" -v '.' \ aplawrence.xml | less xmlstarlet sel \ -N def="http://purl.org/rss/1.0/" \ -N rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" \ -t -m "//rdf:RDF" -v 'def:channel' \ aplawrence.xml | less # same xmlstarlet sel \ -N def="http://purl.org/rss/1.0/" \ -N rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" \ -t -m "//rdf:RDF/def:channel" -n \ -v '../def:channel' -n \ aplawrence.xml | less xmlstarlet sel \ -N def="http://purl.org/rss/1.0/" \ -N rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" \ -t -m "//rdf:RDF/def:channel/def:title" -v '.' \ aplawrence.xml xmlstarlet sel \ -N def="http://purl.org/rss/1.0/" \ -N rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" \ -N sy="http://purl.org/rss/1.0/modules/syndication/" \ -N dc="http://purl.org/dc/elements/1.1/" \ -t \ -m "//rdf:RDF/def:channel" -n \ -v '@rdf:about' -n -n \ -v 'def:title' -n \ -v 'def:description' -n \ -v 'sy:updatePeriod' -n \ -v 'sy:updateFrequency' -n \ -v 'sy:updateBase' -n \ -v 'dc:rights' -n \ -v 'dc:creator' -n \ -v 'dc:date' -n \ -v 'def:image/@rdf:resource' -n -n \ -v 'def:items/rdf:Seq/rdf:li[1]/@rdf:resource' -n \ -v 'def:items/rdf:Seq/rdf:li[2]/@rdf:resource' -n \ -v 'def:items/rdf:Seq/rdf:li[3]/@rdf:resource' -n -n \ -m './def:items/rdf:Seq/rdf:li/@rdf:resource' -v '.' -n \ aplawrence.xml xmlstarlet sel \ -N def="http://purl.org/rss/1.0/" \ -N rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" \ -N sy="http://purl.org/rss/1.0/modules/syndication/" \ -N dc="http://purl.org/dc/elements/1.1/" \ -t \ -m "//rdf:RDF/def:item" -n \ -v 'def:title' -n \ -v '@rdf:about' -n \ aplawrence.xml # delete the matching line plus one further line with sed # cf. http://codesnippets.joyent.com/posts/show/2043 printf "a word\nand yet another word\n" | /usr/bin/sed '/word$/{N;d;}' xmlstarlet sel \ -T \ -N def="http://purl.org/rss/1.0/" \ -N rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" \ -N sy="http://purl.org/rss/1.0/modules/syndication/" \ -N dc="http://purl.org/dc/elements/1.1/" \ -t \ -m "//rdf:RDF/def:item" -n \ -o "Title: " -v 'def:title' -n \ -o "URL: " -v '@rdf:about' -n \ -v 'def:description' -n \ aplawrence.xml | \ sed -E -e 's/^((Title|URL): +.*)/<p>\1<\p>/g' | \ html2text -nobs -style pretty -width 120 | \ sed -E \ -e :a -e '/Advertise_Here$/N; s/\\\n//; ta' \ -e '/Advertise_Here/d' \ -e $'s/^(Title: +.*)/\\\n\\\n\033[31m\\1\033[m/g' \ -e $'s/^(URL: +.*)/\033[34m\\1\033[m\\\n/g' | \ less -r #---------------------------------------------------------- curl -s http://feeds2.feedburner.com/Aplawrencecomments | xmlstarlet fo > aplawrence.xml xmlstarlet el -a aplawrence.xml | less # best of best II xmlstarlet sel \ -T \ -N def="http://purl.org/rss/1.0/" \ -N rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" \ -N sy="http://purl.org/rss/1.0/modules/syndication/" \ -N dc="http://purl.org/dc/elements/1.1/" \ -t \ -m "//rdf:RDF/def:item" -n \ -o "Title: " -v 'def:title' -n \ -o "URL: " -v 'def:link' -n \ -v 'def:description' -n \ aplawrence.xml | \ sed -E -e 's/^((Title|URL): +.*)/<p>\1<\p>/g' | \ html2text -nobs -style pretty -width 120 | \ sed -E \ -e '/Advertise_Here/{N;d;}' \ -e $'s/^(Title: +.*)/\\\n\\\n\033[31m\\1\033[m/g' \ -e $'s/^(URL: +.*)/\033[34m\\1\033[m\\\n/g' | \ less -r
first lines of aplawrence.xml:
<?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss1full.xsl"?> <?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http:/ /webns.net/mvcb/" xmlns="http://purl.org/rss/1.0/" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0"> <channel rdf:about="http://aplawrence.com/rss.rdf"> <title>Main News at A.P.Lawrence</title> <link>http://aplawrence.com/</link> <description> Main feed at aplawrence.com: Thousands of articles, reviews, consultants listings, skills tests, opinion, how-to's for Unix, Linux and Mac OS X, networking, web site maintenance and m ore.. </description> ...