In Groovy, how do I get the value in an element with a specific attribute in XML -
i have groovy script compares xml response jdbc query. trying account scenario need validate xml value based on specific attribute in element.
here example of xml:
<forminstance id="impairment_0111235228_2_174_477"> <formname>new medical history note</formname> <providerformnumber>174</providerformnumber> <formresponse id="imapairment_477_6"> <questionnumber>477</questionnumber> <questiontext>examone rx</questiontext> <answerchoice> <answerchoicetext>examone result</answerchoicetext> <olifeextension vendorcode="08" extensioncode="impairmentattributetype">lbl</olifeextension> <olifeextension vendorcode="08" extensioncode="impairmentattributeseq">2</olifeextension> </answerchoice> <olifeextension vendorcode="08" extensioncode="impairmentsequence">1</olifeextension> <olifeextension vendorcode="08" extensioncode="questionsequence">6</olifeextension> </formresponse> </forminstance>
as can see, there several elements name "olifeextension", unique extensioncode attributes. need able compare value attribute. example, using xml above, if attribute extensioncode = questionsequence, expect see value of 6.
i have tried following in script:
obj.questionsequence = xml.formresponse.olifeextension.{'@extensioncode' == 'questionsequence'} obj.questionsequence = xml.formresponse.{it.name() == 'olifeextension' && it.@extensioncode == 'questionsequence'} obj.questionsequence = xml.formresponse.olifeextension.{it.@extensioncode == 'questionsequence'}
the syntax on 3 examples appear correct because don't errors when run script, when output results, empty.
i tried:
obj.questionsequence = xml.formresponse.olifeextension[1]
this works, problem here questionsequence may not exist in same spot. position 0 or 2. so, need work off attribute name.
also, base of script using in response question if needed anyone.
compare jdbc response xml response number of nodes vary , order change?
thank in advance help.
of course, right after ask question, figure out. if has cleaner way it, appreciate it, seems trick.
obj.questionsequence = xml.formresponse.olifeextension.find{it.@extensioncode == 'questionsequence'}
Comments
Post a Comment