Search code examples
xmllinuxbashxmlstarlet

How to select random node with xmlstarlet in bash?


Bash, ubuntu linux. How to select random node with xmlstarlet in bash?


Solution

  • xmlstarlet sel -B -t -c "//node()[$RANDOM mod last() + 1]" input.xml
    

    The -B strips whitespace nodes, which you probably don't want to select...


    I also tried using math:random() defined at exslt.org:

    xmlstarlet sel -N math=http://exslt.org/math -B -t --var r='math:random()' \
     -c '//node()[round($r * last()) + 1]' -n input.xml
    

    But it appears to use the same seed every time.