Search code examples
xmlbashshellxmlstarlet

Retrieve XML Node Structure - Bash


I am trying to retrieve the node structure of a given XML file on a Windows machine through git bash. I have pretty much followed exactly what was mentioned in this example.

I am running the same command as in the example, which is:

xml sel -T -t -m '//*' \
    -m 'ancestor-or-self::*' -v 'name()' -i 'not(position()=last())' -o \
    . -b -b -n structure.xml

This command runs fine on a MAC (through the regular terminal). However, when I run it on a Windows machine through git bash, it only returns the root node of the XML structure, rather than the whole tree as expected.

What would be the equivalent command to run in order to get the whole structure? I have tried specifying different XPath Axes as specified here, but to no avail.

EDIT:

Say for example, I had the below XML structure

<node1>
   <node2>
       <node3>Whatever</node3>
   </node2>
<node1>

The command above should return (as it does on MAC)

node1
node1.node2
node1.node2.node3

whereas on Windows, it simply returns the root node, i.e. node1


Solution

  • Alternatively, try running the following compound command instead:

    structure=$(xml el structure.xml) && echo "${structure//\//.}"
    

    Explanation:

    1. This runs a simpler xmlstarlet cmd using command substitution to assign the result to a structure variable. The xmlstarlet command returns node names separated with a forward slash (/).

      For instance:

      node1/node2/node3

    2. After the && operator we echo the value of structure using parameter expansion to replace forward slashes with dots (.).