Home > Blockchain >  xmlstarlet replaces nodes that do not match the xpath
xmlstarlet replaces nodes that do not match the xpath

Time:09-22

Trying to use xmlstarlet to set the version on a pom.xml The problem is that it sets the values of ALL "value" nodes in the pom.xml and not just the one that matches the XPATH Here is what I am using:

xmlstarlet ed --inplace -N x=http://maven.apache.org/POM/4.0.0 -u '//x:project//x:version' -v $NEWVERSION pom.xml

So it works but with the unwanted side-effect of setting the value in other xpaths like /project/parent/version and /project/dependencies/dependency/version Why is it doing that?

CodePudding user response:

Why is it doing that?

Because you asked for it. // is shorthand for /descendant-or-self::node()/ so the XPath expression //x:project//x:verston targets all version elements which are descendants of any project element in the namespace bound to prefix x.

To change only the version which is the child of the root element project, for example:

xmlstarlet edit --inplace -u '/_:project/_:version' -v $NEWVERSION pom.xml

where the default namespace is bound to the _ (underscore) prefix since it is declared in the root element (user's guide ch. 5).

  • Related