我已经通过命令行工具xmlstarlet(1.6.1)清理了几个xml文件。
示例1
<?xml version="1.0" encoding="utf-8"?>
<!-- Some license comment
- with some link to http://example.com/foo/ -->
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
<ShortName>Name Sample 1</ShortName>
<Description>Description Sample 1</Description>
<InputEncoding>UTF-8</InputEncoding>
<Image height="16" width="16">data:...</Image>
<Url type="text/html" method="get" template="https://examplesearch.com/" rel="searchform">
<Param name="q" value="{searchTerms}"/>
<MozParam name="m1" condition="purpose" value="abc"/>
<MozParam name="m2" condition="purpose" value="cde"/>
</Url>
</OpenSearchDescription>
示例2
<!-- Some license comment
- with some link to http://example.com/foo/ -->
<SearchPlugin xmlns="http://www.mozilla.org/2006/browser/search/">
<ShortName>Name Sample 2</ShortName>
<Description>Description Sample 2</Description>
<InputEncoding>UTF-8</InputEncoding>
<Image width="16" height="16">data:...</Image>
<Url type="application/x-suggestions+json" method="GET" template="https://www.examplesearch.com/search?client=firefox&q={searchTerms}"/>
<Url type="text/html" method="GET" template="https://examplesearch.com/search" rel="searchform">
<Param name="q" value="{searchTerms}"/>
</Url>
</SearchPlugin>
我尝试首先清除适用于示例2 的Description
节点:
xml ed -L -u "//_:SearchPlugin/_:Description" -v "" sample2.xml
结果是节点<Description/>
,但相同的逻辑不适用于样本1 :
xml ed -L -u "//_:OpenSearchDescription/_:Description" -v "" sample1.xml
我希望收到两个XML样本的结果<Description><Description/>
。
更新: 上一部分已解决。
第二,在示例2 中,我想从第二个client=firefox&
模板值中删除Url
:
"https://www.examplesearch.com/search?client=firefox&q={searchTerms}"
我不知道如何对值应用regex / xslt操作来实现此目的。有什么建议可以做到吗?