Search code examples
xmlsubstringxmlstarlet

How to search and edit an xml tag that matches a condtion using xmlstarlet


I am in need of a solution using xmlstarlet to search for a xmltag based on a condition and edit its xmltag value.

My command

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

I have crafted the xmlstarlet command to search and replace the xmltag value under /properties/ifm.core_system_ui.version but I need our communities help to develop this command to search for the xmltag under <properties> that has it name that ends with version> and edit the value under that tag

Sample 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>

Expected

<?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>

Solution

  • Since xmlstarlet can't use the XPath 2.0 function ends-with(), you could try using substring() to check the end of the name...

    Try replacing:

    a:ifm.core_system_ui.version
    

    with:

    *[substring(name(),string-length(name())-string-length('version')+1)='version']
    

    Here's the updated command (untested)...

    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