xmllint:非根xml元素上的xmlns? [英] xmllint: xmlns on a non-root xml element?

查看:138
本文介绍了xmllint:非根xml元素上的xmlns?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

失败

<?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>

但是如果我这样删除xmlns属性,则会成功:

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>

这有什么问题吗? xmlns在非顶级标签上合法吗?

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

我正在使用Java Maven:

I'm using Java Maven:

mvn help:effective-pom

并使用如图所示的非顶部元素上的xmlns生成xml.

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

推荐答案

最简单的解决方法是检查local-name():

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

另请参阅:

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

希望有帮助.

这篇关于xmllint:非根xml元素上的xmlns?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆