I'm trying to extract the value of a node from a pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<parent>
<groupId>org.me.labs</groupId>
<artifactId>my-random-project</artifactId>
<version>1.5.0</version>
</parent>
...
</project>
I need to extract the artifactId and version from the XML using a shell command. I have the following requirements/observations:
- The shell script will be done within a build assembly file we use at work, so the smaller the script the better.
- Since it'll be used on multiple systems (usually RHEL5), I'm looking for something that can run natively on default images.
- Tags like can occur elsewhere in the pom, so I can't simply awk for those tags.
I have tried the following:
xpath
works on my Mac, but isn't available by default on RHEL machines. Similarly forxmllint --xpath
, which I guess is only available on later versions ofxmllint
, which I don't have and can't enforce.xmllint --pattern
seemed promising, but I can't seem to get an output out ofxmllint --pattern '//project/parent/version' pom.xml
(prints entire XML) orxmllint --stream --pattern '//project/parent/version' pom.xml
(no output).
I realize this is a common question here on SO, but the points above are why I can't use those answers. TIA for your help.
echo "cat //*[local-name()='project']/*[local-name()='parent']/*[local-name()='version']/text()" | xmllint --shell pom.xml | sed '/^\/ >/d'
, so you only need tosed
-remove the xmllint shell stuff – Hasdrubal