XMLStarlet selects nothing

I have a typical pom.xml and want to print groupId, artifactId and a colon separated version. I think XMLStarlet is the right tool for this. I tried several ways, but always get an empty string.

xml sel -t -m project -v groupId -o : -v artifactId -o : -v version pom.xml 

Expected Result:

 org.something.apps:app-acct:5.4 

Real output: empty string

Even if I try to print only the group, I get nothing:

 xml sel -t -v project/groupId pom.xml 

I am sure that the tool sees the elements, because I can list them without problems:

 xml el pom.xml 

prints the following (correctly):

 project project/modelVersion project/parent project/parent/groupId project/parent/artifactId project/parent/version project/groupId project/artifactId project/version project/packaging 

What's wrong?

Here is a cut out version of pom.xml:

 <?xml version="1.0" encoding="UTF-8"?> <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> <parent> <groupId>org.something</groupId> <artifactId>base</artifactId> <version>1.16</version> </parent> <groupId>org.something.apps</groupId> <artifactId>app-acct</artifactId> <version>5.4</version> <packaging>war</packaging> </project> 
+6
source share
2 answers

Unfortunately, XMLStarlet is very picky about the default namespace. If the document is declared ( xmlns= ), you must also declare it for the XMLStarlet and the prefix of the elements with your chosen name (see here ):

xml sel -N my = http://maven.apache.org/POM/4.0.0 -t -m my: project -v my: groupId -o: -v my: artifactId -o: - v my: version pom .xml

Execution of the specified command gives the expected result:

 org.something.apps:app-acct:5.4 

However, if the document does NOT specify the default namespace (or the namespace has a slightly different URL), the above command will NOT work, which is the real PITA. A more universal solution is to remove the default namespace declaration before selecting items. Starting with XMLStarlet 1.3.1, converting the XML format to PYX and vice versa removes namespace declarations:

xml pyx pom.xml | xml p2x | xml sel -t -m project -v groupId -o: -v artifactId -o: -v version 2> nul

UPDATE (2014-02-12): compared to XMLStarlet 1.4.2, the PYX ↔ XML conversion is fixed (does not remove namespace declarations), so the above command will NOT work (thanks to Peter Gluck for the tip). Use the following command instead:

xml pyx pom.xml | grep -v ^ A | xml p2x | xml sel -t -m project -v groupId -o: -v artifactId -o: -v version

Note. grep above removes ALL attributes from the document, not just namespace declarations. For this particular case (choosing element values ​​from pom.xml, where elements with non-standard namespaces are not expected), this is normal, but for general XML you should remove only the default namespace names declared by it and nothing more:

xml pyx pom.xml | grep -v "^ Axmlns" | xml p2x | xml sel -t -m project -v groupId -o: -v artifactId -o: -v version


Note (deprecated): error redirection ( 2>nul ) is needed to hide the complaint about the (now) unknown xsi namespace:

-: 1.28: xsi namespace prefix for schemaLocation in project not defined

Another way to get rid of the complaint is to remove the schemaLocation attribute (in fact, this command removes all the attributes from the PYX document, not just xsi: schemaLocation):

xml pyx pom.xml | grep -v ^ A | xml p2x | xml sel -t -m project -v groupId -o: -v artifactId -o: -v version

+28
source

XML-> PYX β†’ XML trick didn’t work for me (using XMLStarlet version 1.4.2). However, the XMLStarlet documentation contains this handy sed command that removes namespace declarations from an XML document:

 sed -e 's/ xmlns.*=".*"//g' 

It worked. For the original question, the syntax will look like this:

 cat pom.xml | sed -e 's/ xmlns.*=".*"//g' | xml sel -t -m project -v groupId -o : -v artifactId -o : -v version 
+5
source

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


All Articles