检索 XML 节点结构 - Bash

Retrieve XML Node Structure - Bash

我正在尝试通过 git bash 在 Windows 机器上检索给定 XML 文件的节点结构。我几乎完全遵循了所提到的 in this example.

我运行使用与示例中相同的命令,即:

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

这个命令 运行 在 MAC 上没问题(通过常规终端)。但是,当我通过 git bash 在 Windows 机器上 运行 它时,它只是 return 是 XML 结构的根节点,而不是预期的整棵树。

为了获得整个结构,与 运行 等效的命令是什么?我已尝试指定不同的 XPath 轴 here,但无济于事。

编辑:

例如,我有下面的XML结构

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

上面的命令应该return(和MAC一样)

node1
node1.node2
node1.node2.node3

而在 Windows 上,它只是 return 根节点,即 node1

或者,尝试 运行 以下复合命令:

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

解释:

  1. 这会运行一个更简单的 xmlstarlet cmd,使用 command substitution 将结果分配给 structure 变量。 xmlstarlet 命令 returns 节点名称用正斜杠分隔 (/)。

    例如:

    node1/node2/node3

  2. && 运算符之后,我们 echo structure 的值使用 parameter expansion 将正斜杠替换为点(. ).