Search code examples
xmlxmlstarlet

How to proper add xml namespace in an xmlstarlet update command when along used with XPath


What is wrong with my xmlstarlet command? I am trying to update the version element of my pom.xml if the following constraints are true under parent element
groupId == com.cisco.aems and
version == 3.700.11-SNAPSHOT

the xml namespace concept is complicating my understanding of xmlstarlet syntax, any help on fixing my xmlstarlet problem will be appreciated. Thank you :-)

I have tried variations of the below command

xmlstarlet ed -N a="http://maven.apache.org/POM/4.0.0" -u "/a:groupId[.='com.cisco.aems' and following
-sibling::version[1] = '3.700.11-SNAPSHOT']/a:following-sibling::version[1]" -x "concat(.,'-done')" pom_sandbox_nam.xml

either I get a Invalid expression error or nothing but I don't the expected result

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">
  <modelVersion>4.0.0</modelVersion>
  <artifactId>avc-utils</artifactId>
  <parent>
    <groupId>com.cisco.aems</groupId>
    <artifactId>aems_parent_pom</artifactId>
    <version>3.700.11-SNAPSHOT</version>
    <relativePath>../../pom.xml</relativePath>
  </parent>
</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">
  <modelVersion>4.0.0</modelVersion>
  <artifactId>avc-utils</artifactId>
  <parent>
    <groupId>com.cisco.aems</groupId>
    <artifactId>aems_parent_pom</artifactId>
    <version>3.700.11-SNAPSHOT-done</version>
    <relativePath>../../pom.xml</relativePath>
  </parent>
</project>

Actual Result

<?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">
  <modelVersion>4.0.0</modelVersion>
  <artifactId>avc-utils</artifactId>
  <parent>
    <groupId>com.cisco.aems</groupId>
    <artifactId>aems_parent_pom</artifactId>
    <version>3.700.11-SNAPSHOT</version>
    <relativePath>../../pom.xml</relativePath>
  </parent>
</project>

Solution

  • Use the following command:

    xmlstarlet ed -N a="http://maven.apache.org/POM/4.0.0" -u "/a:project/a:parent/a:groupId[.='com.cisco.aems' and following-sibling::a:version[1] = '3.700.11-SNAPSHOT']/following-sibling::a:version[1]" -x "concat(.,'-done')" pom_sandbox_nam.xml
    

    It results in the following 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">
      <modelVersion>4.0.0</modelVersion>
      <artifactId>avc-utils</artifactId>
      <parent>
        <groupId>com.cisco.aems</groupId>
        <artifactId>aems_parent_pom</artifactId>
        <version>3.700.11-SNAPSHOT-done</version>
        <relativePath>../../pom.xml</relativePath>
      </parent>
    </project>
    

    You missed to specify the whole path to the element you wanted to change and not to specify the appropriate namespace.