检索 XML 节点结构 - Bash [英] Retrieve XML Node Structure - Bash

查看:29
本文介绍了检索 XML 节点结构 - Bash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 git bash 在 Windows 机器上检索给定 XML 文件的节点结构.我几乎完全遵循了在这个例子中提到的内容.

我正在运行与示例中相同的命令,即:

xml sel -T -t -m '//*' \-m '祖先或-self::*' -v 'name()' -i 'not(position()=last())' -o \.-b -b -n 结构.xml

此命令在 MAC 上运行良好(通过常规终端).但是,当我通过 git bash 在 Windows 机器上运行它时,它只返回 XML 结构的根节点,而不是预期的整个树.

为了获得整个结构而运行的等效命令是什么?我曾尝试按照此处指定不同的 XPath Axes,但无济于事.>

例如,我有以下 XML 结构

<节点2><node3>随便</node3><节点1>

上面的命令应该返回(就像在 MAC 上一样)

node1节点1.节点2节点1.节点2.节点3

而在 Windows 上,它只返回根节点,即 node1

解决方案

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

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

说明:

  1. 这使用 xmlstarlet cmd="nofollow noreferrer">命令替换 将结果分配给 structure 变量.xmlstarlet 命令返回用正斜杠 (/) 分隔的节点名称.

    例如:

    <块引用>

    node1/node2/node3

  2. && 操作符之后,我们使用 参数扩展 用点替换正斜杠 (.).

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

解决方案

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

这篇关于检索 XML 节点结构 - Bash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆