为默认命名空间中的元素添加了 tag0 命名空间
我正在尝试使用 Groovy 的 XmlSlurper 解析和修改 Maven 的 pom.xml。我的 pom.xml 声明了命名空间 xsi。
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>a-group-id</groupId>
<artifactId>an-artifact-id</artifactId>
我的 Groovy 源代码如下:
import groovy.xml.XmlUtil
def pom = new XmlSlurper().parse('pom.xml')
.declareNamespace('': 'http://maven.apache.org/POM/4.0.0',
xsi: 'http://www.w3.org/2001/XMLSchema-instance')
//manipulate the pom
println XmlUtil.serialize(pom)
正如您所注意到的,我已将第一个名称空间声明为空。然而,在输出中 tag0 被添加到各处。
<?xml version="1.0" encoding="UTF-8"?>
<tag0:project xmlns:tag0="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<tag0:modelVersion>4.0.0</tag0:modelVersion>
<tag0:groupId>a-group-id</tag0:groupId>
<tag0:artifactId>an-artifact-id</tag0:artifactId>
如何避免这种情况?
目前我的解决方法是手动删除标签:
println XmlUtil.serialize(pom).replaceAll('tag0:', '').replaceAll(':tag0', '')
I'm trying to parse and modify a Maven's pom.xml using Groovy's XmlSlurper. My pom.xml declares the namespace xsi.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>a-group-id</groupId>
<artifactId>an-artifact-id</artifactId>
My Groovy source is as follows:
import groovy.xml.XmlUtil
def pom = new XmlSlurper().parse('pom.xml')
.declareNamespace('': 'http://maven.apache.org/POM/4.0.0',
xsi: 'http://www.w3.org/2001/XMLSchema-instance')
//manipulate the pom
println XmlUtil.serialize(pom)
As you notice, I've declared the first namespace as empty. However in the output tag0 is added everywhere.
<?xml version="1.0" encoding="UTF-8"?>
<tag0:project xmlns:tag0="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<tag0:modelVersion>4.0.0</tag0:modelVersion>
<tag0:groupId>a-group-id</tag0:groupId>
<tag0:artifactId>an-artifact-id</tag0:artifactId>
How to avoid that?
For the moment my workaround is removing the tags manually:
println XmlUtil.serialize(pom).replaceAll('tag0:', '').replaceAll(':tag0', '')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以构造没有 命名空间的
XmlSlurper
意识像这样:这应该给你你想要的答案...目前不知道如何在slurp/serialize周期中维护评论:-(
正如你所说,它使用 XmlParser 可能是可行的,但我当前的尝试失败了:-( 这里有一些代码可能< /strong> 让你接近,但到目前为止我还没有成功:-(
You can construct the
XmlSlurper
with no namespace awareness like so:Which should give you the answer you want... No idea currently about how to maintain comments during the slurp/serialize cycle :-(
As you say, it might be possible with XmlParser, but my current attempts have failed :-( There's some code here which might get you close, but as yet I've had no success :-(
我遇到了同样的问题,将“tag0”添加到未定义名称空间的元素(即它们位于“无名称空间”名称空间中)。我通过添加
将元素从默认命名空间重置为“无命名空间”命名空间来修复此问题。
I had the same issue with "tag0" getting added to elements that didn't define a namespace (i.e they were in the "no namespace" namespace). I fixed this by adding
which resets elements from being in the default namespace to being in the "no namespace" namespace.
我发现如果您正在处理命名空间并遇到 tag0 问题,最好使用 XmlParser 而不是 XmlSlurper。从语法上讲,它们看起来是相同的,例如:
上面的代码将准确地输出 example.xml,因为它应该包含名称空间。
如果您想以某种方式处理根,例如查找特定节点,请使用 Groovy API 并输出结果,例如
I found that it is better to use XmlParser rather than XmlSlurper if you are dealing with namespaces and having the tag0 problem. Syntactically they seem the same, eg:
The above code would output the example.xml exactly as it should be including namespaces.
If you want to process the root in some way, eg find a specific node, use the Groovy API and output the result, eg