如何使用xmlstarlet

时间:2019-08-08 04:38:40

标签: xml substring 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>

1 个答案:

答案 0 :(得分:2)

由于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