Extract last child element value using xmllint

1.1k views Asked by At

I have a the below XML and want to extract value of last version element

<?xml version="1.0" encoding="UTF-8"?>
<metadata>
  <groupId>org.continuous.Integration</groupId>
  <artifactId>Continuous-Integration</artifactId>
  <versioning>
    <versions>
      <version>1.11-SNAPSHOT</version>
      <version>1.12-SNAPSHOT</version>
      <version>1.13-SNAPSHOT</version>
      <version>1.14-SNAPSHOT</version>
      <version>1.15-SNAPSHOT</version>
      ..
      ..
      ..
      ..
      <version>1.N-SNAPSHOT</version>
    </versions>
    <lastUpdated>20161220194944</lastUpdated>
  </versioning>
</metadata>

I am using xmllint and this below command returning all the version values

 echo "cat //versioning/versions/version" | xmllint --shell maven-metadata.xml | sed '/^\/ >/d' | sed 's/<[^>]*.//g'

2016-12-21 17:34:52 (119 MB/s) - ‘maven-metadata.xml’ saved [463/463]

1.11-SNAPSHOT
 -------
1.12-SNAPSHOT
 -------
1.13-SNAPSHOT
 -------
1.14-SNAPSHOT
 -------
1.15-SNAPSHOT

**But I want OUTPUT should be like below.Value of last element **

1.N-SNAPSHOT

1

There are 1 answers

0
Robson Cassol On

try something like that:

xmllint --xpath "//metadata/versioning/versions/version[last()]/text()