为默认名称空间中的元素添加tag0名称空间 [英] tag0 namespace added for elements in default namespace

查看:150
本文介绍了为默认名称空间中的元素添加tag0名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Groovy的XmlSlurper解析和修改Maven的pom.xml。我的pom.xml声明了命名空间xsi。

 <?xml version =1.0encoding =UTF-8standalone ?= 无 > 
< 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')
//操作pom
println XmlUtil.serialize(pom)

正如您注意到的,我声明第一个名称空间为空。

 <?xml version =1.0encoding =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','')


解决方案

您可以使用 XmlSlurper ,%20boolean%29> namespace awareness 就像这样:

  import groovy.xml.XmlUtil 

def pom = new XmlSlurper(false,false).parse('pom.xml')
println XmlUtil.serialize(pom)

哪一个应该给你你想要的答案......目前还不知道如何在slurp / serialize循环期间维护注释: - ($ / b
$ b

正如你所说,XmlParser可能是可能的,但是我目前的尝试失败了:-(这是一些代码在这里,哪些 可能 让你关闭,但是我还没有成功: - (


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', '')

解决方案

You can construct the XmlSlurper with no namespace awareness like so:

import groovy.xml.XmlUtil

def pom = new XmlSlurper( false, false ).parse( 'pom.xml' )
println XmlUtil.serialize(pom)

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名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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