使用相同的 XPath 处理多个 XML 元素
Handling multiple XML elements with the same XPath
我在同一个 XML 中使用同一个 Xpath 修改多个元素时遇到问题。这是代码:
*** 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}
有问题,给定的 Xpath 不唯一。我试过索引:
Set ${root} ${CMmessage1} \ ${Xpath}[1]
Set ${root} ${CMmessage1} \ ${Xpath}[2]
但这没用...有谁知道如何处理 Robotframework-XMLlibrary 中的索引?
您需要对这些元素进行一些处理。
使用 Get Elements 关键字。
好的,终于找到问题了。使用
Set ${root} ${CMmessage1} \ ${Xpath}[1]
错了
@{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
没问题。关键是我有 2 个 CMme,而不是 CMes 中的 2 条消息。
我在同一个 XML 中使用同一个 Xpath 修改多个元素时遇到问题。这是代码:
*** 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}
有问题,给定的 Xpath 不唯一。我试过索引:
Set ${root} ${CMmessage1} \ ${Xpath}[1]
Set ${root} ${CMmessage1} \ ${Xpath}[2]
但这没用...有谁知道如何处理 Robotframework-XMLlibrary 中的索引?
您需要对这些元素进行一些处理。
使用 Get Elements 关键字。
好的,终于找到问题了。使用
Set ${root} ${CMmessage1} \ ${Xpath}[1]
错了
@{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
没问题。关键是我有 2 个 CMme,而不是 CMes 中的 2 条消息。