JMeter:@符号(对于xml)

JMeter: @ notation (for xml)

groovy templates for jmeter page 上有一个我想效仿的例子:

String xml = “
<actions>
 <action type=”error” info=”itsErrors”/>
 <action type="warning" info=”warnWarn”/>
 <action type=”info” info=”justLogInfo”/>
</actions>"

XmlParser parser = new XmlParser()
def actions= parser.parseText (xml)
actions.action.each { action ->
    println "${action.'@type'}: ${action.'@info'}";
}

至少在我的 JMeter 5.1 中它没有像发布的那样工作,但是当我固定引号时它确实如此:

String xml = """
<actions>
 <action type="error" info="itsErrors"/>
 <action type="warning" info="warnWarn"/>
 <action type="info" info="justLogInfo"/>
</actions>"""

XmlParser parser = new XmlParser()
def actions= parser.parseText (xml)
actions.action.each { action ->
    println "${action.'@type'}: ${action.'@info'}";
}

我的问题主要是 @ 的用法,还有点和引号 (.'@type')。我尝试在网络上搜索 Groovy @ 但一无所获,因为 JMeter 符号发现 https://jmeter.apache.org/usermanual/functions.html 只有一个用法实例:

Example: ${__XPath(/path/to/build.xml, //target/@name)} This will match all targets in build.xml and return the contents of the next name attribute

关于变量相同link:

Referencing a variable in a test element is done by bracketing the variable name with '${' and '}'.

Groovy xml 的文档页面给出了其他符号: https://groovy-lang.org/processing-xml.html

def text = '''
    <list>
        <technology>
            <name>Groovy</name>
        </technology>
    </list>
'''

def list = new XmlParser().parseText(text) 

assert list instanceof groovy.util.Node 
assert list.technology.name.text() == 'Groovy' 

"${action.'@type'}: ${action.'@info'}"中的每个符号是什么意思?

即使 ${} 也不是 JMeter 变量,是吗? 我设法只继续工作 w/put ',其他部分似乎是必要的:".@{}$.我可能在最后一句话中添加了额外的内容,有些我可以解释,但只是为了确保我理解正确。

GPath groovy

中使用的语法

The most common way of querying XML in Groovy is using GPath

For XML, you can also specify attributes, e.g.:

a["@href"] → the href attribute of all the a elements

a.'@href' → an alternative way of expressing this

a.@href → an alternative way of expressing this when using XmlSlurper