如果子节点包含特定字符串,则删除XML节点
如果子节点包含特定字符串或单词,则需要从XML文件中删除一些节点,示例XML文件如果子节点包含特定字符串,则删除XML节点,xml,bash,shell,Xml,Bash,Shell,如果子节点包含特定字符串或单词,则需要从XML文件中删除一些节点,示例XML文件 <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="/3982474/sitemap_nb.xsl"?> <urlset xmlns="http://www.sitemaps.org
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/3982474/sitemap_nb.xsl"?>
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xsi:schemaLocation="
http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
<loc>https://www.test.com/home</loc>
<lastmod>2020-08-03T14:41:44+00:00</lastmod>
<changefreq>daily</changefreq>
<priority>0.8000</priority>
</url>
<url>
<loc>https://www.test.com/features?xxxxx=serviceability-point-access</loc>
<lastmod>2020-08-03T14:41:44+00:00</lastmod>
<changefreq>daily</changefreq>
<priority>0.5120</priority>
</url>
<url>
<loc>https://www.test.com/eu/index</loc>
<lastmod>2020-08-03T14:41:44+00:00</lastmod>
<changefreq>daily</changefreq>
<priority>0.8000</priority>
</url>
<url>
<loc>https://www.test.com/features?xxxxx=serviceability-point-access</loc>
<lastmod>2020-08-03T14:41:44+00:00</lastmod>
<changefreq>daily</changefreq>
<priority>0.5120</priority>
</url>
<url>
<loc>https://www.test.com/models/s510/features?xxxxx=serviceability</loc>
<lastmod>2020-08-03T14:41:44+00:00</lastmod>
<changefreq>daily</changefreq>
<priority>0.5120</priority>
</url>
<url>
<loc>https://www.test.com/index</loc>
<lastmod>2020-08-03T14:41:44+00:00</lastmod>
<changefreq>daily</changefreq>
<priority>0.8000</priority>
</url>
</urlset>
https://www.test.com/home
2020-08-03T14:41:44+00:00
每日的
0.8000
https://www.test.com/features?xxxxx=serviceability-点访问
2020-08-03T14:41:44+00:00
每日的
0.5120
https://www.test.com/eu/index
2020-08-03T14:41:44+00:00
每日的
0.8000
https://www.test.com/features?xxxxx=serviceability-点访问
2020-08-03T14:41:44+00:00
每日的
0.5120
https://www.test.com/models/s510/features?xxxxx=serviceability
2020-08-03T14:41:44+00:00
每日的
0.5120
https://www.test.com/index
2020-08-03T14:41:44+00:00
每日的
0.8000
找到字符串“xxxxx”并删除节点集
结果应该是
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/3982474/sitemap_nb.xsl"?>
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xsi:schemaLocation="
http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
<loc>https://www.test.com/home</loc>
<lastmod>2020-08-03T14:41:44+00:00</lastmod>
<changefreq>daily</changefreq>
<priority>0.8000</priority>
</url>
<url>
<loc>https://www.test.com/eu/index</loc>
<lastmod>2020-08-03T14:41:44+00:00</lastmod>
<changefreq>daily</changefreq>
<priority>0.8000</priority>
</url>
<url>
<loc>https://www.test.com/index</loc>
<lastmod>2020-08-03T14:41:44+00:00</lastmod>
<changefreq>daily</changefreq>
<priority>0.8000</priority>
</url>
</urlset>
https://www.test.com/home
2020-08-03T14:41:44+00:00
每日的
0.8000
https://www.test.com/eu/index
2020-08-03T14:41:44+00:00
每日的
0.8000
https://www.test.com/index
2020-08-03T14:41:44+00:00
每日的
0.8000
我使用了下面的sed注释,但它省略了urlset的元素属性。我不是shell脚本专家,请检查并建议我缺少的内容
sed -ne '/?xml/{ p; b }; /urlset/{ p; b }; /<url/{ h; b }; H; /<\/url>/{ x; /?xxxxx/b; /?xxxxx/b; p }'
sed-ne'/-xml/{p;b}/urlset/{p;b}/ 使用xmlstarlet
(对于一些默认命名为xml
的shell)
这将删除所有具有包含文本xxxxx
的子节点
的
节点。XML的根元素具有以下命名空间属性,这将导致问题
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xsi:schemaLocation="
http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
xmlstarlet-1.5.0+
建议使用前缀“”brefore node(例如,使用/:node而不是/node)
xmlstarlet ed-d///:urlset/:url[*[contains(text(),'xxxxx')]]file.xml
此解决方案完全适用于上述问题。谢谢您没有使用sed
解析XML标记,因为您没有用锤子拧入螺钉。使用正确的工具完成工作。使用XML解析器和处理器,如xmlstarlet、xsltproc、saxon…我们使用的是CentOS 6.10,它不允许安装snapd来安装xmlstarlet。因此决定使用sed@Rajaguru检查是否已安装,名称为xml
@thanasisp I只能xmllint,xmlcatalog,xmlwf
来自我的服务器,而不是“xmlstarlt”和“xml”。请检查并提出建议。他们不编辑xml。sed-ne'/?xml/{p;b};/?xml样式表/{p;b}/urlset/{p;b}/xmlstarlet安装需要snapd,但CentOS 6.10不允许安装snapd。所以我们决定使用sed。
xmlstarlet ed -d '//urlset/url[loc[contains(text(), "xxxxx")]]' file.xml
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xsi:schemaLocation="
http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">