xmlstarlet 使用子节点提取组信息
xmlstarlet extract group info with subnode
我需要从复杂的 XML 文件中提取组信息,然后遍历组以执行一些更改,但我无法以干净的方式循环获取数据。尝试了很多,看到了很多例子,但没有运气。感谢任何帮助。
给定以下 XML 文件:
<?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>
如何获得像下面的 csv 一样的输出,每组一行 4 列:id、name、siteRole 和 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
...
我设法使用以下命令输出“组”节点内的值:
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;;
但是正如您在上面看到的那样,我无法获取子节点“import”上的@siteRole 和@grantLicenseMode 的值。
我建议:
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
输出:
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
参见:xmlstarlet select --help
我需要从复杂的 XML 文件中提取组信息,然后遍历组以执行一些更改,但我无法以干净的方式循环获取数据。尝试了很多,看到了很多例子,但没有运气。感谢任何帮助。
给定以下 XML 文件:
<?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>
如何获得像下面的 csv 一样的输出,每组一行 4 列:id、name、siteRole 和 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
...
我设法使用以下命令输出“组”节点内的值:
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;;
但是正如您在上面看到的那样,我无法获取子节点“import”上的@siteRole 和@grantLicenseMode 的值。
我建议:
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
输出:
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
参见:xmlstarlet select --help