xml - How to return array with XPath? -
i wondering if xpath return array of value.
i have someting :
<cities> <city>paris</city> <city>lyon</city> <city>marseille</city> </cities>
and i'd array of form ['paris', 'lyon', 'marseille'].
i using xpath unix utility.
any idea ?
in addition post-processing output of:
xpath -q -e '//city/text()' inputfile
which is:
paris lyon marseille
in number of ways, including:
xpath -q -e '//city/text()' inputfile | awk 'begin {sq = "\047"; ofs = sq "," sq} {a[$1] = nr} end { printf "[" sq; (i in a) { printf "%s%s", d, i; d = ofs}; print sq "]" }'
which gives output:
['lyon','marseille','paris']
you can use modified version of xpath
. since it's simple perl script, modified have more control on formatting of output. version, can do:
xpath -q -b "[" -p "'" -i "," -s "'" -a "]"$'\n' -e '//city/text()'
to desired output. here usage message:
usage: xpath [options] -e query [-e query...] [filename...] if no filenames given, supply xml on stdin. must provide @ least 1 query. each supplementary query done in order, previous query giving context of next one. options: -q quiet output resulting path -b before use before instead of nothing. -p prefix use prefix instead of nothing. -s suffix use suffix instead of linefeed. -i infix use infix instead of nothing. -a after use after instead of nothing.
you can download version here.
Comments
Post a Comment