Get version of pom.xml using xmllint

I have pom.xmlas such

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.qualtrics.sujitv</groupId>
  <artifactId>maven-test</artifactId>
  <packaging>jar</packaging>
  <version>1.2.3.4</version>
  <name>Test app</name>
  <url>http://maven.apache.org</url>

</project>

I try to use xmllint to get the version, however, when I run

xmllint --xpath 'string(project/version)' ./pom.xml

displays nothing

+4
source share
1 answer

Since your XML uses namespaces, there is no real good way to do this. For a direct xpath request, if it is safe to ignore the namespace, you can do something like this:

$ xmllint --xpath '/*[local-name()="project"]/*[local-name()="version"]/text()' pom.xml

If you want to do this in the “right” way, you need to run the shell to set the namespaces and output the desired value. Unfortunately, you will have to clear the exit prompts.

$ xmllint --shell pom.xml <<< 'setns ns=http://maven.apache.org/POM/4.0.0
cat /ns:project/ns:version/text()'
+9
source

Source: https://habr.com/ru/post/1663582/


All Articles