Retrieve XML Node Structure - Bash

允我心安 提交于 2020-01-16 01:16:10

问题


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


回答1:


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 (.).



来源:https://stackoverflow.com/questions/58643568/retrieve-xml-node-structure-bash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!