Tuesday, January 26, 2016

Getting child node names from xmlstarlet

Ok. Just throwing this out there, because I was banging my head on my desk this morning trying to figure this out. I couldn't find any simple examples of this, but that could just be due to my inability to find things. I would have likely fared better if I asked my wife to find it for me. (It certainly works for my car keys). Here's hoping this helps someone.


cat <xml file>|  xmlstarlet sel -t -m "//path/to/child::node()" -v "name()" -n

with XML like this:

<?xml version="1.0" encoding="UTF-8"?><landshark_properties VERSION="2.3.1">
<general>
<stuff/>
</general>
<groups>
<vdb_groups>
<vdb_group>
<value>Dev Copies</value>
</vdb_group>
</vdb_groups>
<dsource_group>
<value>Sources</value>
<type>String</type>
<description>The ONE group Landshark setup will create for the dSources - No need to change</description>
</dsource_group>
</groups>
<environments>
<things/>
</environments>
<engine>
<morethings/>
</engine>
<content>
<sugarcrm/>
<employee_11/>
</content>
</landshark_properties>

Output like this:
cat text.xml |  xmlstarlet sel -t -m "//content/child::node()" -v "name()" -n

sugarcrm

employee_11