Search code examples
xmlmavenxpathxmllint

xmllint: xmlns on a non-root xml element?


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

fails on

<?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 succeeds if I remove the xmlns attribute like this:

<?xml version="1.0" encoding="UTF-8"?>

<projects>
  <project>
    <modelVersion>4.0.0</modelVersion>
  </project>
</projects>

Is there some problem with this? Is xmlns legal on non-top level tags?

I'm using Java Maven:

mvn help:effective-pom

and that generates xml with the xmlns on the non-top elements like shown.


Solution

  • The easiest workaround is to check for the 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
    

    Also see:

    Hope that helps.