Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use <a href="http://xmlsoft.org/xmllint.html" rel="nofollow">xmllint</a> which is a command line tool bundled with libxml2. Very likely that its available on your system.</p> <p>Based on your example data (deleted the ellipsis) I played around and managed the following:</p> <pre><code>echo -e "du\nbye\n" | \ xmllint --shell data </code></pre> <p>which returns</p> <pre><code>/ &gt; du / bookstore book title price retail discounts amazon currency / &gt; bye </code></pre> <p>This uses the interactive mode of the tool.<br> <code>du</code> ask to print the whole subtree starting from current node (here root). <code>bye</code> just exits the program.</p> <p>The next step is now to parse this output.</p> <p><strong>UPDATED:</strong> (assuming that the XML is in <code>data</code>)<br> Note that the node in question is currently hardcoded!</p> <pre><code>#!/bin/bash echo -e "du\nbye\n" | \ xmllint --shell data | \ sed 's/ /: /g' | \ awk ' BEGIN {depth = 0} $NF == "amazon" { for(i=1; i&lt;NF; i++) {printf("/%s", STACK[i])} print "/" $NF } /^\// {next} NF == depth + 1 {depth = NF; STACK[depth] = $NF; next} NF == depth {STACK[depth] = $NF; next} NF &lt; depth {depth = NF; STACK[depth] = $NF; next} 1 {print "something went horribly wrong!"} ' </code></pre> <p>gives</p> <pre><code>/bookstore/book/price/discounts/amazon </code></pre> <p>To explain this look at the output after the <code>sed</code> command:</p> <pre><code>/ &gt; du / bookstore : book : : title : : price : : : retail : : : discounts : : : : amazon : : : currency / &gt; bye </code></pre> <p><code>sed</code> substitutes <code>[two spaces]</code> with <code>[:space]</code>.<br> In the following it is simple to detect the depth with <code>awk</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload