簡體   English   中英

處理具有相同 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 中的索引?

您將需要對這些元素進行一些處理。

使用獲取元素關鍵字。

好吧,我終於找到了問題所在。 使用

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 個 CMmes,而不是 CMes 中的 2 個消息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM