问题
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:
This runs a simpler
xmlstarlet
cmd using command substitution to assign the result to astructure
variable. Thexmlstarlet
command returns node names separated with a forward slash (/
).For instance:
node1/node2/node3
After the
&&
operator weecho
the value ofstructure
using parameter expansion to replace forward slashes with dots (.
).
来源:https://stackoverflow.com/questions/58643568/retrieve-xml-node-structure-bash