如何使用 xmlstarlet 搜索和编辑符合条件的 xml 标签
How to search and edit an xml tag that matches a condtion using xmlstarlet
我需要一个使用 xmlstarlet 来根据条件搜索 xmltag 并编辑其 xmltag 值的解决方案。
我的命令
xmlstarlet ed -N a="http://maven.apache.org/POM/4.0.0" -u "/a:project/a:properties/a:ifm.core_system_ui.version[.=contains(.,'3.700.999')]" -x "concat(translate(substring-before(.,','),'8','7'),',',substring-after(.,','))" pom.xml
我已经制作了 xmlstarlet 命令来搜索和替换 /properties/ifm.core_system_ui.version
下的 xmltag 值,但我需要我们的社区帮助开发此命令以搜索 <properties>
下的 xmltag 其名称以 version>
结尾并编辑该标签下的值
样本pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<properties>
<ifm.core_system_ui.version>[3.800.0, 3.800.999)</ifm.core_system_ui.version>
<ifm.fault_ui.version>[3.800.0, 3.800.999)</ifm.fault_ui.version>
<ifm.ifm_admin_ui.version>[3.800.0, 3.800.999)</ifm.ifm_admin_ui.version>
<ifm.ifm_grouping_ui.version>(3.800.0, 3.800.999)</ifm.ifm_grouping_ui.version>
<ifm.ifm_config_archive_ui.version>(3.800.0, 3.800.999)</ifm.ifm_config_archive_ui.version>
<xmp_nbi_static_content.version>(17.1.8, 18.1.999)</xmp_nbi_static_content.version>
<helpdesk_ui.version>[3.95.0,3.95.999)</helpdesk_ui.version>
<someother>[7.77.0,8.88.888)<someother>
</properties>
</project>
预计
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<properties>
<ifm.core_system_ui.version>[3.700.0, 3.800.999)</ifm.core_system_ui.version>
<ifm.fault_ui.version>[3.700.0, 3.800.999)</ifm.fault_ui.version>
<ifm.ifm_admin_ui.version>[3.700.0, 3.800.999)</ifm.ifm_admin_ui.version>
<ifm.ifm_grouping_ui.version>(3.700.0, 3.800.999)</ifm.ifm_grouping_ui.version>
<ifm.ifm_config_archive_ui.version>(3.700.0, 3.800.999)</ifm.ifm_config_archive_ui.version>
<xmp_nbi_static_content.version>(17.1.8, 18.1.999)</xmp_nbi_static_content.version>
<helpdesk_ui.version>[3.95.0,3.95.999)</helpdesk_ui.version>
<someother>[7.77.0,8.88.888)<someother>
</properties>
</project>
由于 xmlstarlet 无法使用 XPath 2.0 函数 ends-with()
,您可以尝试使用 substring()
检查名称的结尾...
尝试替换:
a:ifm.core_system_ui.version
与:
*[substring(name(),string-length(name())-string-length('version')+1)='version']
这是更新后的命令(未经测试)...
xmlstarlet ed -N a="http://maven.apache.org/POM/4.0.0" -u "/a:project/a:properties/*[substring(name(),string-length(name())-string-length('version')+1)='version'][.=contains(.,'3.700.999')]" -x "concat(translate(substring-before(.,','),'8','7'),',',substring-after(.,','))" pom.xml
我需要一个使用 xmlstarlet 来根据条件搜索 xmltag 并编辑其 xmltag 值的解决方案。
我的命令
xmlstarlet ed -N a="http://maven.apache.org/POM/4.0.0" -u "/a:project/a:properties/a:ifm.core_system_ui.version[.=contains(.,'3.700.999')]" -x "concat(translate(substring-before(.,','),'8','7'),',',substring-after(.,','))" pom.xml
我已经制作了 xmlstarlet 命令来搜索和替换 /properties/ifm.core_system_ui.version
下的 xmltag 值,但我需要我们的社区帮助开发此命令以搜索 <properties>
下的 xmltag 其名称以 version>
结尾并编辑该标签下的值
样本pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<properties>
<ifm.core_system_ui.version>[3.800.0, 3.800.999)</ifm.core_system_ui.version>
<ifm.fault_ui.version>[3.800.0, 3.800.999)</ifm.fault_ui.version>
<ifm.ifm_admin_ui.version>[3.800.0, 3.800.999)</ifm.ifm_admin_ui.version>
<ifm.ifm_grouping_ui.version>(3.800.0, 3.800.999)</ifm.ifm_grouping_ui.version>
<ifm.ifm_config_archive_ui.version>(3.800.0, 3.800.999)</ifm.ifm_config_archive_ui.version>
<xmp_nbi_static_content.version>(17.1.8, 18.1.999)</xmp_nbi_static_content.version>
<helpdesk_ui.version>[3.95.0,3.95.999)</helpdesk_ui.version>
<someother>[7.77.0,8.88.888)<someother>
</properties>
</project>
预计
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<properties>
<ifm.core_system_ui.version>[3.700.0, 3.800.999)</ifm.core_system_ui.version>
<ifm.fault_ui.version>[3.700.0, 3.800.999)</ifm.fault_ui.version>
<ifm.ifm_admin_ui.version>[3.700.0, 3.800.999)</ifm.ifm_admin_ui.version>
<ifm.ifm_grouping_ui.version>(3.700.0, 3.800.999)</ifm.ifm_grouping_ui.version>
<ifm.ifm_config_archive_ui.version>(3.700.0, 3.800.999)</ifm.ifm_config_archive_ui.version>
<xmp_nbi_static_content.version>(17.1.8, 18.1.999)</xmp_nbi_static_content.version>
<helpdesk_ui.version>[3.95.0,3.95.999)</helpdesk_ui.version>
<someother>[7.77.0,8.88.888)<someother>
</properties>
</project>
由于 xmlstarlet 无法使用 XPath 2.0 函数 ends-with()
,您可以尝试使用 substring()
检查名称的结尾...
尝试替换:
a:ifm.core_system_ui.version
与:
*[substring(name(),string-length(name())-string-length('version')+1)='version']
这是更新后的命令(未经测试)...
xmlstarlet ed -N a="http://maven.apache.org/POM/4.0.0" -u "/a:project/a:properties/*[substring(name(),string-length(name())-string-length('version')+1)='version'][.=contains(.,'3.700.999')]" -x "concat(translate(substring-before(.,','),'8','7'),',',substring-after(.,','))" pom.xml