在 soapui 中使用 groovy 为当前设置为 xsi:nil 的 xml 元素设置文本

Using groovy in soapui to set the text for an xml element that is currently set to xsi:nil

我正在编写一个 groovy 脚本,该脚本当前使用 groovyUtils 来更新请求消息中 xml 元素的文本。我在更新具有 xsi:nil 属性集的元素的文本时遇到问题。我想设置文本并去掉 xsi:nil 属性。以下脚本:

def text = '''
<list xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <technology>
        <name i:nil="true" />
    </technology>
</list>
'''

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context ) 
def holder = groovyUtils.getXmlHolder(text)
holder["//name"] = "newtext"

log.info holder.xml

Returns:

<list xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <technology>
        <name i:nil="true">newtext</name>
    </technology>
</list>

我应该使用什么脚本来摆脱 i:nil 属性。我希望输出为:

<list xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <technology>
        <name>newtext</name>
    </technology>
</list>

只需使用removeAttribute(String nodeName)方法如下:

def text = '''
<list xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <technology>
        <name i:nil="true" />
    </technology>
</list>
'''

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context ) 
def holder = groovyUtils.getXmlHolder(text)
holder["//name"] = "newtext"

def node = holder.getDomNode('//name')
node.removeAttribute('i:nil')

log.info holder.xml

或者你也可以使用 removeAttributeNS(String namespaceUri,String localName):

def node = holder.getDomNode('//name')
node.removeAttributeNS('http://www.w3.org/2001/XMLSchema-instance','nil')

此代码输出:

<list xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <technology>
        <name>newtext</name>
    </technology>
</list>

希望这对您有所帮助,