How to print the location of an XPath match with xmlstarlet?

Viewed 1077

I would like to find references to SNAPSHOT versions in a pom.xml file. Let's use the POM file located here for an example. I came up with the following command to find elements containing the string SNAPSHOT:

$ xmlstarlet sel -t -m "//*[contains(text(), 'SNAPSHOT')]" -v . -n pom.xml
0.2-SNAPSHOT
4.12-SNAPSHOT
1.9.13-SNAPSHOT
20.0-SNAPSHOT

This, however, as can be seen above, only gives me the text of the matches themself. What I would like to see is some more context regarding the location of the matches, for example the path leading to the matching elements, like this:

$ xmlstarlet magical arguments
/project/version: 0.2-SNAPSHOT
/project/dependencies/dependency: 4.12-SNAPSHOT
/project/properties/jackson.version: 1.9.13-SNAPSHOT
/project/properties/guava.version: 20.0-SNAPSHOT

Alternatively, having a stripped-down version of the XML as the output would also work for me, e.g.:

$ xmlstarlet magical arguments
<project>
  <version>0.2-SNAPSHOT</version>
  <dependencies>
    <dependency>
      <version>4.12-SNAPSHOT</version>
    </dependency>
  </dependencies>
  <properties>
    <jackson.version>1.9.13-SNAPSHOT</jackson.version>
    <guava.version>20.0-SNAPSHOT</guava.version>
  </properties>
</project>

Is it possible to print either of these or some other kind of indication of where the matches occured?

3 Answers

xmlstarlet can produce the requested output using the -b option which breaks the nesting:

xmlstarlet sel -t \
    -m "//*[contains(text(),'SNAPSHOT')]" \
    -m 'ancestor::*' -v 'name()' -o '/' \
    -b -v "concat(name(),': ',.)" -n pom.xml

Output:

project/version: 0.2-SNAPSHOT
project/dependencies/dependency/version: 4.12-SNAPSHOT
project/properties/jackson.version: 1.9.13-SNAPSHOT
project/properties/guava.version: 20.0-SNAPSHOT

I was unable to produce the desired result with the selection tool. I did however manage to modify the generated XSL to suit your need.

I generated the XSL with the -C switch:

xmlstarlet sel -C -t -m "//*[contains(text(), 'SNAPSHOT')]" -m 'ancestor-or-self::*' -v 'name()' -o / -n

Output:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exslt="http://exslt.org/common" version="1.0" extension-element-prefixes="exslt">
  <xsl:output omit-xml-declaration="yes" indent="no"/>
  <xsl:template match="/">
    <xsl:for-each select="//*[contains(text(), 'SNAPSHOT')]">
      <xsl:for-each select="ancestor-or-self::*">
        <xsl:call-template name="value-of-template">
          <xsl:with-param name="select" select="name()"/>
        </xsl:call-template>
        <xsl:text>/</xsl:text>
        <xsl:value-of select="'&#10;'"/>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:template>
  <xsl:template name="value-of-template">
    <xsl:param name="select"/>
    <xsl:value-of select="$select"/>
    <xsl:for-each select="exslt:node-set($select)[position()&gt;1]">
      <xsl:value-of select="'&#10;'"/>
      <xsl:value-of select="."/>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

Then I applied the following patch:

11d10
<         <xsl:value-of select="'&#10;'"/>
12a12,13
>       <xsl:value-of select="text()"/>
>       <xsl:value-of select="'&#10;'"/>

Resulting in:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exslt="http://exslt.org/common" version="1.0" extension-element-prefixes="exslt">
  <xsl:output omit-xml-declaration="yes" indent="no"/>
  <xsl:template match="/">
    <xsl:for-each select="//*[contains(text(), 'SNAPSHOT')]">
      <xsl:for-each select="ancestor-or-self::*">
        <xsl:call-template name="value-of-template">
          <xsl:with-param name="select" select="name()"/>
        </xsl:call-template>
        <xsl:text>/</xsl:text>
      </xsl:for-each>
      <xsl:value-of select="text()"/>
      <xsl:value-of select="'&#10;'"/>
    </xsl:for-each>
  </xsl:template>
  <xsl:template name="value-of-template">
    <xsl:param name="select"/>
    <xsl:value-of select="$select"/>
    <xsl:for-each select="exslt:node-set($select)[position()&gt;1]">
      <xsl:value-of select="'&#10;'"/>
      <xsl:value-of select="."/>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

If you apply this transform to the original xml file, you get the desired result:

xmlstarlet tr modified.xsl input.xml

Output:

project/version/0.2-SNAPSHOT 
project/dependencies/dependency/version/4.12-SNAPSHOT
project/properties/jackson.version/1.9.13-SNAPSHOT
project/properties/guava.version/20.0-SNAPSHOT

I came up with the following to produce a stripped-down version of the XML:

xmlstarlet ed -d "//*[count((.|.//*)[contains(text(), 'SNAPSHOT')]) = 0]" pom.xml

Output:

<?xml version="1.0"?>
<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">
  <version>0.2-SNAPSHOT</version>
  <dependencies>
    <dependency>
      <version>4.12-SNAPSHOT</version>
    </dependency>
  </dependencies>
  <properties>
    <jackson.version>1.9.13-SNAPSHOT</jackson.version>
    <guava.version>20.0-SNAPSHOT</guava.version>
  </properties>
</project>

The idea is to delete every node that does not contain the text SNAPSHOT nor has any descendants containing it. I don't really like that I had to use (.|.//*) to match either the current node or its descendants, there must be a better way, but I found that a plain .//* did not match the current node, only its descendants.

Related