Home > database >  Handling multiple XML elements with the same XPath
Handling multiple XML elements with the same XPath

Time:11-30

I have problems modifying multiple elements with the same Xpath in the same XML. Here is the code:

*** Settings ***
Library    XML
Library    String

*** Variables ***
${XMLpath}   AbsolutePath.xml
${Xpath}    GM/BODY/CMss/message  #(there are two elements with this xpath)

*** Test Cases ***
TestCase
    ${root}  Parse Xml  ${XMLpath}
    @{CMmessage}     Get Elements Text   ${root}  ${Xpath}
    
    ${CMmessage1}  Set Variable  @{CMmessage}[1]
       #...CMmessage1 modifications...
    Set  ${root}  ${CMmessage1}  \  ${Xpath} #Here is failing due there are multiple elements (2) matching the XPath
    
    ${CMmessage2}  Set Variable  @{CMmessage}[2]
       #...CMmessage2 modfications...
    Set  ${root}  ${CMmessage1}  \  ${Xpath} #Here is failing due there are multiple elements (2) matching the XPath

    Save Xml  ${root}  ${XMLpath}

There is the problem, Xpath given is not unique. I have tried with index:

Set ${root} ${CMmessage1} \ ${Xpath}[1] Set ${root} ${CMmessage1} \ ${Xpath}[2]

but this was useless... Does anyone have any idea how to handle with index in Robotframework-XMLlibrary?

CodePudding user response:

You will need to do some processing on those elements.

Use the Get Elements keyword.

CodePudding user response:

okay, finally I found the problem. Using

Set  ${root}  ${CMmessage1}  \  ${Xpath}[1]

it's wrong

@{CMmes1}     Get Element Text   ${root}  ${Xpath}
@{CMmes2}     Get Element Text   ${root}  ${Xpath}
...
Set  ${root}  ${CMmes1}  \  GM/BODY/CMss[1]/message
Set  ${root}  ${CMmes2}  \  GM/BODY/CMss[2]/message

it's fine. The point is that I have 2 CMmes, not 2 messages inside CMes.

  • Related