【问题标题】:soapui how to get the list of nodes by using local-name in xpath expression?soapui 如何在 xpath 表达式中使用 local-name 获取节点列表?
【发布时间】:2014-05-10 19:40:53
【问题描述】:

我正在使用 soapui 进行自动化测试。我正在尝试编写一个 xpath 表达式来使用以下 xml 进行属性传输

<snapshots query="after=2014-04-16 12:30:00-0700" mask="op" xmlns="http://ws.example.com/roma/201907" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <AsOf>2014-04-16T19:20:44-07:00</AsOf>
    <offers>
        <offer entityId="6500588"/>
        <offer entityId="6500589"/>
        <offer entityId="6500590"/>
        <offer entityId="6557335">
            <rubber>KJM</rubber>
            <code>B44733</code>
            <offerCode>PA</offerCode>
            <status name="Completed">C</status>
            <startDate basis="GMT-4">2013-04-01</startDate>
            <endDate basis="GMT-4">2014-04-15</endDate>
            <template>
                <sourceKey-Ref entityId="36KTV" code="KPA513C36KTV" uri="/snapshot/sourcekey/36KTV"/>
                <pTemplateCode>PPAKXC</pTemplateCode>
                <panelCode>HPA5LTM</panelCode>
                <itemCode>PA1467</itemCode>
            </template>
            <venue code="28">
                <supervenue>5</supervenue>
            </venue>
            <currencyDetail currency="USD">
                <unitPrice>29.95</unitPrice>
                <numberPayments>1</numberPayments>
            </currencyDetail>
            <hData>
                <legacyScriptCode>300</legacyScriptCode>
                <hpKeycode>189161</hpKeycode>
                <hpProductNumber>014399</hpProductNumber>
                <hpMpgCode>300</hpMpgCode>
            </hData>
        </offer>
        <offer entityId="6557336">
            <rubber>KJM</rubber>
            <code>B44734</code>
            <offerCode>VY</offerCode>
            <status name="Completed">C</status>
            <startDate basis="GMT-4">2013-04-01</startDate>
            <endDate basis="GMT-4">2014-04-15</endDate>
            <template>
                <sourceKey-Ref entityId="36KTV" code="KPA513C36KTV" uri="/snapshot/sourcekey/36KTV"/>
                <pTemplateCode>PPAKXC</pTemplateCode>
                <panelCode>HPA5LTM</panelCode>
                <offerCode>OVYC8UM9</offerCode>
                <itemCode>VY4023</itemCode>
            </template>
            <venue code="28">
                <supervenue>5</supervenue>
            </venue>
            <currencyDetail currency="USD">
                <unitPrice>0.00</unitPrice>
                <numberPayments>1</numberPayments>
            </currencyDetail>
            <hData>
                <legacyScriptCode>947</legacyScriptCode>
                <hpKeycode>189162</hpKeycode>
                <hpProductNumber>602185</hpProductNumber>
                <hpMpgCode>947</hpMpgCode>
            </hData>
        </offer>
        <offer entityId="6557337">
            <rubber>KJM</rubber>
            <code>B44736</code>
            <offerCode>VY</offerCode>
            <status name="Completed">C</status>
            <startDate basis="GMT-4">2013-04-01</startDate>
            <endDate basis="GMT-4">2014-04-15</endDate>
            <template>
                <sourceKey-Ref entityId="36KTV" code="KPA513C36KTV" uri="/snapshot/sourcekey/36KTV"/>
                <pTemplateCode>PPAKXC</pTemplateCode>
                <panelCode>HPA5LTM</panelCode>
                <offerCode>OVYC8UMA</offerCode>
                <itemCode>VY4012</itemCode>
            </template>
            <venue code="28">
                <supervenue>5</supervenue>
            </venue>
            <currencyDetail currency="USD">
                <unitPrice>0.00</unitPrice>
                <numberPayments>1</numberPayments>
                <firstPaymentAmount>0.00</firstPaymentAmount>
                <firstShippingAmount>5.98</firstShippingAmount>
            </currencyDetail>
            <hData>
                <legacyScriptCode>947</legacyScriptCode>
                <hpKeycode>189163</hpKeycode>
                <hpProductNumber>602094</hpProductNumber>
                <hpMpgCode>947</hpMpgCode>
            </hData>
        </offer>
    </offers>
</snapshots>

我希望所有的hpKeycode 在 XPath 表达式中都使用 local-name()。我试过了

//*[local-name()='hpKeycode']

但这只给了我第一个节点 189161。这个

/*[local-name()='hpKeycode'][2]

也不行。任何帮助将不胜感激。

【问题讨论】:

  • 在 xmllint 中运行时,您的两个表达式都有效,尽管 2. 开头缺少 /
  • 感谢 Artjom。您是否使用第一个表达式获取所有节点?正如我所说,我使用的是 soapui 属性传输窗口,它总是呈现第一个元素,但有趣的是,当我使用 count(//*[local-name()='hpKeycode']) 时,它给了我正确的节点数。对于第二种情况,即使 / 我无法获得第二个元素
  • 是的,我得到了所有 3 个节点。所以这可能是一个soapui问题。我对此无能为力。

标签: xpath soapui xml-namespaces


【解决方案1】:

您可以尝试使用 XQuery。在属性转移步骤中,选择 Use XQuery 复选框,如下图所示。并使用此代码:

declare namespace soap='http://schemas.xmlsoap.org/soap/envelope/';
declare namespace ns1='http://ws.example.com/roma/201907';
<ns1:offer>
{
  for $id in //*[local-name()='hpKeycode'] return string($id)
}
</ns1:offer>

编辑:

如果你想避免使用 XQuery,你可以在你的属性转移上添加三个转移,每个使用:

第一个 ID (//*[local-name()='hpKeycode'])[1]

第二个身份证 (//*[local-name()='hpKeycode'])[2]

第三个 ID (//*[local-name()='hpKeycode'])[3]

希望这会有所帮助,

【讨论】:

  • 感谢 albciff 的及时回复。
  • 嗨 albciff,我正在向其他团队进行知识转移,他们对 xquery 有点犹豫。因此我要求上面在 xpath 中运行。
  • 嗨@user3624039 我用xpath给你一个替代答案:)。
【解决方案2】:

这不会以您期望的方式工作,因为您正在提取一组值。您需要在树的上方指定您想要的节点分支。

这就像道路......如果你给某人指路,你需要告诉他们在道路上的每个岔路口走哪条路。假设您想告诉他们如何到达 3 个可能的机场之一,每个机场都位于不同的城市。如果您说“当您到达机场 1,2 和 3 时进入 2nd”。他们会感到困惑并说“虽然是哪个城市?”也许“它们不可能存在于同一位置”。

解决方案:

根据您提供的 xml,这就是您想要的(都在 soapui 中工作)。

Xpath 2.0

//*:offer[4]/*:hData/*:hpKeycode
//*:offer[5]/*:hData/*:hpKeycode
//*:offer[6]/*:hData/*:hpKeycode

Xpath 1.0

//*[local-name()='offer'][4]/*[local-name()='hData']/*[local-name()='hpKeycode']
//*[local-name()='offer'][5]/*[local-name()='hData']/*[local-name()='hpKeycode']
//*[local-name()='offer'][6]/*[local-name()='hData']/*[local-name()='hpKeycode']

希望这个更详细的解释有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 1970-01-01
    • 2014-07-19
    • 2014-06-22
    相关资源
    最近更新 更多