I need to extract group info from a complex XML file, then loop through the groups to perform some changes, but I'm not able to get data in a clean way to loop through. Tried a lot, seen a lot of examples, but no luck. Any help is appreciated.
Given the following XML file:
<?xml version='1.0' encoding='UTF-8'?>
<tsResponse xmlns="http://tableau.com/api" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tableau.com/api http://tableau.com/api/ts-api-3.12.xsd">
<pagination pageNumber="1" pageSize="100" totalAvailable="12"/>
<groups>
<group id="74c29008-7550-11eb-94d7-73020b27aff3" name="Todos os usuários">
<domain name="local"/>
</group>
<group id="e392c24a-535d-4e78-8463-0a492d61c317" name="Tbv_Vs_Operacoes">
<domain name="university.net"/>
<import domainName="university.net" siteRole="Viewer" grantLicenseMode="onLogin"/>
</group>
<group id="6c236376-aa1b-451e-8b63-488387aac20b" name="Tbv_Alertas_Inteligentes">
<domain name="university.net"/>
<import domainName="university.net" siteRole="Viewer" grantLicenseMode="onLogin"/>
</group>
<group id="58c86b3e-e1b2-46e5-9acf-43335de1c58d" name="TBE_VS_OPERACOES">
<domain name="university.net"/>
<import domainName="university.net" siteRole="ExplorerCanPublish" grantLicenseMode="onLogin"/>
</group>
<group id="89c33fb6-dce9-4097-9be8-f01d502cc11b" name="Tbl_Server_Admin_Creator">
<domain name="university.net"/>
<import domainName="university.net" siteRole="SiteAdministratorExplorer" grantLicenseMode="onLogin"/>
</group>
<group id="306dac3b-a377-451f-9283-c7be3533e02f" name="Tbv_Vs_Canais_Nao_Proprietarios">
<domain name="university.net"/>
<import domainName="university.net" siteRole="Viewer" grantLicenseMode="onSync"/>
</group>
<group id="9259961c-3ca7-4c1a-9c44-374a4555c270" name="Tbe_Vs_Canais_Nao_Proprietarios">
<domain name="university.net"/>
<import domainName="university.net" siteRole="Explorer" grantLicenseMode="onSync"/>
</group>
</groups>
</tsResponse>
How to get an output like the csv below, with one row per group and 4 columns: id, name, siteRole and grantlicenseMode?
74c29008-7550-11eb-94d7-73020b27aff3,Todos os usuários,,
e392c24a-535d-4e78-8463-0a492d61c317,Tbv_Vs_Operacoes,Viewer,onLogin
6c236376-aa1b-451e-8b63-488387aac20b,Tbv_Alertas_Inteligentes,Viewer,onLogin
58c86b3e-e1b2-46e5-9acf-43335de1c58d,TBE_VS_OPERACOES,ExplorerCanPublish,onLogin
...
I managed to output the values inside the "group" node with the following command:
xmlstarlet sel -N api=http://tableau.com/api -T -t -m //api:group -v "concat(@id,';',@name,';',import/@siteRole,';',import/@grantLicenseMode)" -n groups.xml
74c29008-7550-11eb-94d7-73020b27aff3;Todos os usuários;;
e392c24a-535d-4e78-8463-0a492d61c317;Tbv_Vs_Operacoes;;
6c236376-aa1b-451e-8b63-488387aac20b;Tbv_Alertas_Inteligentes;;
58c86b3e-e1b2-46e5-9acf-43335de1c58d;TBE_VS_OPERACOES;;
89c33fb6-dce9-4097-9be8-f01d502cc11b;Tbl_Server_Admin_Creator;;
306dac3b-a377-451f-9283-c7be3533e02f;Tbv_Vs_Canais_Nao_Proprietarios;;
9259961c-3ca7-4c1a-9c44-374a4555c270;Tbe_Vs_Canais_Nao_Proprietarios;;
6ed1b848-3206-4723-8b3b-7721a9869f53;Tbe_VS_Financeiro;;
701a819f-f176-4807-ad2c-572b970a9f8f;Tbv_VS_Financeiro;;
232613c8-9129-493c-b2af-a65c1d82d88a;Tbe_VS_Consumidor_CRM;;
c9346a0e-4f7a-4e53-95b4-71d7ef4c4e3b;Tbe_Vs_Demanda_e_Abastecimento;;
f8345037-4153-4303-bdb7-39ca3bee32ee;QlikSense_Sustent_BI;;
but as you can see above, I'm not able to get the values for @siteRole and @grantLicenseMode, that are both on the subnode "import".
CodePudding user response:
I suggest:
xmlstarlet select -N x="http://tableau.com/api" --text --template \
--match "//x:tsResponse/x:groups/x:group" \
--value-of "concat(@id,',',@name,',',x:import/@siteRole,',',x:import/@grantLicenseMode)" \
-n file.xml
Output:
74c29008-7550-11eb-94d7-73020b27aff3,Todos os usuários,,
e392c24a-535d-4e78-8463-0a492d61c317,Tbv_Vs_Operacoes,Viewer,onLogin
6c236376-aa1b-451e-8b63-488387aac20b,Tbv_Alertas_Inteligentes,Viewer,onLogin
58c86b3e-e1b2-46e5-9acf-43335de1c58d,TBE_VS_OPERACOES,ExplorerCanPublish,onLogin
89c33fb6-dce9-4097-9be8-f01d502cc11b,Tbl_Server_Admin_Creator,SiteAdministratorExplorer,onLogin
306dac3b-a377-451f-9283-c7be3533e02f,Tbv_Vs_Canais_Nao_Proprietarios,Viewer,onSync
9259961c-3ca7-4c1a-9c44-374a4555c270,Tbe_Vs_Canais_Nao_Proprietarios,Explorer,onSync
See: xmlstarlet select --help