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