Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 497
user2684301 Avatar asked Mar 24 '14 01:03

user2684301


2 Answers

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:

  • xmllint failing to properly query with xpath
  • XPath select node with namespace
  • xmllint and namespace

Hope that helps.

like image 101
alecxe Avatar answered Oct 02 '22 13:10

alecxe


Actually it succeeds when there is a namespace declaration. It returns an empty set, which is what the spec says it returns, so that counts as success.

Your definition of success seems to be different from the one in the spec. You don't say so, but we can guess that you are expecting the "project" elements to be returned even though they are in a different namespace from the one you are searching.

I won't go further; @alecxe has given you answer, and you will find the same question answered a thousand times if you search for "XPath default namespace". In future, though, please don't assume that we know implicitly what you expect your incorrect code to do: tell us the wanted result; and don't assume that we know what you mean by "failure": tell us what actually happens.

like image 20
Michael Kay Avatar answered Oct 02 '22 12:10

Michael Kay