我正在尝试将XML电话簿(Fritzbox格式)转换为VCARD格式。稍后,我想使用Winmerge或Meld将创建的vcard文件与来自其他来源的vcard导出合并。
有一条记录没有电话号码/ Test.xml @type=‘home’元素的类型:
<phonebooks>
<phonebook name="Telefonbuch">
<contact>
<category/>
<person>
<realName>Paul Tiger</realName>
</person>
<telephony nid="4">
<number type="work" id="1" vanity="" prio="1">071150885524</number>
</telephony>
<services/>
</contact>
</phonebook>
</phonebooks>我可以使用xmlstarlet将其转换为vcard:
xmlstarlet \
sel -t -m /phonebooks/phonebook/contact \
-o "begin:vcard" -n \
-v "concat('n:',person/realName)" -n \
-v "concat('tel;cell:',telephony/number[@type='work'])" -n \
-v "concat('tel;home:',telephony/number[@type='home'])" -n \
-o "end:vcard" -n \
-n test.xml结果:
begin:vcard
n:Paul Tiger
tel;cell:071150885524
tel;home:
end:vcard这就是我想要的:
begin:vcard
n:Paul Tiger
tel;cell:071150885524
end:vcard问题是,如果不存在任何元素,我如何抑制像“tel;home:”这样的vcard标记。
发布于 2018-08-28 02:44:14
在深入研究之后,我发现必须使用条件(--if)。
xmlstarlet \
sel -t -m /phonebooks/phonebook/contact \
-o "BEGIN:VCARD" -n \
-o "VERSION:2.1" -n \
-v "concat('FN:',person/realName)" -n \
\
--if "telephony/number[@type='work'] != ''" \
-v "concat('TEL;WORK;VOICE:',telephony/number[@type='work'])" -n \
--break \
\
--if "telephony/number[@type='home'] != ''" \
-v "concat('TEL;HOME;VOICE:',telephony/number[@type='home'])" -n \
--break \
\
--if "telephony/number[@type='mobile'] != ''" \
-v "concat('TEL;CELL;VOICE:',telephony/number[@type='mobile'])" -n \
--break \
-o "END:VCARD" -n -n \
FB.xml https://stackoverflow.com/questions/51920381
复制相似问题