Home > Enterprise >  xmlstarlet to comma separated values
xmlstarlet to comma separated values

Time:06-08

I have an XML file like this

<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
    <types>
        <members>B2B_ProductDetailDesktop</members>
        <name>ApexComponent</name>
    </types>
    <types>
        <members>Class1</members>
        <members>Class2</members>
        <name>ApexClass</name>
    </types>
    <version>54.0</version>
</Package>

I need to extract the members value to a comma separated list, only for the ApexCLass type. In the given example, this should output this:

CLass1,Class2

I try with

xmlstarlet sel -N d=http://soap.sforce.com/2006/04/metadata -t -m "//d:Package/d:types" -v "d:members" temp/package/package.xml

But it outputs only

B2B_ProductDetailDesktopClass1

Is it possible?

CodePudding user response:

This should do it (assuming a POSIX shell):

xmlstarlet select -T -t \
  -m "/_:Package/_:types[_:name='ApexClass']/_:members" \
    -i "position() != 1" -o "," -b  \
    -v "." \
  -b -n \
file.xml

By default (--doc-namespace being in effect) namespaces declared in the input document can be referred to without explicit -N options; the default namespace is bound to the _ / DEFAULT prefix. See the user's guide ch. 5.

  • Related