Xmllint: xmlns for an element without an xml element?

xmllint --xpath "//project" test.xml 

does not work

 <?xml version="1.0" encoding="UTF-8"?> <projects> <project xmlns="http://maven.apache.org/POM/4.0.0"> <modelVersion>4.0.0</modelVersion> </project> </projects> 

but successfully, if I remove the xmlns attribute as follows:

 <?xml version="1.0" encoding="UTF-8"?> <projects> <project> <modelVersion>4.0.0</modelVersion> </project> </projects> 

Are there any problems with this? Is xmlns legal for non-top level tags?

I am using Java Maven:

 mvn help:effective-pom 

and which generates xml with xmlns in non-top elements as shown.

+6
source share
2 answers

The simplest workaround is to check for local-name() :

 xmllint --xpath "//*[local-name()='project']" test.xml 

Or, define a namespace and use it:

 echo -e 'setns ns=http://maven.apache.org/POM/4.0.0\ncat //ns:project' | xmllint --shell test.xml 

See also:

Hope this helps.

+9
source

This actually succeeds when there is a namespace declaration. It returns an empty set, and this is what the specification says it returns, therefore it is considered successful.

Your definition of success seems to be different from the definition in the specification. You are not talking about this, but we can guess that you expect the elements of the “project” to return, even if they are in a different namespace from the one you are looking for.

I will not go any further; @alecxe gave you the answer, and you will find the same question a thousand times if you look for the "default XPath namespace". In the future, however, do not assume that we implicitly know what you expect from your incorrect code: let us know about the desired result; and don’t assume that we know what you mean by “failure”: tell us what is really happening.

+2
source

All Articles